一、Ansible的安装
epel源
dnf install ansible -y
ansible --viersion
ansible的基本信息:
/etc/ansible/ansible.conf ##全局配置文件,默认很少修改
/etc/ansible/hosts ##全局主机清单清单文件
dnf install sshpass-1.06-9.el8.x86_64.rpm -y
dnf install ansible-2.9.11-1.el8.noarch.rpm -y
二、主控机和被控机之间免密连接
[root@ansible mnt]# ssh-keygen ##生成key
[root@ansible mnt]# dnf install expect -y
[root@ansible mnt]# vim sshkey.sh ##通过脚本将key传给主机210和211
[root@ansible mnt]# cat sshkey.sh
#!/bin/bash
AUTOSSH()
{
/usr/bin/expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.$i
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "westos\r" }
}
expect eof
EOF
}
for i in 210 211
do
AUTOSSH
done
[root@ansible mnt]# sh sshkey.sh
[root@ansible mnt]# ssh -l root 172.25.254.211 ##测试,可以直接免密登陆
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Fri Nov 26 11:09:48 2021 from 172.25.254.70
[root@westoslinux ~]#
三 、构建Anisble清单
清单就是ansible控制主机的列表
/etc/ansible/hosts ##全局清单文件
1.直接书写受管主机名或ip,每行一个
node1.westos.com
node2.westos.com
172.25.254.240
2.设定受管主机的组[组名称]
清单查看:
ansible 清单中组名称 [-i 清单文件]
ansible ungrouped --list-hosts
ansible all --list-hosts