序号 | 类别 | 主机名 | ip | 备注 |
---|---|---|---|---|
1 | ansible主机 | master | 192.168.3.7 | |
2 | ansible控机 | slaver1 | 192.168.3.8 | |
… … | ||||
注:机器的统一密码为123456 |
yum -y install epel-release
yum -y install ansible vim
sed -i "s/^#(inventory.)/\1/g" /etc/ansible/ansible.cfg
#打开主机清单列表文件
sed -i 's/^#(host_key_check.)/\1/g’ /etc/ansible/ansible.cfg#免去主机检查密钥
cat >> /etc/hosts << EOF
192.168.3.7 master
192.168.3.8 slaver1
EOF
for i in 8
do
scp /etc/hosts 192.168.3.$i:/etc/
done
cat >> /etc/ansible/hosts << EOF
[web1]
slaver1[web:children]
web1
[web:vars]
ansible_ssh_user=“root”
ansible_ssh_pass=“123456”
ansible_ssh_port=“22”
EOF
ansible all --list-host
echo ‘’ | ssh-keygen -t rsa -b 2048 -N ‘’ -f /root/.ssh/id_rsa
ansible all -m authorized_key -a “user=root exclusive=true manage_dir=true key=‘$(< /root/.ssh/id_rsa.pub)’” -k -v
ansible web1 -m copy -a “src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys”
#!/usr/bin/env expect
set PASSWD "xxx"
spawn ansible all -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub')}}' path='/root/.ssh/authorized_keys' manage_dir=no" --ask-pass -c paramiko
expect {
"SSH password:" {send "$PASSWD\r"}
}
expect eof