1、控制节点yum安装epel-release 和 ansible
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install ansible -y
[root@localhost ~]# ansible
[root@localhost ~]# cd /etc/ansible/
[root@localhost ~]# vim ansible.cfg
【根据实际情况配置参数】
inventory = /etc/ansible/hosts
library = /usr/share/my_modules/
forks = 5
sudo_user = root
remote_port = 22
host_key_checking = False
timeout = 60
log_path = /var/log/ansible.log
2、配置被控制节点
[root@localhost ~]# vim hosts
[web]
192.168.193.20
192.168.193.22
(这里添加一个web组,下有两台服务器)
3、配置公私钥,用加密方式生成公钥
[root@localhost ~]# ssh-keygen -t rsa
【一直回车到结束】
向被控制节点192.168.193.20分发私钥
[root@localhost ~]# ssh-copy-id root@192.168.193.20
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.193.20 (192.168.193.20)' can't be established.
ECDSA key fingerprint is SHA256:+iFwpdLBAirR98HIl0BaE4S85siezj4PZwJecotxRUs.
ECDSA key fingerprint is MD5:85:81:b3:4e:e9:07:c8:2b:9f:06:99:8c:95:86:65:79.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.193.20's password: 【输入控制节点root密码】
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.193.20'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
向被控制节点192.168.193.22分发私钥
[root@localhost ~]# ssh-copy-id root@192.168.193.22
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.193.22 (192.168.193.22)' can't be established.
ECDSA key fingerprint is SHA256:PtXL4y7BbOiWL9kUzVaPrVEf855pyDFgYZhAIxddRck.
ECDSA key fingerprint is MD5:94:f7:12:77:7d:b4:89:c4:e0:fd:a7:22:22:35:f9:b6.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.193.22's password: 【输入控制节点root密码】
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.193.22'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#
4、测试验证ansible命令
[root@localhost ~]# ansible "web" --list
hosts (2):
192.168.193.20
192.168.193.22
[root@localhost ~]# time ansible web -m ping
192.168.193.22 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.193.20 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
real 0m1.037s
user 0m0.502s
sys 0m0.321s
【ansible前面加time可以显示出命令执行时间】