新增被控机:192.168.3.71、192.168.3.72、192.168.3.251
如果被控机python版本低于2.6,需升级到2.6版本或以上。
/etc/ansible/hosts:内容如下:
-
[wsf1]
-
192.168.3.71
-
[wsf2]
-
192.168.3.72
-
[se]
-
192.168.3.251
-
#生成秘钥,免交互。
-
root@ubuntu:/etc/ansible#ssh-keygen -t rsa -P "" -f /home/ubuntu/.ssh/id_rsa
-
需从ansible主控机分发公钥到被控机:
-
root@ubuntu:/etc/ansible#ssh-copy-id -i /root/.ssh/id_rsa.pub -p 8020 test@192.168.3.71
-
root@ubuntu:/etc/ansible#ssh-copy-id -i /root/.ssh/id_rsa.pub -p 8020 test@192.168.3.72
-
root@ubuntu:/etc/ansible#ssh-copy-id -i /root/.ssh/id_rsa.pub -p 8020 test@192.168.3.251
-
被控机使用非root用户
-
root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping -u test
-
192.168.3.72 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
-
192.168.3.71 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
-
192.168.3.251 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
-
被控机使用root用户
-
root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping -u root
-
或
-
root@ubuntu:/etc/ansible# ansible se,wsf1,wsf2 -m ping
-
192.168.3.72 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
-
192.168.3.71 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
-
192.168.3.251 | SUCCESS => {
-
"changed": false,
-
"ping": "pong"
-
}
分发公钥脚本:
-
#!/bin/bash
-
IP_CONF="/etc/ansible/hosts"
-
IP_INFO="$(cat ${IP_CONF}|grep "^[0-9]")"
-
SSH_USER=`uname -n`
-
SSH_PUB="/home/${SSH_USER}/.ssh/id_rsa.pub"
-
#SSH_PWD='123456'
-
-
if [ ! -f ${SSH_PUB} ];then
-
echo "NO FOUND ${SSH_PUB}."
-
exit 1
-
fi
-
if [ ! -f ${IP_CONF} ];then
-
echo "NO FOUND ${IP_CONF}."
-
exit 1
-
fi
-
for i in ${IP_INFO}
-
do
-
echo $i
-
sleep 1
-
#-o StrictHostKeyChecking=no,第一次连接服务器时,自动接受新的公钥。或者修改/etc/ssh/ssh_config文件中StrictHostKeyChecking no
-
sshpass -p "${SSH_PWD}" ssh-copy-id -i /home/${SSH_USER}/.ssh/id_rsa.pub -p 8020 kylinyunwei@${i} -o StrictHostKeyChecking=no
-
done
阅读(1891) | 评论(0) | 转发(0) |