yum安装ansible
1、在hosts文件中为主机做解析
2、安装epel源
3、安装ansible
4、配置公钥认证【非必须】ssh-keygen
如未做公钥认证可使用-u -k选项指定账号和密码,且最好保证连接时不需要yes确认
例:
[root@localhost ~]# ansible 192.168.12.4 -m ping
1. 环境
ansible 192.168.12.2
client1 192.168.12.3
client2 192.168.12.4
2. 服务端安装ansible,客户端无需安装,关闭selinux
setenforce 0
systemctl stop firewalld
yum install -y epel-release
yum install -y ansible
3. 服务端生成密钥对,目录/root/.ssh/
ssh-keygen -t rsa //无需设置单独密码,生成公钥和秘钥id_rsa.pub id_rsa
4. 将服务端公钥内容放到客户机和本机认证文件中,默认是600权限
ssh-copy-id -i 192.168.12.4(客户端)
5. 修改ansible配置文件/etc/ansible/hosts,添加组以及组内IP
[webservers] #自定义主机组名字
192.168.12.3 #组内机器ip或者机器域名(需先配置好/etc/hosts)
192.168.12.4
6.在服务端测试
[root@localhost prometheus]# ansible 192.168.12.3 -m ping
192.168.12.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost prometheus]# ansible webservers -m ping
192.168.12.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.12.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}