RHCE模拟题
RHCE模拟题
系统信息
在本考试期间,您将操作下列虚拟系统:
系统 | IP 地址 | Ansible 角色 |
---|---|---|
control | 172.25.250.254 | ansible control node |
node1 | 172.25.250.9 | ansible managed node |
node2 | 172.25.250.10 | ansible managed node |
node3 | 172.25.250.11 | ansible managed node |
node4 | 172.25.250.12 | ansible managed node |
node5 | 172.25.250.13 | ansible managed node |
安装和配置 Ansible
安装和配置 Ansible
按照下方所述,在控制节点
control
上安装和配置 Ansible:
- 安装所需的软件包
- 创建名为
/home/greg/ansible/inventory
的静态清单文件,以满足以下要求:
node1
是dev
主机组的成员node2
是test
主机组的成员node3
和node4
是prod
主机组的成员node5
是balancers
主机组的成员prod
组是webservers
主机组的成员- 创建名为
/home/greg/ansible/ansible.cfg
的配置文件,以满足以下要求:
- 主机清单文件为
/home/greg/ansible/inventory
- playbook 中使用的角色的位置包括
/home/greg/ansible/roles
解答:
-
先ssh连接主控节点
[root@foundation0 ~]# ssh greg@control Activate the web console with: systemctl enable --now cockpit.socket Last login: Sun Nov 6 18:33:20 2022 from 172.25.250.250
-
安装
ansible
[greg@control ~]$ sudo yum -y install ansible //检查安装是否正常 [greg@control ~]$ rpm -qc ansible /etc/ansible/ansible.cfg /etc/ansible/hosts
-
添加
ansible
目录,角色目录//一键创建ansible目录和角色目录,考试可能无需创建 [greg@control ~]$ mkdir -p ansible/roles //进入目录后,直接复制ansible原本的配置文件到此目录下 [greg@control ~]$ cd ansible [greg@control ansible]$ cp /etc/ansible/ansible.cfg . //检查ansible当前使用的是不是此目录的配置文件 [greg@control ansible]$ ansible --version ansible 2.8.0 config file = /home/greg/ansible/ansible.cfg configured module search path = ['/home/greg/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Apr 3 2019, 17:26:03) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
-
修改配置文件
[greg@control ansible]$ vim ansible.cfg inventory = /home/greg/ansible/inventory ...... //取消注释,无需效验密码 host_key_checking = False ...... roles_path = /home/greg/ansible/roles ...... //取消注释,受控主机的默认用户 remote_user = root
-
配置主机清单
[greg@control ansible]$ vim inventory [dev] node1 [test] node2 [prod] node3 node4 [balancers] node5 [webservers:children] prod //验证主机清单是否配置正确 [greg@control ansible]$ ansible-inventory --graph @all: |--@balancers: | |--node5 |--@dev: | |--node1 |--@test: | |--node2 |--@ungrouped: |--@webservers: | |--@prod: | | |--node3 | | |--node4 //检查文件是否在当前目录下 [greg@control ansible]$ ls ansible.cfg inventory roles
创建和运行 Ansible 临时命令
创建和运行 Ansible 临时命令
作为系统管理员,您需要在受管节点上安装软件。
请按照正文所述,创建一个名为
/home/greg/ansible/adhoc.sh
的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库:存储库1:
- 存储库的名称为
EX294_BASE
- 描述为
EX294 base software
- 基础 URL 为
http://content/rhel8.0/x86_64/dvd/BaseOS
- GPG 签名检查为
启用状态
- GPG 密钥 URL 为
http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库为
启用状态
存储库2:
- 存储库的名称为
EX294_STREAM
- 描述为
EX294 stream software
- 基础 URL 为
http://content/rhel8.0/x86_64/dvd/AppStream
- GPG 签名检查为
启用状态
- GPG 密钥 URL 为
http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库为
启用状态
解答:
-
创建脚本,编辑内容
[greg@control ansible]$ vim /home/greg/ansible/adhoc.sh #!/bin/bash ansible all -m yum_repository -a "name=EX294_BASE description='EX294 base software' baseurl= http://content/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes" ansible all -m yum_repository -a "name=EX294_STREAM description='EX294 stream software' baseu rl=http://content/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"
-
赋予执行权限,执行脚本
[greg@control ansible]$ chmod +x adhoc.sh [greg@control ansible]$ ./adhoc.sh node5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "repo": "EX294_BASE", "state": "present" } ......
-
验证yum网络仓库是否正常使用
[greg@control ansible]$ ansible all -m shell -a 'yum repolist all' //提示信息不用管 [WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command because yum is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. //查看仓库是否都是`enabled`状态 node2 | CHANGED | rc=0 >> repo id repo name status EX294_BASE EX294 base software enabled: 1,658 EX294_STREAM EX294 stream software enabled: 4,672Last metadata expiration check: 0:05:44 ago on Sun 06 Nov 2022 08:40:56 AM EST.
安装软件包
安装软件包
创建一个名为
/home/greg/ansible/packages.yml
的 playbook :
- 将
php
和mariadb
软件包安装到dev
、test
和prod
主机组中的主机上- 将
RPM Development Tools
软件包组安装到dev
主机组中的主机上- 将
dev
主机组中主机上的所有软件包更新为最新版本
解答:
-
为了方便编写
playbook
,可以将tab
键设置为两个空格[greg@control ansible]$ vim ~/.vimrc set ts=2 sw=2
-
编辑
playbook
[greg@control ansible]$ vim /home/greg/ansible/packages.yml --- - name: 1 install package hosts: dev,test,prod tasks: - name: 1 packages installed yum: name: "{{ packages }}" vars: packages: - php - mariadb - name: 2 install package hosts: dev tasks: - name: 2 packages installed yum: name: "@RPM Development Tools" state: present - name: 2 upgrade all packages yum: name: '*' state: latest //验证即可 [greg@control ansible]$ ansible-playbook packages.yml
使用 RHEL 系统角色
使用 RHEL 系统角色
安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook
/home/greg/ansible/timesync.yml
:
- 在
所有受管节点
上运行- 使用
timesync
角色- 配置该角色,以使用当前有效的 NTP 提供商
- 配置该角色,以使用时间服务器
172.25.254.254
- 配置该角色,以启用
iburst
参数
解答:
-
安装RHEL系统角色包
//先查找一下这个包 [greg@control ansible]$ yum search role ...... ================================= Name Matched: role ================================== rhel-system-roles.noarch : Set of interfaces for unified system management //安装 [greg@control ansible]$ sudo yum -y install rhel-system-roles.noarch
-
修改配置文件
//首先查找一下RHEL角色文件路径 [greg@control ansible]$ rpm -ql rhel-system-roles-1.0-5.el8.noarch //这个路径用于写入ansible配置文件 /usr/share/ansible/roles/ ...... //这个路径为timesync角色playbook模板 /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml //修改ansible配置文件相关项 roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles //列出系统角色,检验是否路径写错 [greg@control ansible]$ ansible-galaxy list # /home/greg/ansible/roles # /usr/share/ansible/roles - linux-system-roles.kdump, (unknown version) - linux-system-roles.network, (unknown version) - linux-system-roles.postfix, (unknown version) - linux-system-roles.selinux, (unknown version) - linux-system-roles.timesync, (unknown version) - rhel-system-roles.kdump, (unknown version) - rhel-system-roles.network, (unknown version) - rhel-system-roles.postfix, (unknown version) - rhel-system-roles.selinux, (unknown version) - rhel-system-roles.timesync, (unknown version)
-
创建
playbook
//先将上面查找到的timesync角色`playbook`模板复制到当前目录下 [greg@control ansible]$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml /home/greg/ansible/timesync.yml //再根据题目进行删减和编辑 [greg@control ansible]$ vim timesync.yml --- - hosts: all vars: timesync_ntp_servers: - hostname: 172.25.254.254 iburst: yes roles: - rhel-system-roles.timesync
-
运行检查
[greg@control ansible]$ ansible-playbook timesync.yml //有些许报错为正常现象,查看最后结果即可 PLAY RECAP ********************************************************************************** node1 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6 node2 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6 node3 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6 node4 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6 node5 : ok=17 changed=4 unreachable=0 failed=0 skipped=18 rescued=0 ignored=6 //验证,查看是否都是yes和active即可 [greg@control ansible]$ ansible all -a 'timedatectl' node3 | CHANGED | rc=0 >> Local time: Sun 2022-11-06 10:18:50 EST Universal time: Sun 2022-11-06 15:18:50 UTC RTC time: Sun 2022-11-06 15:18:50 Time zone: America/New_York (EST, -0500) System clock synchronized: yes NTP service: active RTC in local TZ: no
使用 Ansible Galaxy 安装角色
使用 Ansible Galaxy 安装角色
使用 Ansible Galaxy 和要求文件
/home/greg/ansible/roles/requirements.yml
。从以下 URL 下载角色并安装到/home/greg/ansible/roles
:
http://materials/haproxy.tar
此角色的名称应当为balancer
http://materials/phpinfo.tar
此角色的名称应当为phpinfo
解答:
-
编辑
playbook
[greg@control ansible]$ vim /home/greg/ansible/roles/requirements.yml --- - src: http://materials/haproxy.tar name: balancer - src: http://materials/phpinfo.tar name: phpinfo
-
安装角色及验证
[greg@control ansible]$ ansible-galaxy install -r roles/requirements.yml - downloading role from http://materials/haproxy.tar - extracting balancer to /home/greg/ansible/roles/balancer - balancer was installed successfully - downloading role from http://materials/phpinfo.tar - extracting phpinfo to /home/greg/ansible/roles/phpinfo - phpinfo was installed successfully //查看是否有了`balancer`和`phpinfo`角色 [greg@control ansible]$ ansible-galaxy list # /home/greg/ansible/roles - balancer, (unknown version) - phpinfo, (unknown version) ......
创建和使用角色
创建和使用角色
根据下列要求,在
/home/greg/ansible/roles
中创建名为apache
的角色:
httpd 软件包已安装,设为在
系统启动时启用
并启动
防火墙
已启用并正在运行,并使用允许访问Web
服务器的规则模板文件
index.html.j2
已存在,用于创建具有以下输出的文件/var/www/html/index.html
:Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的
完全限定域名
,IPADDRESS
则是受管节点的 IP 地址。
解答:
-
创建角色
[greg@control ansible]$ cd roles/ [greg@control roles]$ ansible-galaxy init apache - apache was created successfully [greg@control roles]$ ls apache balancer phpinfo requirements.yml
-
编辑
playbook
[greg@control roles]$ vim apache/tasks/main.yml --- # tasks file for apache - name: 1 Start service httpd, if not started service: name: httpd state: started enabled: yes - name: 2 start firewalld service: name: firewalld state: started enabled: yes - name: 3 firewalld for http firewalld: service: http permanent: yes state: enabled immediate: yes - name: 4 Template a file to index.html.j2 template: src: index.html.j2 dest: /var/www/html/index.html
-
查找IP地址变量和主机名变量
[greg@control ansible]$ ansible node1 -m setup -a 'filter=*ipv4*' node1 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.25.250.9", "192.168.122.1" ], //ansible_default_ipv4.address为IP地址变量 "ansible_default_ipv4": { "address": "172.25.250.9", "alias": "enp1s0", "broadcast": "172.25.250.255", "gateway": "172.25.250.254", "interface": "enp1s0", "macaddress": "52:54:00:00:fa:09", "mtu": 1500, "netmask": "255.255.255.0", "network": "172.25.250.0", "type": "ether" }, "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false } [greg@control ansible]$ ansible node1 -m setup -a 'filter=*name*' node1 | SUCCESS => { //考试的时候ansible_hostname为空,所以我们使用nsible_nodename "ansible_facts": { "ansible_hostname": "node1", "ansible_nodename": "node1.lab.example.com", "ansible_product_name": "KVM", "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false } //主机名变量也可以使用ansible_fqdn,但考试的时候也可能为空,具体看考试环境 [greg@control ansible]$ ansible node1 -m setup -a 'filter=*fqdn*' node1 | SUCCESS => { "ansible_facts": { "ansible_fqdn": "node1.lab.example.com", "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false }
-
查找到相关变量之后就可以写入配置文件了
[greg@control roles]$ vim apache/templates/index.html.j2 Welcome to {{ ansible_nodename }} on {{ ansible_default_ipv4.address }}
从 Ansible Galaxy 使用角色
从 Ansible Galaxy 使用角色
根据下列要求,创建一个名为
/home/greg/ansible/roles.yml
的 playbook :
playbook 中包含一个 play, 该 play 在
balancers
主机组中的主机上运行并将使用balancer
角色。
此角色配置一项服务,以在
webservers
主机组中的主机之间平衡 Web 服务器请求的负载。浏览到
balancers
主机组中的主机(例如http://172.25.250.13
)将生成以下输出:Welcome to serverb.lab.example.com on 172.25.250.11
重新加载浏览器将从另一 Web 服务器生成输出:
Welcome to serverc.lab.example.com on 172.25.250.12
playbook 中包含一个 play, 该 play 在
webservers
主机组中的主机上运行并将使用phpinfo
角色。
请通过 URL
/hello.php
浏览到webservers
主机组中的主机将生成以下输出:Hello PHP World from FQDN
其中,FQDN 是主机的完全限定名称。
Hello PHP World from serverb.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。
同样,浏览到
http://172.25.250.12/hello.php
会生成以下输出:Hello PHP World from serverc.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。
解答:
-
编辑
playbook
[greg@control ansible]$ vim /home/greg/ansible/roles.yml --- - name: phpinfo playbook hosts: webservers roles: - role: phpinfo - role: apache - name: balancer playbook hosts: balancers roles: - role: balancer
-
运行
playbook
[greg@control ansible]$ ansible-playbook /home/greg/ansible/roles.yml PLAY RECAP ********************************************************************************** node3 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 node4 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 node5 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
-
测试
[greg@control ansible]$ curl http://172.25.250.13 Welcome to node3.lab.example.com on 172.25.250.11 [greg@control ansible]$ curl http://172.25.250.12 Welcome to node4.lab.example.com on 172.25.250.12
创建和使用逻辑卷
创建和使用逻辑卷
创建一个名为
/home/greg/ansible/lv.yml
的 playbook ,它将在所有受管节点
上运行以执行下列任务:
创建符合以下要求的逻辑卷:
- 逻辑卷创建在
research
卷组中- 逻辑卷名称为
data
- 逻辑卷大小为
1500 MiB
使用
ext4
文件系统格式化逻辑卷如果无法创建请求的逻辑卷大小,应显示错误信息
Could not create logical volume of that size
,并且应改为使用大小
800 MiB
。如果卷组
research
不存在,应显示错误信息Volume group done not exist
。
不要以任何方式挂载逻辑卷
解答:
-
查找卷组变量
[greg@control ansible]$ ansible all -m setup -a 'filter=*lvm*' node5 | SUCCESS => { "ansible_facts": { //使用下面的环境变量 "ansible_lvm": { "lvs": {}, "pvs": { "/dev/vdb": { "free_g": "5.00", "size_g": "5.00", "vg": "research" } }, "vgs": { "research": { "free_g": "5.00", "num_lvs": "0", "num_pvs": "1", "size_g": "5.00" } } }, "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false } ......
-
创建
playbook
[greg@control ansible]$ vim /home/greg/ansible/lv.yml --- - name: create all hosts: all tasks: - block: - name: 1 create lv 1500 lvol: vg: research lv: data size: 1500 - name: 2 create a filesystem filesystem: fstype: ext4 dev: /dev/research/data when: ansible_lvm.vgs.research is defined rescue: - debug: msg: Could not create logical volume of that size - name: 3 create lv 800 lvol: vg: research lv: data size: 800 when: ansible_lvm.vgs.research is defined - debug: msg: Volume group done not exist when: ansible_lvm.vgs.research is not defined
-
执行
[greg@control ansible]$ ansible-playbook lv.yml //注意查看各个信息是否都显示
-
查看
[greg@control ansible]$ ansible all -a 'lvs' node2 | CHANGED | rc=0 >> LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data research -wi-a----- 1.46g node5 | CHANGED | rc=0 >> LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data research -wi-a----- 1.46g node4 | CHANGED | rc=0 >> LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data research -wi-a----- 1.46g node3 | CHANGED | rc=0 >> LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data research -wi-a----- 800.00m node1 | CHANGED | rc=0 >> //查看是否格式化 [greg@control ansible]$ ansible all -a 'blkid /dev/research/data' node5 | CHANGED | rc=0 >> /dev/research/data: UUID="c1441f1d-321a-4f81-baf9-299cae83a7ea" TYPE="ext4" node2 | CHANGED | rc=0 >> /dev/research/data: UUID="82c86d8d-8b68-4396-a056-a52cfa7ee4f9" TYPE="ext4" node3 | FAILED | rc=2 >> non-zero return code node4 | CHANGED | rc=0 >> /dev/research/data: UUID="5ff0a6ed-21a7-4a06-9d7e-928f68231425" TYPE="ext4" node1 | FAILED | rc=2 >> non-zero return code
生成主机文件
生成主机文件
- 将一个初始模板文件从
http://materials/hosts.j2
下载到/home/greg/ansible
- 完成该模板,以便用它生成以下文件:针对每个清单主机包含一行内容,其格式与
/etc/hosts
相同- 创建名为
/home/greg/ansible/hosts.yml
的 playbook ,它将使用此模板在dev
主机组中的主机上生成文件/etc/myhosts
。该 playbook 运行后,
dev
主机组中主机上的文件/etc/myhosts
应针对每个受管主机包含一行内容:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.25.250.9 node1.lab.example.com node1 172.25.250.10 node2.lab.example.com node2 172.25.250.11 node3.lab.example.com node3 172.25.250.12 node4.lab.example.com node4 172.25.250.13 node5.lab.example.com node5
注:清单主机名称的显示顺序不重要。
修改文件内容
修改文件内容
按照下方所述,创建一个名为
/home/greg/ansible/issue.yml
的 playbook :
- 该 playbook 将在
所有清单主机
上运行- 该 playbook 会将
/etc/issue
的内容替换为下方所示的一行文本:
- 在
dev
主机组中的主机上,这行文本显示 为:Development
- 在
test
主机组中的主机上,这行文本显示 为:Test
- 在
prod
主机组中的主机上,这行文本显示 为:Production
创建 Web 内容目录
创建 Web 内容目录
按照下方所述,创建一个名为
/home/greg/ansible/webcontent.yml
的 playbook :
该 playbook 在
dev
主机组中的受管节点上运行创建符合下列要求的目录
/webdev
:
- 所有者为
webdev
组- 具有常规权限:
owner=read+write+execute , group=read+write+execute ,other=read+execute
- 具有
特殊权限
:设置组 ID用符号链接将
/var/www/html/webdev
链接到/webdev
创建文件
/webdev/index.html
,其中包含如下所示的单行文件:Development
在
dev
主机组中主机上浏览此目录(例如http://172.25.250.9/webdev/
)将生成以下输出:Development
生成硬件报告
生成硬件报告
创建一个名为
/home/greg/ansible/hwreport.yml
的 playbook ,它将在所有受管节点上生成含有以下信息的输出文件/root/hwreport.txt
:
清单主机名称
- 以
MB
表示的总内存大小
BIOS 版本
- 磁盘设备
vda 的大小
- 磁盘设备
vdb 的大小
- 输出文件中的每一行含有一个 key=value 对。
您的 playbook 应当:
- 从
http://materials/hwreport.empty
下载文件,并将它保存为/root/hwreport.txt
- 使用
正确的值
改为 /root/hwreport.txt- 如果硬件项不存在,相关的值应设为
NONE
创建密码库
创建密码库
按照下方所述,创建一个 Ansible 库来存储用户密码:
- 库名称为
/home/greg/ansible/locker.yml
- 库中含有两个变量,名称如下:
pw_developer
,值为Imadev
pw_manager
,值为Imamgr
- 用于加密和解密该库的密码为
whenyouwishuponastar
- 密码存储在文件
/home/greg/ansible/secret.txt
中
创建用户帐户
创建用户帐户
- 从
http://materials/user_list.yml
下载要创建的用户的列表,并将它保存到/home/greg/ansible
- 在本次考试中使用在其他位置创建的密码库
/home/greg/ansible/locker.yml
。创建名为/home/greg/ansible/users.yml
的 playbook ,从而按以下所述创建用户帐户:
- 职位描述为
developer
的用户应当:
- 在
dev
和test
主机组中的受管节点上创建- 从
pw_developer
变量分配密码- 是补充组
devops
的成员- 职位描述为
manager
的用户应当:
- 在
prod
主机组中的受管节点上创建- 从
pw_manager
变量分配密码- 是补充组
opsmgr
的成员- 密码采用
SHA512
哈希格式。- 您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件
/home/greg/ansible/secret.txt
正常运行。
更新 Ansible 库的密钥
更新 Ansible 库的密钥
按照下方所述,更新现有 Ansible 库的密钥:
- 从
http://materials/salaries.yml
下载 Ansible 库到/home/greg/ansible
- 当前的库密码为
insecure8sure
- 新的库密码为
bbs2you9527
- 库使用
新密码
保持加密状态
配置 cron 作业
配置cron作业
- 创建⼀个名为
/home/greg/ansible/cron.yml
的 playbook- 配置 cron 作业,该作业每隔 2 分钟运⾏并执⾏以下命令:
logger "EX294 in progress"
,以⽤户natasha
身份运⾏