ansible安装
1.yum安装
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install ansible
2.编辑/etc/ansible/hosts文件
[root@localhost ~]# vim /etc/ansible/hosts
[websevers]
192.168.0.37
192.168.0.45
192.168.0.46
192.168.0.47
3.SSH免密认证
yum install -y sshpass
ssh-keygen -f /root/.ssh/id_rsa -P ''
export IP="192.168.1.37 192.168.1.45 192.168.1.46 192.168.1.47" #如做host解析,直接使用hostname
export SSHPASS=123123
for HOST in $IP;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $HOST
done
或者生成ip文件推送密钥
for i in {99..128};do echo 172.16.51.$i >> iplist.txt;done
vim sshpush.sh
#!/usr/bin/bash
export SSHPASS="123456"
for i in `cat /root/iplist.txt`
do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $i
done
4.举例
[root@k8s-master01 ~]# ansible al1 -m ping
192.168.0.37 | SUCCESS => {
"ansible facts":
"discovered_interpreter_python":"/usr/bin/python"
},
"changed": false,
"ping":"pong"
}
192.168.0.45 | SUCCESS => {
"ansible facts":
"discovered_interpreter_python":"/usr/bin/python"
},
"changed": false,
"ping":"pong"
}
192.168.0.46 | SUCCESS => {
"ansible facts":
"discovered_interpreter_python":"/usr/bin/python"
},
"changed": false,
"ping":"pong"
}
192.168.0.47 | SUCCESS => {
"ansible facts":
"discovered_interpreter_python":"/usr/bin/python"
},
"changed": false,
"ping":"pong"
}
root@k8s-master01 ~# ansible all -m shell -a date
192.168.0.46 | CHANGED | rc=0 >>
Mon Oct 31 13:54:45 CST 2022
192.168.0.45 | CHANGED | rc=0 >>
Mon Oct 31 13:54:45 CST 2022
192.168.0.47 | CHANGED | rc=0 >>
Mon Oct 31 13:54:45 CST 2022
192.168.0.37 | CHANGED | rc=0 >>
Mon Oct 31 13:54:45 CST 2022