陆大明 发表于 2023-5-12 16:27:09

RHCE认证(RedHat8)

考前说明:所有项目运行过程中出现红色字体的报错信息是正常的,运行完成后看 “failed=0” 就代表执行成功,如果在执行任务期间暂停并且报错那么代表项目内部书写格式或者命令输入错误,请认真检查

此文档为考前模拟不代表真实考试环境及内容。

环境说明:

systemIP AddressRoleworkstation.lab.example.com172.25.250.9Ansible control nodeservera.lab.example.com172.25.250.10Ansible managed nodeserverb.lab.example.com172.25.250.11Ansible managed nodeserverc.lab.example.com172.25.250.12Ansible managed nodeserverd.lab.example.com172.25.250.13Ansible managed nodebastion.lab.example.com172.25.250.254Ansible managed node帐户信息:

这些系统的 IP 地址采用静态设置,主机名称解析已配置为解析上方列出的主机名。 请勿更改这些 设置。
foundation0 主机(以下简称 f0)的 root 密码为 Asimov ,f0 上其他用户的密码均为 redhat
f0 里面所有虚拟系统的 root 密码是 redhat ,请勿更改 root 密码。
所有系统上已预装了 SSH 密 钥,允许在不输⼊密码的前提下通过 SSH 进⾏ root 访问。请勿对系 统上的 root SSH 配置文件进⾏ 任何修改。
Ansible 控制节点上已创建了用户 student 。此帐户预装了 SSH 密钥,允许在 Ansible 控制节点 和 各个 Ansible 受管节点之间进行 SSH 登录。请勿对系统上的 student SSH 配置文件进行任何修改。 Ansible 被管理节点上已创建了用户 devops 。用于控制节点连接使用,考试时 ssh 免密和 sudo 提权已 经全部配置好,请勿修改。
初始化虚拟机:
# rht-vmctl all -y
# rht-vmctl classroom -y说明:考试需要通过图形界面对虚拟机进行开机(start),关机(poweroff),重启(reboot)和重置(rebuilt)操 作,重置虚拟机后,虚拟机所有的配置将会清空。
一、安装和配置 ansible

按照下方所述,在控制节点 workstation.lab.example.com 上安装和配置 Ansible:
1.安装所需的软件包
2.创建名为/home/student/ansible/inventory 的静态清单文件, 以满足以下需求:
servera 是 dev 主机组的成员
serverb 是 test 主机组的成员
serverc 和 serverd 是 prod 主机组的成员
bastion 是 balancers 主机组的成员
prod 组是 webservers 主机组的成员
3.创建名为/home/student/ansible/ansible.cfg 的配置文件, 以满足以下要求:
主机清单文件为/home/student/ansible/inventory
playbook 中使用的角色的位置包括/home/student/ansible/roles
准备工作:

# ssh root@workstation
Activate the web console with: systemctl enable --now cockpit.socket

# ssh root@bastion "useradd devops; echo redhat |passwd --stdin
devops"
Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.
Changing password for user devops.
passwd: all authentication tokens updated successfully.

# for i in server{a..d} bastion;do ssh root@$i "echo 'devops
ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devops";done
Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.

# yum -y install ansible #若考试已经安装好了,则不需要安装了
# su - student #考试要求所有的配置都⽤⼀个普通⽤户进⾏配置开始:

$ mkdir ansible
$ cd ansible/
$ vim inventory


servera


serverb


serverc
serverd


bastion


prod

$ cp /etc/ansible/ansible.cfg .
$ vim ansible.cfg

inventory      = /home/student/ansible/inventory        //取消注释并更改路径
roles_path    = /home/student/ansible/roles                        //取消注释并更改路径
remote_user = devops                                                                //取消注释并更改用户

become=True                                                                                        //取消注释即可
become_method=sudo                                                                        //取消注释即可
become_user=root                                                                        //取消注释即可
become_ask_pass=False                                                                //取消注释即可

$ mkdir -p /home/student/ansible/roles
$ ansible all -m ping        //执行后呈现绿色的“pingpong”即代表成功二、创建和运行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
存储库为开启状态
准备工作:

$ for i in server{a..d} bastion; do ssh root@$i "rm -rf/etc/yum.repos.d/*"; done                                //因虚拟机自带 yum 源, 需要先删除, 考试时不需要操作

Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.
Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.开始:

$ vim adhoc.sh

#!/bin/bash

ansible all -m yum_repository -a "name=rh294_BASE description='rh294 base software' file=rhed_dvd gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS/ enabled=yes"

ansible all -m yum_repository -a "name=rh294_STREAM description='rh294 stream software' file=rhed_dvd gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream/ enabled=yes"

$ chmod a+x adhoc.sh
$ ./adhoc.sh三、安装软件包

创建⼀个名为 /home/student/ansible/packages.yml 的 playbook:
1.将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
2.将 Development Tools 软件包组安装到 dev 主机组中的主机上
3.将 dev 主机组中主机上的所有软件包更新为最新版本
开始:

$ vim packages.yml

---
- hosts: dev,test,prod
tasks:
    - name: install mariadb php
      yum:
      name:
          - php
          - mariadb
      state: present
- hosts: dev
tasks:
    - name: install Development Tools
      yum:
      name: "@Development Tools"
      state: present
    - name: update pkgs
      yum:
      name: '*'
      state: latest
      
$ ansible-playbook packages.yml四、使用 RHEL 系统角色

安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/student/ansible/timesync.yml:
1.在所有受管节点上运行
2.使用 timesync 角色
3.配置该角色,以使用当前有效的 NTP 提供
4.配置该角色,以使用时间服务器 classroom.example.com
准备工作:

# ssh root@workstation
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Sep 21 07:51:44 2022 from 172.25.250.250

# yum -y install rhel-system-roles
# su - student
$ cd ansible/
$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync roles/timesync开始:

$ vim timesync.yml

---
- hosts: all
vars:
    timesync_ntp_servers:
      - hostname: classroom.example.com
      iburst: yes
    timesync_ntp_provider: chrony
roles:
    - timesync
post_tasks:
    - name: set timezone
      timezone:
      name: Asia/Shanghai
      notify: restart crond
handlers:
    - name: restart crond
      service:
      name: crond
      state: restarted
      
$ ansible-playbook timesync.yml

//说明:若考试没有要求设置时区,post_tasks 和 handlers 部分可以不⽤配置;如果重新设置了时区,建议重启⼀下
crond 定时器,确保计划任务运⾏的时间是对的。五、使用 RHEL 系统角色

安装 RHEL 系统角色软件包,并使用 SeLinux 角色,要求在所有节点运行,将 SELINUX 设置为强制模式。
准备工作:

$ sudo yum -y install rhel-system-roles
password for student: student
$ ls
adhoc.sh ansible.cfg inventory packages.yml roles timesync.yml

$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux
roles/selinux开始:

$ vim selinux.yml                 //看题目要求的文件名是什么

---
- hosts: all
vars:
    selinux_policy: targeted
    selinux_state: enforcing
roles:
    - role: selinux
      become: true
      
$ ansible-playbook selinux.yml六、使用 Ansible Galaxy 安装角色

使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL
下载角色并安装到 /home/student/ansible/roles:
1.http://classroom.example.com/content/haproxy.tar.gz 此角色的名称应当为 balancer
2.http://classroom.example.com/content/phpinfo.tar.gz 此角色的名称应当为 phpinfo
准备工作:

将 haproxy.tar.gz 和 phpinfo.tar.gz 下载下来放入 foundation0 下面的/content 目录下。
起一个新的终端:
# cd /content/
# wget
http://classroom.example.com/content/ansible2.8/haproxy.tar.gz

# wget
http://classroom.example.com/content/ansible2.8/phpinfo.tar.gz开始:

# ssh workstation
$ cd ansible/
$ ls
adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml

$ vim roles/requirements.yml

- name: balancer
src: http://content.example.com/haproxy.tar.gz
- name: phpinfo
src: http://content.example.com/phpinfo.tar.gz

$ ansible-galaxy install -r roles/requirements.yml -p
roles/七、创建和使用角色

根据下列要求,在 /home/student/ansible/roles 中创建名为 apache 的角色:
1.httpd 软件包已安装,设为在系统启动时启用
2.防⽕墙已启用并正在运行,并使用允许访问 Web 服务器的规则
3.模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html:
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。
4.按照下方所述,创建⼀个使用此角色的 playbook /home/student/ansible/newrole.yml:
该 playbook 在 webservers 主机组中的主机上运行
开始:

$ ls
adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml

$ cd roles/
$ ansible-galaxy init apache
- apache was created successfully

$ vim apache/tasks/main.yml

---
# tasks file for apache
- name: install http
yum:
    name: "{{ item }}"
    state: present
loop:
    - httpd
    - firewalld
- name: system service
service:
    name: "{{ item }}"
    state: started
    enabled: yes
loop:
    - httpd
    - firewalld
- name: firewalld service
firewalld:
    service: http
    zone: public
    permanent: yes
    immediate: yes
    state: enabled
- name: user templates
template:
    src: index.html.j2
    dest: /var/www/html/index.html

$ vim apache/templates/index.html.j2

Welcome to {{ ansible_facts['fqdn'] }} on {{ ansible_facts['default_ipv4']['address'] }}

$ cd ..
$ vim newrole.yml

---
- hosts: webservers
roles:
    - apache
   
$ ansible-playbook newrole.yml

$ curl serverc                        //验证
Welcome to serverc.lab.example.com on 172.25.250.12

$ curl serverd                        //验证
Welcome to serverd.lab.example.com on 172.25.250.13八、从 Ansible Galaxy 使用角色

根据下列要求,创建⼀个名为 /home/student/ansible/roles.yml 的 playbook:
1.playbook 中包含⼀个 play,该 play 在 balancers 主机组中的主机上运⾏并将使用 balancer
角色。
此角色配置⼀项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。
浏览到 balancers 主机组中的主机(例如 http:/bastion.lab.example.com/ )将生成以下输
出:
Welcome to serverc.example.com on 172.25.250.12
重新加载浏览器将从另⼀ Web 服务器生成输出:
Welcome to serverd.example.com on 172.25.250.13
2.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 版本等。
准备工作:

$ ssh root@bastion 'systemctl stop httpd && systemctl
disable httpd'                        //关闭 bastion 主机上的 httpd 服务,以免冲突,考试不需要做
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.开始:

$ vim roles.yml

---
- hosts: webservers
gather_facts: false
tasks:
    - name: test facts
      setup:

- hosts: balancers
roles:
    - balancer
- hosts: webservers
roles:
    - phpinfo

$ ansible-playbook roles.yml

$ curl http://bastion.lab.example.com/                //验证
Welcome to serverc.lab.example.com on 172.25.250.12

$ curl http://bastion.lab.example.com/                //验证
Welcome to serverd.lab.example.com on 172.25.250.13

$ curl http://serverc.lab.example.com/hello.php        //验证
Hello PHP World form serverc.lab.example.com

$ curl http://serverd.lab.example.com/hello.php        //验证
Hello PHP World form serverd.lab.example.com九、 创建和使用逻辑卷

将创建一个名为/home/student/ansible/lv.yml 的 playbook,它将在所有受管节点上运行以执行下
列任务
1.创建符合以下要求的逻辑卷:
逻辑卷创建在 research 卷组中
逻辑卷名称为 data
逻辑卷大小为 1500MiB
2.使用 ext4 文件系统格式化逻辑卷
3.如果无法创建请求的逻辑卷大小,应显示错误消息
Could not create logical volume of that size,并且应改为使用大小 800MiB。
4.如果卷组 research 不存在 ,应显示错误消息
Volume group does not exist。
5.不要以任何方式挂载逻辑卷。
准备工作:

$ vim lvm_pre.yml---- hosts: dev,testtasks:    - name: crteam 2G      parted:      device: /dev/vdb      number: 1      flags: [ lvm ]      state: present      part_start: 1MiB      part_end: 2GiB    - name: create vg      lvg:       vg: research       pvs: /dev/vdb1- hosts: prodtasks:    - name: crteam 1G      parted:      device: /dev/vdb      number: 1      flags: [ lvm ]      state: present      part_start: 1MiB      part_end: 1GiB    - name: create vg      lvg:       vg: research       pvs: /dev/vdb1$ ansible-playbook lvm_pre.yml$ for i in server{a..d};do ssh root@$i 'vgs';done        //验证VG #PV #LV #SN Attr VSize VFree research 1 0 0 wz--n-
页: [1]
查看完整版本: RHCE认证(RedHat8)