重点:ansible互信认证部署、ansible主机和组的定义Inventoryshell
Ansible默认是经过SSH key和远程被控制主机进行通讯,固然咱们能够SSH password来和远程主机进行通讯。 若是使用SSH KEY,则要将控制主机上的公钥放到被监控主机的/root/.ssh/authorized_keys文件中。vim
一、安装ansible和简单的配置的设置dom
安装epel源再yum安装ansible:ssh
# yum installl ansible -yide
# vim /etc/ansible/ansible.cfg工具
。。。。开发工具
# uncomment this to disable SSH key host checking测试
host_key_checking = Falsethis
二、主机组inventory设置spa
# cat /root/ans/ansible_inventory.txt
[front]
10.11.7.224 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=xuAKCeU
10.11.5.84 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=pwByh
三、建立SSH认证文件
# ssh-keygen -t rsa -N yOdaf
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/sshhost
Your identification has been saved in /root/.ssh/ansssh.
Your public key has been saved in /root/.ssh/ansssh.pub.
The key fingerprint is:
4b:8a:fb:f6:ca:58:81:b1:49:4b:47:55:c6:c1:61:df root@ecloud
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| ...+*o |
| . oo. . |
| + . . E |
| o B |
| = . S |
| . + . |
| . o . |
| =. |
| oo+o. |
+-----------------+
SSH认证文件建立成功以后,将控制主机的公钥文件 id_rsa.pub (这里更名为sshhost)添加到被控制主机的~/.ssh/authorized_keys。
# ~ 指的是控制主机和被控制主机通讯的用户家目录。
# id_rsa 是控制主机的私钥文件,要严格保管。
# id_rsa.pub 是控制主机的公钥文件,可随意分发。
四、分发公钥文件
分发添加:
# ansible front -i /root/ans/ansible_inventory.txt -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/sshhost.pub') }}'" -k
测试ping
# ansible front -i /root/ans/ansible_inventory.txt -m ping
10.11.5.84 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.11.7.224 | SUCCESS => {
"changed": false,
"ping": "pong"
指定ip操做:
# ansible front -i /root/ans/ansible_inventory.txt -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/sshhost.pub') }}'" -k --limit 10.11.7.209
分发删除:
# ansible front -i /root/ans/ansible_inventory.txt -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/sshhost.pub') }}' state=absent"
安装开发工具:
# ansible all -i /root/ans/ansible_inventory.txt -m shell -a "yum groupinstall 'Development Tools' -y"