RHCE9练习题 (EX294)
· 阅读需 24 分钟
Lab环境
FQDN | Description | IP Addresses | Roles |
---|---|---|---|
control.lab.example.com | control | 172.25.250.254 | ansible control node |
classroom.lab.example.com | classroom | 172.25.250.254 | materials |
content.lab.example.com | content | 172.25.250.254 | YUM repo |
node1.lab.example.com | node1 | 172.25.250.9 | ansible managed node |
node2.lab.example.com | node2 | 172.25.250.10 | ansible managed node |
node3.lab.example.com | node3 | 172.25.250.11 | ansible managed node |
node4.lab.example.com | node4 | 172.25.250.12 | ansible managed node |
node5.lab.example.com | node5 | 172.25.250.13 | ansible managed node |
utility.lab.example.com | utilit | 172.25.250.220 | utility |
1. 安装和配置 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。
- 自定义collection目录为 /home/greg/ansible/mycollection。
ssh greg@control
# 安装软件包
sudo yum -y install ansible-core ansible-navigator
# 创建所需文件目录
mkdir -p /home/greg/ansible/roles
mkdir /home/greg/ansible/mycollection
cd ansible/
# 生成配置文件
ansible-config init --disabled > /home/greg/ansible/ansible.cfg
vim ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
remote_user = greg
host_key_checking = False
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles
collections_path = ./mycollection/:.ansible/collections:/usr/share/ansible/collections
[privilege_escalation]
become=True
# 确认配置文件
ansible --version
ansible-galaxy list
# 创建清单文件
vim /home/greg/ansible/inventory
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod
# 确认清单文件的正确性
ansible-inventory --graph
# ping测试
ansible all -m ping
考试过程可以使用ansible-navigator,如要用ansible-navigator先登陆podman并验证一下。
podman login utility.lab.example.com -u admin -p redhat
ansible-navigator images
ansible-navigator collections
2. 配置系统以使用默认存储库
作为系统管理员,您需要在受管节点上安装软件。
请按照正⽂所述,创建一个名为 /home/greg/ansible/yum_repo.yml 的剧本,在各个受管节点上安装 yum 存储库。
- 配置存储库1:
- 名称:EX294_BASE
- 描述:EX294 base software
- 基础 URL:http://content/rhel9.0/x86_64/dvd/BaseOS
- GPG 签名检查:启用
- GPG 密钥 URL:http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库状态:启用
- 配置存储库2:
- 名称:EX294_STREAM
- 描述:EX294 stream software
- 基础 URL:http://content/rhel9.0/x86_64/dvd/AppStream
- GPG 签名检查:启用
- GPG 密钥 URL:http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库状态:启用
# 查找模块名
ansible-doc -l | grep yum
# doc
ansible-doc yum_repository
# 编写playbook
vim /home/greg/ansible/yum_repo.yml
---
- name: Configure YUM repositories
hosts: all
tasks:
- name: Configure EX294_BASE repository
yum_repository:
file: EX294_BASE
name: EX294_BASE
description: "EX294 base software"
baseurl: http://content/rhel9.0/x86_64/dvd/BaseOS
gpgcheck: yes
gpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
enabled: yes
- name: Configure EX294_STREAM repository
yum_repository:
file: EX294_STREAM
name: EX294_STREAM
description: "EX294 stream software"
baseurl: http://content/rhel9.0/x86_64/dvd/AppStream
gpgcheck: yes
gpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
enabled: yes
# 执行playbook
ansible-navigator run yum_repo.yml -m stdout
# 验证配置
ansible all -a 'yum repoinfo'
ansible all -a 'yum -y install ftp'
ansible all -a 'rpm -q ftp'
3.安装软件包
创建一个名为 /home/greg/ansible/packages.yml 的playbook,并按以下要求在受管节点上安装软件包:
- 将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上。
- 将 RPM Development Tools 软件包组安装到 dev 主机组中的主机上。
- 将 dev 主机组中主机的所有软件包更新到最新版本。
# doc
ansible-doc yum
# 编写playbook
vim /home/greg/ansible/packages.yml
---
- name: Install php and mariadb
hosts: dev,test,prod
tasks:
- name: Install required packages
yum:
name:
- php
- mariadb
state: present
- name: Install RPM Development Tools and upgrade packages
hosts: dev
tasks:
- name: Install RPM Development Tools group
yum:
name: "@RPM Development Tools"
state: present
- name: Upgrade all packages to the latest version
yum:
name: "*"
state: latest
# 执行playbook
ansible-navigator run packages.yml -m stdout
# 验证
ansible dev,test,prod -a 'rpm -q php mariadb'
ansible dev -a 'yum grouplist'
ansible dev -a 'yum update'
4.使用 RHEL 系统角色
创建一个名为 /home/greg/ansible/selinux.yml 的playbook,满足以下要求:
- 在 所有受管节点 上运行。
- 使用 RHEL 提供的 selinux 系统角色。
- 将受管节点的 SELinux 配置为 enforcing 模式。
# 查询系统角色包名
yum search role
# 安装
sudo yum -y install rhel-system-roles
# 查看当前可用的system roles
ansible-galaxy list
cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml /home/greg/ansible/selinux.yml
vim selinux.yml
# 显示行号,并删除不需要的内容(不同版本,行号可能有些差异,自己看着删除不需要要的内容)
:set nu
:43,51d
:11,39d
最终内容:
---
- hosts: all
become: true
become_method: sudo
become_user: root
vars:
# Use "targeted" SELinux policy type
selinux_policy: targeted
# Set "enforcing" mode
selinux_state: enforcing
# Prepare the prerequisites required for this playbook
tasks:
- name: execute the role and catch errors
block:
- name: Include selinux role
include_role:
name: rhel-system-roles.selinux
rescue:
# Fail if failed for a different reason than selinux_reboot_required.
- name: handle errors
fail:
msg: "role failed"
when: not selinux_reboot_required
- name: restart managed host
reboot:
- name: wait for managed host to come back
wait_for_connection:
delay: 10
timeout: 300
- name: reapply the role
include_role:
name: rhel-system-roles.selinux
# 执行playbook
# 通过rpm包安装的role,用ansible-playbook执行,通过collection安装的角色用ansible-navigator执行
ansible-playbook selinux.yml
# 验证
ansible all -m shell -a 'grep ^SELINUX= /etc/selinux/config; getenforce'
node3 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node2 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node5 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node1 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node4 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
5.配置 Collection
- http://classroom/materials
- redhat-insights-1.0.7.tar.gz
- community-general-5.5.0.tar.gz
- redhat-rhel_system_roles-1.19.3.tar.gz
- 上⾯3个collection安装到 /home/greg/ansible/mycollection ⽬录中
vim requirements.yml
---
collections:
- name: http://classroom/materials/redhat-insights-1.0.7.tar.gz
- name: http://classroom/materials/community-general-5.5.0.tar.gz
- name: http://classroom/materials/redhat-rhel_system_roles-1.19.3.tar.gz
# install
ansible-galaxy collection install -r requirements.yml -p /home/greg/ansible/mycollection
# 验证
ansible-navigator collections
ansible-navigator doc community.general.filesystem -m stdout
6.使用Ansible Galaxy 安装角色
使用 Ansible Galaxy 和要求文 件(/home/greg/ansible/roles/requirements.yml)。从以下 URL 下载角色并安装到目录 /home/greg/ansible/roles:
- URL: http://classroom/materials/haproxy.tar, 此⻆⾊的名称应当为 balancer
- URL: http://classroom/materials/phpinfo.tar, 此⻆⾊的名称应当为 phpinfo
vim /home/greg/ansible/roles/requirements.yml
---
- src: http://classroom/materials/haproxy.tar
name: balancer
- src: http://classroom/materials/phpinfo.tar
name: phpinfo
# install
ansible-galaxy install -r /home/greg/ansible/roles/requirements.yml
# 验证
ansible-galaxy list
7.创建和使用角色
根据以下要求,在目录 /home/greg/ansible/roles 中创建一个名为 apache 的角色,并满足以下条件:
- httpd 软件包已安装,设为在 系统启动时启⽤并启动
- 防⽕墙 已启⽤并正在运⾏,并使⽤允许访问 Web 服务器的规则
- 模板⽂件 index.html.j2 已存在,⽤于创建具有以下输出的⽂件
- /var/www/html/index.html :
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的 完全限定域名 , IPADDRESS 则是受管节点的 IP 地址。
- /var/www/html/index.html :
创建⼀个名为 /home/greg/ansible/apache.yml 的 playbook:
- 该 play 在 webservers 主机组中的主机上运⾏并将使⽤ apache ⻆⾊
# 创建角色apache
ansible-galaxy role init --init-path /home/greg/ansible/roles apache
# 编写task
vim /home/greg/ansible/roles/apache/tasks/main.yml
---
- name: Install Apache
yum:
name: httpd
state: latest
- name: Start and enable Apache service
systemd:
name: httpd
state: started
enabled: yes
- name: Start and enable firewalld
systemd:
name: firewalld
state: started
enabled: yes
- name: Configure firewalld to allow HTTP
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: Deploy index.html template
template:
src: index.html.j2
dest: /var/www/html/index.html
vim /home/greg/ansible/roles/apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
# 编写playbook
vim /home/greg/ansible/apache.yml
---
- name: Deploy Apache role
hosts: webservers
roles:
- apache
# 执行playbook
ansible-navigator run apache.yml -m stdout
# 验证
ansible webservers -a 'systemctl status httpd'
ansible webservers -a 'firewall-cmd --list-all'
ansible webservers --list-hosts
curl http://node3
curl http://node4