1)安装和配置ansible以及ansible控制节点server.example.com
修改主机名:
hostnamectl set-hostname server.example.com
配置静态IP
nmcli connection modify ens160 ipv4.addresses 172.25.250.100/24 ipv4.gateway 172.25.250.2 ipv4.method manual connection.autoconnect yes
安装ansible
[root@server ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@server ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@server ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
2)创建一个名为/home/student/ansible/inventory的静态库存文件如下所示:
先创建student用户
[root@server ~]# useradd student
[root@server ~]# passwd student
切换到student身份
[root@server ~]# su - student
创建文件/home/student/ansible/inventory
[student@server ~]$ mkdir ~/ansible
[student@server ansible]$ touch inventory
2.1)node1 是dev主机组的成员
2.2)node2是test主机组的成员
2.3)node1和node2是prod主机组的成员
2.4)node1是balancers主机组的一员
2.5)prod组是webservers主机组的成员
编写静态库存文件
[student@server ansible]$ vim inventory
[dev]
node1
[test]
node2
[prod]
node1
node2
[balancers]
node1
[webservers:children]
prod
3).创建一个名为/home/student/ansible/ansible.cfg的配置文件,如下所示:
创建文件/home/student/ansible/ansible.cfg
[student@server ansible]$ touch ansible.cfg
3.1主机库存文件/home/student/ansible/inventory
3.2剧本中角色的位置被定义为/home/student/ansible/roles
编写该配置文件
[student@server ansible]$ vim ansible.cfg
[defaults]
inventroy=./inventory
remote_user=devops
ask_pass=false
[privilege_escalation]
become=true
become_method =sudo
become_user=root
become_ask_pass=false
4)控制主机中登录student用户,remote_user = devops,开启特权升级
在上一步中已经做过了