ansible 是基于模块工作的, 本身没有批量部署的能力. 真正具有批量部署的是 ansible 所运行的模块, ansible 只是提供一种框架. 主要包括:
(1), 连接插件 connection plugins: 负责和被监控端实现通信;
(2),host inventory: 指定操作的主机, 是一个配置文件里面定义监控的主机;
(3), 各种模块核心模块, command 模块, 自定义模块;
(4), 借助于插件完成记录日志邮件等功能;
(5),playbook: 剧本执行多个任务时, 非必需可以让节点一次性运行多个任务.-- 摘自 360 百科
简而言之 ansible 有如下的特点:
(一) 批量管理工具
(二) 模块
(三)python
(四) 无终端, 是基于 SSH 实现管理的
(五) 也支持主从模式
(六) 也支持 playbook
Ansible 的安装
可以直接使用 yum 进行安装, 前提是你已经配置了 epel 源
第一步: yum 安装 ansible
[root@ken ~]# yum install ansible -y
第二步: 查看 ansible 的版本信息
可以看到我的安装版本是 2.6.2 的
- [root@ken ~]# ansible --version
- ansible 2.6.2
- config file = /etc/ansible/ansible.cfg
- configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
- ansible python module location = /usr/lib/python2.7/site-packages/ansible
- executable location = /usr/bin/ansible
- python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
第三步: 查看 ansible 配置文件
我们接下来各个节点的管理主要是配置 / etc/ansible/hosts 文件
- [root@ken ~]# rpm -qc ansible
- /etc/ansible/ansible.cfg
- /etc/ansible/hosts
Ansible 使用基本配置
第一步: 把需要管理的节点的 IP 地址加入到 / etc/ansible/hosts 文件中
在文件末行添加如下三行信息, 第一行是定义了一个主机组, 下面两行只要把需要管理的 IP 地址即可填写进去即可
- [root@ken ~]# VIM /etc/ansible/hosts
- ...
- # Here's another example of host ranges, this time there are no
- # leading 0s:
- ## db-[99:101]-node.example.com
- ############## 在末行添加如下信息 #################
- [ken]
- 10.220.5.138
- 10.220.5.139
第二步: 发送秘钥至被操控节点
ansible 是基于 sshd 服务, 所以如果我们需要管理其他节点的话, 需要给各个节点发送秘钥
在主节点生成秘钥, 发送至各个被监控节点
使用如下脚本即可进行批量安装
- #!/bin/bash
- . /etc/init.d/functions
- #author: 技术流 ken
- #date:2018.11.16
- #desc:this script for SSH key
- # 下载 expect
- yum install expect -y &>/dev/null
- if [ $? -eq 0 ];then
- echo -n "download expect"
- success
- echo ""
- else
- echo -n "download expect"
- failure
- echo ""
- exit 8
- fi
- # 删除保存的秘钥信息
- if [ -f id_rsa -o -f id_rsa.pub -o known_hosts ];then
- rm -rf /root/.SSH/id*
- rm -rf /root/.SSH/known*
- fi
- # 自动生成秘钥对
- /usr/bin/expect<<eof
- spawn SSH-keygen
- expect {
- "(/root/.ssh/id_rsa)" {send \r;exp_continue}
- "passphrase" {send \r;exp_continue}
- "again" {send \r}
- }
- expect eof
- exit
- eof
- # 在各个节点分发秘钥
- for i in 37 38 39
- do
- ken=10.220.5.1$i
- /usr/bin/expect<<eof
- spawn SSH-copy-id $ken
- expect {
- "yes/no" {send yes\r;exp_continue}
- "password" {send o\r}
- }
- expect eof
- exit
- eof
- done
Ansible 使用基本格式
ansible 使用格式
可以输入一个 ansible 回车即可看到使用格式
- [root@ken ~]# ansible
- Usage: ansible <host-pattern> [options]
ansible 常用使用选型
你可以输入 ansible 回车看到很多的选型, 这里选取几个比较常用的选项进行说明
-m: 指定模块名称
-a: 指定模块的具体参数
-s: 以 sudo 的方式运行操作
-i: 指定被管理节点的主机列表
-f: 一批连接几个主机进行操作 (默认是 5 个主机)
Ansible 模块使用帮助
正如我们前文介绍的, ansible 是基于模块来工作的, 所以要想使用 ansible, 必须对 ansible 的模块有个清晰的认识.
查看模块
可以使用如下命令进行查看所支持的模块
[root@ken ~]# ansible-doc -l
获取模块使用帮助
使用 - s 指定获取 shell 模块的使用帮助
- [root@ken ~]# ansible-doc -s shell
- - name: Execute commands in nodes.
- shell:
- chdir: # cd into this directory before running the command
- creates: # a filename, when it already exists, this step will *not* be run.
- executable: # change the shell used to execute the command. Should be an absolute path to the executable.
- free_form: # (required) The shell module takes a free form command to run, as a string. There's not an
- actual option named "free form". See the examples!
- removes: # a filename, when it does not exist, this step will *not* be run.
- stdin: # Set the stdin of the command directly to the specified value.
- warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to
- no/false.
Ansible 管理节点的三种方法
还记得刚才在 hosts 文件添加的三行内容吗?
- [ken]
- 10.220.5.138
- 10.220.5.139
在使用 ansible 的时候你可以指定主机组名, 也可以指定一个 IP, 也可以用 all 来表示全部, 因为你的配置文件里面可能不止一个主机组名, 想要实现批量管理, 就要用到了 all.
所以这里有三种使用方法
(一) 指定主机组名
通过如下的命令就可以获取到了整个主机组节点的信息
- [root@ken ~]# ansible ken -m command -a "hostname"
- 10.220.5.138 | SUCCESS | rc=0>>
- ken
- 10.220.5.139 | SUCCESS | rc=0>>
- ken
(二) 指定一个特定 IP
指定 ip 10.220.5.138 获取特定节点的信息
- [root@ken ~]# ansible 10.220.5.138 -m command -a "ip a"
- 10.220.5.138 | SUCCESS | rc=0>>
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- .NET 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
- link/ether 00:0c:29:a9:90:16 brd ff:ff:ff:ff:ff:ff
- .NET 10.220.5.138/24 brd 10.220.5.255 scope global noprefixroute eth0
- valid_lft forever preferred_lft forever
- inet6 fe80::20c:29ff:fea9:9016/64 scope link
- valid_lft forever preferred_lft forever
(三) 使用 all
因为在配置文件里面我只定义了一个主机组, 所以这里呈现的效果和使用 ken 是一样的, 大家可以尝试定义多个主机组, 再使用 all.
- [root@ken ~]# ansible all -m command -a "ls /tmp"
- 10.220.5.138 | SUCCESS | rc=0>>
- ansible_TpWP26
- hsperfdata_root
- hsperfdata_zabbix
- systemd-private-495d844cb6f24a5fa04192c973de9274-chronyd.service-SVap94
- systemd-private-495d844cb6f24a5fa04192c973de9274-httpd.service-Grw0SF
- systemd-private-79452c683402427e944cc4959183f774-httpd.service-DENLXJ
- systemd-private-79452c683402427e944cc4959183f774-ntpd.service-cH4QGP
- systemd-private-f0243ed42bf34679b61e0687522914f6-chronyd.service-DADZWt
- systemd-private-f0243ed42bf34679b61e0687522914f6-httpd.service-lCPw92
- vmware-root
- 10.220.5.139 | SUCCESS | rc=0>>
- ansible_bxGz8A
- systemd-private-2e376cd91398450f85a81bc060207ef8-chronyd.service-TxdhUO
- systemd-private-2e376cd91398450f85a81bc060207ef8-httpd.service-k8IZOZ
- systemd-private-5c9f32d6cff64520b10075e086d943ab-chronyd.service-iAH3c0
- systemd-private-5c9f32d6cff64520b10075e086d943ab-httpd.service-dsAqeg
- systemd-private-65ded84926e64a90b0a201a805f752ca-chronyd.service-eSj3iR
- systemd-private-6706ba5361284cd4a0c91f3c8b68c606-chronyd.service-sLgAei
- systemd-private-6706ba5361284cd4a0c91f3c8b68c606-httpd.service-op5Yg7
- vmware-root
- yum_save_tx.2018-11-15.16-02.KHC9kd.yumtx
来源: https://www.cnblogs.com/kenken2018/p/9970683.html