RHCE题库
环境准备
1、
[kiosk@foundation ~]$ virt-manager
[kiosk@foundation ~]$ rht-vmctl reset all
输入y确认重置所有主机
[kiosk@foundation ~]$ ssh -X root@workstation
[root@workstation ~]# dnf install -y ansible
[root@workstation ~]# vim /etc/sudoers.d/student
student ALL=(ALL) NOPASSWD: ALL
[root@workstation ~]# for i in server{a…d} bastion
do scp /etc/sudoers.d/student root@$i:/etc/sudoers.d/
done
2、更改workstation、servera、serverb、serverc、serverd、bastion
主机的/etc/hosts文件,把文件中content.example.com对应的ip改为172.25.254.250
[root@workstation ~]# for i in server{a…d} bastion
do scp /etc/hosts root@$i:/etc/hosts
done
3、使用xshell将考试环境需要的那些文件都上传到/content/目录下
4、关闭bastion的httpd服务
ssh root@bastion
systemctl stop httpd
systemctl disable httpd
1、安装和配置Ansible
按照下方所述,在控制节点workstation.lab.example.com 上安装和配置Ansible:
安装所需的软件包
创建名为/home/student/ansible/inventory的静态清单文件, 以满足以下需求:
servera是dev主机组的成员
serverb是test主机组的成员
serverc和serverd是prod主机组的成员
bastion是balancers主机组的成员
prod组是webservers主机组的成员
创建名为/home/student/ansible/ansible.cfg的配置文件, 以满足以下要求:
主机清单文件为/home/student/ansible/inventory
playbook中使用的角色的位置包括/home/student/ansible/roles
解答:
[root@foundation0 ~]# ssh student@workstation
Activate the web console with: systemctl enable --now cockpit.socket
[student@workstation ansible]$ cp /etc/ansible/ansible.cfg .
[student@workstation ansible]$ ls
ansible.cfg
[student@workstation ansible]$ mkdir roles
inventory = /home/student/ansible/inventory
remote_user = student
···
roles_path = /home/student/ansible/roles
host_key_checking = False
···
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@workstation ansible]$ vim inventory
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
bastion
[webservers:children]
prod
验证:
[student@workstation ansible]$ ansible all -m ping
serverd | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
bastion | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
2、创建和运行Ansible临时命令
作为系统管理员, 您需要在受管节点上安装软件.
请按照下方所述, 创建一个名为/home/student/ansible/adhoc.sh的shell脚本,
该脚本将使用Ansible临时命令在各个受管节点上安装yum存储库:
存储库1:
存储库的名称为 rh294_BASE
描述为 rh294 base software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
GPG签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存储库为开启状态
存储库2:
存储库的名称为 rh294_STREAM
描述为 rh294 stream software
基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/AppStream
GPG签名检查为启用状态
GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存储库为开启状态
解答:
[student@workstation ansible]$ vim adhoc.sh
[student@workstation ansible]$ cat adhoc.sh
#!/bin/bash
ansible all -m you_repository -a 'file=server name=rh294_BASE description="rh294 base software" baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"'
ansible all -m you_repository -a 'file=server name=rh294_STREAM description="rh294 stream software" baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes'
[student@workstation ansible]$ chmod +x adhoc.sh
[student@workstation ansible]$ ./adhoc.sh
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_BASE",
"state": "present"
}
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_BASE",
"state": "present"
}
bastion | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_BASE",
"state": "present"
}
serverc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_BASE",
"state": "present"
}
serverd | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_BASE",
"state": "present"
}
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_STREAM",
"state": "present"
}
serverc | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_STREAM",
"state": "present"
}
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_STREAM",
"state": "present"
}
serverd | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_STREAM",
"state": "present"
}
bastion | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"repo": "rh294_STREAM",
"state": "present"
}
3、安装软件包
创建一个名为 /home/student/ansible/packages.yml的 playbook:
将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
将 RPM Development Tools 软件包组安装到 dev主机组中的主机上
将 dev 主机组中主机上的所有软件包更新为最新版本
解答:
[student@workstation ansible]$ vim packages.yml
[student@workstation ansible]$ cat packages.yml
---
- name: install php mariadb
hosts: dev,test,prod
tasks:
- name: install php mariadb
yum:
name:
- php
- mariadb
state: present
- name: install RPM
hosts: dev
tasks:
- name: yum RPM
yum:
name: "@RPM Development Tools"
state: present
- name: update all packages
yum:
name: '*'
state: latest
[student@workstation ansible]$ ansible-playbook packages.yml
PLAY [install php mariadb] **************************************************************************
TASK [Gathering Facts] ******************************************************************************
ok: [serverc]
ok: [serverd]
ok: [servera]
ok: [serverb]
TASK [install php mariadb] **************************************************************************
changed: [serverd]
changed: [serverc]
changed: [serverb]
changed: [servera]
PLAY [install RPM] **********************************************************************************
TASK [Gathering Facts] ******************************************************************************
ok: [servera]
TASK [yum RPM] **************************************************************************************
changed: [servera]
TASK [update all packages] **************************************************************************
ok: [servera]
PLAY RECAP ******************************************************************************************
servera : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverb : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverc : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、使用RHEL系统角色
安装 RHEL 系统角色软件包,并创建符合以下条件的playbook /home/student/ansible/timesync.yml:
在所有受管节点上运行
使用 timesync 角色
配置该角色,以使用当前有效的 NTP 提供商
配置该角色,以使用时间服务器 classroom.example.com
配置该角色,以启用 iburst 参数
解答:
[student@workstation ansible]$ sudo yum install -y rhel-system-roles
[student@workstation ansible]$ cd roles/
[student@workstation roles]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync/ timesync
[student@workstation roles]$ ls
timesync
[student@workstation roles]$ cd ..
[student@workstation ansible]$ vim timesync.yml
[student@workstation ansible]$ cat timesync.yml
---
- name: set time
hosts: all
vars:
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yes
roles:
- timesync
[student@workstation ansible]$ ansible-playbook timesync.yml
5、使用Ansible Galaxy安装角色
使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/student/ansible/roles:
http://content.example.com/haproxy.tar.gz 此角色的名称应当为 balancer
http://content.example.com/phpinfo.tar.gz 此角色的名称应当为 phpinfo
解答:
[student@workstation roles]$ vim requirements.yml
[student@workstation roles]$ cat requirements.yml
---
- name: balancer
src: http://content.example.com/haproxy.tar.gz
- name: phpinfo
src: http://content.example.com/phpinfo.tar.gz
[student@workstation roles]$ ansible-galaxy install -r requirements.yml -p .
- downloading role from http://content.example.com/haproxy.tar.gz
- extracting balancer to /home/student/ansible/roles/balancer
- balancer was installed successfully
- downloading role from http://content.example.com/phpinfo.tar.gz
- extracting phpinfo to /home/student/ansible/roles/phpinfo
- phpinfo was installed successfully
[student@workstation roles]$ ls
balancer phpinfo requirements.yml selinux timesync
6、创建和使用角色
根据下列要求,在/home/student/ansible/roles中创建名为apache的角色:
httpd软件包已安装,设为在系统启动时启用并启动
防火墙已启用并正在运行,并使用允许访问Web服务器的规则
模板文件 index.html.j2 已存在,用于创建具有以下输出的文件/var/www/html/index.html:
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME是受管节点的完全限定域名,IPADDRESS则是受管节点的IP地址。
按照下方所述,创建一个使用此角色的playbook /home/student/ansible/newrole.yml:
该playbook在webservers主机组中的主机上运行
解答:
[student@workstation roles]$ cat apache/tasks/main.yml
---
# tasks file for apache
- name: install httpd firewalld
yum:
name:
- httpd
- firewalld
state: present
- name: cp template
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
- name: restart firewalld
service:
name: firewalld
state: restarted
enabled: yes
- name: firewalld for http
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
[student@workstation roles]$ cat apache/templates/index.html.j2
Welcome to {
{
inventory_hostname }} on {
{
ansible_default_ipv4.address }}
[student@workstation roles]$ cd ..
[student@workstation ansible]$ vim newrole.yml
[student@workstation ansible]$ cat newrole.yml
---
- name: user apache
hosts: webservers
roles:
- apache
[student@workstation ansible]$ ansible-playbook newrole.yml
PLAY [user apache] ***************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************
ok: [serverd]
ok: [serverc]
TASK [apache : install httpd firewalld] ******************************************************************************************************************
ok: [serverd]
ok: [serverc]
TASK [apache : cp template] ******************************************************************************************************************************
changed: [serverd]
changed: [serverc]
TASK [apache : restart httpd] ****************************************************************************************************************************
changed: [serverd]
changed: [serverc]
TASK [apache : restart firewalld] ************************************************************************************************************************
changed: [serverc]
changed: [serverd]
TASK [apache : firewalld for http] ***********************************************************************************************************************
changed: [serverc]
changed: [serverd]
PLAY RECAP ***********************************************************************************************************************************************
serverc : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证结果:
[student@workstation ansible]$ curl serverc
Welcome to serverc on 172.25.250.12
[student@workstation ansible]$ curl serverd
Welcome to serverd on 172.25.250.13
7、从Ansible Galaxy使用角色
根据下列要求,创建一个名为 /home/student/ansible/roles.yml的playbook:
playbook中包含一个play,该play在balancers主机组中的主机上运行并将使用balancer角色。
此角色配置一项服务,以在webservers主机组中的主机之间平衡Web服务器请求的负载。
浏览到balancers主机组中的主机(例如http://bastion.lab.example.com/ )将生成以下输出:
Welcome to serverc.example.com on 172.25.1.12
重新加载浏览器将从另一Web服务器生成输出:
Welcome to serverd.example.com on 172.25.1.13
playbook 中包含一个 play,该 play 在 webservers主机组中的主机上运行并将使用 phpinfo 角色。
通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
Hello PHP World from FQDN
其中,FQDN是主机的完全限定名称。
例如,浏览到 http://serverc.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverc.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
同样,浏览到 http://serverd.lab.example.com/hello.php 会生成以下输出:
Hello PHP World from serverd.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
解答:
[student@workstation ansible]$ vim roles.yml
[student@workstation ansible]$ cat roles.yml
---
- name: get webservers fact
hosts: webservers
- name: user balacer role
hosts: balancers
roles:
- balancer
- name: user phpinfo role
hosts: webservers
roles:
- phpinfo
再来执行该playbook
[student@workstation ansible]$ ansible-playbook roles.yml
PLAY [get webservers fact] *******************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************
ok: [serverd]
ok: [serverc]
PLAY [user balacer role] *********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************
ok: [bastion]
TASK [balancer : Install haproxy] ************************************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and
specifying `name: "{
{ item }}"`, please use `name: ['haproxy']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings
can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [bastion] => (item=['haproxy'])
TASK [balancer : Configure the haproxy cnf file] *********************************************************************************************************
changed: [bastion]
TASK [balancer : Start the haproxy service] **************************************************************************************************************
changed: [bastion]
TASK [balancer : Install firewalld] **********************************************************************************************************************
ok: [bastion]
TASK [balancer : Start and enable firewalld] *************************************************************************************************************
ok: [bastion]
TASK [balancer : Enable http in firewall] *********************************************