1.下表列出了有关四个受管主机的信息。您将根据主机的用途、所在的城市以及它所属的部署 环境,将每个主机分配给多个组以进行管理。此外,美国城市组(罗利和山景城)必须设为组\us**的子项(children),这样美国的主机就可以作为一 个组进行管理。
主机名称 | 用途 | 位置 | 运行环境 |
servera.lab.example.com | Web服务器 | 罗利raleigh | 开发development |
serverb.lab.example.com | Web服务器 | 罗利 | 测试testing |
serverc.lab.example.com | Web服务器 | 山景城mountainview | 生产production |
serverd.lab.example.com | Web服务器 | 伦敦london | 生产 |
yum 安装ansible
1.确认控制主机能够上网
2.配置三个源(本地源、epel源、Centos-stream)
(1)配置本地源
[root@server ~]# mount /dev/sr0 /mnt
[root@server ~]# vim /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=file:///mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///mnt/AppStream
gpgcheck=0
(2)配置阿里云的扩展源
[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*(3)配置centos-stream源
[root@server ~]# vim /etc/yum.repos.d/Centos-stream.repo
[AppStream1]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/x86_64/os/
gpgcheck=0
[BaseOS1]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
gpgcheck=03.获取ansible软件包
[root@server ~]# yum install ansible -y
4.判断是否安装成功
[root@server ~]# ansible --version
ansible [core 2.12.7]
config file = /root/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
jinja version = 2.11.3
libyaml = True
配置
#编辑配置文件
[root@server ~]# vim /etc/ansible/ansible.cfg
[defaults]
inventory=/etc/ansible/hosts
#配置主机清单
vim /etc/ansible/hosts
[raleigh]
servera.lab.example.com
serverb.lab.example.com
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production:children]
mountainview
london
[mountainview]
serverc.lab.example.com
[london]
serverd.lab.example.com
[root@server ~]# ansible-inventory --graph #查看主机组
@all:
|--@development:
| |--servera.lab.example.com
|--@production:
| |--@london:
| | |--serverd.lab.example.com
| |--@mountainview:
| | |--serverc.lab.example.com
|--@raleigh:
| |--servera.lab.example.com
| |--serverb.lab.example.com
|--@testing:
| |--serverb.lab.example.com
|--@ungrouped:
[root@server ~]#
2.实现ansible配置的四个案例
1)安装和配置ansible以及ansible控制节点server.example.com如下:
2)创建一个名为/home/student/ansible/inventory的静态库存文件如下所示:
2.1)node1 是dev主机组的成员
2.2)node2是test主机组的成员
2.3)node1和node2是prod主机组的成员
2.4)node1是balancers主机组的一员
2.5)prod组是webservers主机组的成员
3).创建一个名为/home/student/ansible/ansible.cfg的配置文件,如下所示:
3.1主机库存文件/home/student/ansible/inventory
3.2剧本中角色的位置被定义为/home/student/ansible/roles
4)控制主机中登录student用户,remote_user = devops,开启特权升级 ()
添加student用户
[root@server ~]# useradd student
[root@server ~]# cd /home/student #切换目录到student家目录下
[root@server student]# mkdir ansible #创建ansible目录
[root@server student]# vim ansible/inventory #配置库文件
[dev]
node1
[test]
nod2
[prod]
node1
node2
[balancers]
[webserbers:children]
prod[root@server student]# unset ANSIBLE_CONFIG #取消变量
[root@server student]# echo $ANSIBLE_CONFIG #查看[root@server student]# vim ansible/ansible.cfg #编辑配置文件
[defaults]
inventory=/home/student/ansible/inventory
roles_path=/home/student/ansible/roles
remote_user=devops
[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=False
#切换用户身份到stuenet测试
[root@server student]# su - student
[student@server ~]$ cd ansible
[student@server ansible]$ unset ANSIBLE_CONFIG
[student@server ansible]$ ansible --version
ansible [core 2.12.7]
config file = /home/student/ansible/ansible.cfg
configured module search path = ['/home/student/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
ansible collection location = /home/student/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
jinja version = 2.11.3
libyaml = True
[student@server ansible]$