Ansible是一个简单高效的自动化运维管理工具,用Python开发(安装时依赖Python版本,控制主机master器要求Python2.6以上,被控制的主机server需要Python2.6以上。centos6.8自带Python2.6),能大批量管理N多台机器,可以并发的在多台机器上部署应用、安装软件、执行命令、配置和编排任务。
安装ansible之前要检查控制机的Python版本:
Python -V 即可显示版本
因为控制机是Python2.6,也可以安装Python2.7。(详见Python2.7.13的安装)
Python版本合格之后开始安装ansible
一、安装ansible
1台控制主机(master):192.168.1.88
3台被控制主机(server):
192.168.1.18
192.168.1.98
192.168.1.185
控住主机:
yum install epel-release
yum install ansible
二、配置
vim /etc/ansible/hosts
在最后面加上被控制主机的IP:
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
## db-[99:101]-node.example.com //此行为原文件的最后一行
192.168.1.98
192.168.1.18
192.168.1.185
vim /etc/ansible/ansible.cfg
[accelerate]
#accelerate_port = 5099
accelerate_port = 10000
#accelerate_timeout = 30
#accelerate_connect_timeout = 5.0
# If set to yes, accelerate_multi_key will allow multiple
# private keys to be uploaded to it, though each user must
# have access to the system via SSH to add a new key. The default
# is "no".
accelerate_multi_key = yes
三、实现控制主机和被控制主机之间的无密码认证
同样是控制主机:
ssh-keygen
一路回车,即可在/root/.ssh目录下生成id_rsa和id_rsa.put私钥和公钥两个文件。
然后将钥匙给到被控制的主机:
ssh-copy-id 192.168.1.18
ssh-copy-id 192.168.1.98
ssh-copy-id 192.168.1.185
至此,安装配置已经完成,测试是否成功
四、测试:
在控制主机上执行命令,看被控制主机的反应:
[root@localhost ~]# ansible all -m ping
192.168.1.98 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.1.185 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.1.18 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
被控制主机有响应,说明安装配置成功。
注意可能会有下面的警告,没有报错即可运行。
[DEPRECATION WARNING]: ACCELERATE_MULTI_KEY option, Removing accelerate as a
connection method, settings not needed either. . This feature will be removed in
version 2.5. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: ACCELERATE_PORT option, Removing accelerate as a
connection method, settings not needed either. . This feature will be removed in
version 2.5. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
五、实例
[root@localhost ~]# ansible 192.168.1.98 -a "touch /mnt/file3"
[DEPRECATION WARNING]: ACCELERATE_TIMEOUT option, Removing accelerate as a
connection method, settings not needed either. . This feature will be removed
in version 2.5. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: ACCELERATE_MULTI_KEY option, Removing accelerate as a
connection method, settings not needed either. . This feature will be removed
in version 2.5. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Consider using file module with state=touch rather than running
touch
192.168.1.98 | SUCCESS | rc=0 >>
注意参数 -a 后面用双引号或者单引号“”将命令引起来,即
ansible 192.168.1.98 -a "command"