重點: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"