首先,你得有一台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.12192.168.1.13192.168.1.14192.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