首先,你得有一台ansible的服務端,就是安裝個ansible軟件,例如:
yum -y install ansible-2.4.2.0-2.el7.noarch.rpm
編輯配置文件,新增管理節點:
vim /etc/ansible/ansible.cfg
------------------------------------------------------
去掉注釋:inventory = /etc/ansible/hosts
去掉注釋:host_key_checking = False
------------------------------------------------------
vim /etc/ansible/host
------------------------------------------------------
[app1]
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
[app:children]
app1
[app:vars]
ansible_ssh_user="root"
ansible_ssh_pass="123456"
ansible_ssh_port="22"
------------------------------------------------------
啟動ansible服務,設置開機自啟動
systemctl start ansible
systemctl enable ansible
本地生成密鑰:
ssh-keygen -N '' -f /root/.ssh/id_rsa
利用anslbie命令發送公鑰給其他管理節點:
ansible all -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys"
搞定:ssh登陸測試(直接免密登陸):
ssh root@192.168.1.11
[root@localhost ~]#
下面,只要刪除/etc/ansible/hosts下的部分配置即可,以下信息均可刪除
------------------------------------------------------
[app:children]
app1
[app:vars]
ansible_ssh_user="root"
ansible_ssh_pass="123456"
ansible_ssh_port="22"
------------------------------------------------------
相比較用使用以下這條命令來說,更容易記憶,理解(那么碩大的一條命令,根本記不住啊~!!!)
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/authorized_keys)'" -k -v