自定义环境
1.操作环境
角色 | 主机名 | IP地址 | 组名 |
控制主机 | server.example.com | 192.168.90.134 | server |
受控主机 | node1.example.com | 192.168.90.135 | node1 |
受控主机 | node2.example.com | 192.168.90.133 | node2 |
需要保准三台主机能够互相通信。设置同一种网络模式(nat)。
2.配置IP地址
server---192.168.90.134
node1---192.168.90.135
node2---192.168.90.133
3.修改主机名
hostnamectl set-hostname server.exmaple.com
hostnamectl set-hostname node1.example.com
hostnamectl set-hostname node2.example.com
安装ansible
1.查看yun配置
#cd /etc/yum.repos.d
删除多余的(yum配置)源文件
[root@server yum.repos.d]# ll
2.配置centos8基础源
vim /etc/yum.repos.d/base.repo
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
gpgcheck=0
[BaseOS]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
gpgcheck=0
3.配置epel
#yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
成功显示:
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
yum install ansible -y
4.判断是否安装成功能显示对应版本
案例一:控制主机和受控主机通过root用户通过免密验证方式远程控住受控主机实施对应任务
1.控住主机--server通过主机名匹配对应连接的受控主机
#vim /etc/hosts
192.168.90.135 node1 node1.example.com
192.168.90.133 node2 node2.example.com
2.制作秘钥
# ssh-keygen -t rsa -P '' -q -f ~/.ssh/id_rsa
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
3.发送密钥
ssh-copy-id -i node1
ssh-copy-id -i node2
4.验证
[root@server ~]# ssh node1
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Fri Dec 30 21:05:46 2022 from 192.168.90.1
[root@node1 ~]# exit
logout
Connection to node1 closed.
[root@server ~]# ssh node2
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Dec 31 05:06:00 2022 from 192.168.90.1
[root@node2 ~]# exit
logout
Connection to node2 closed.
[root@server ~]# ssh node1 hostname
node1.example.com
[root@server ~]# ssh node2 hostname
node2.example.com