翼度科技»论坛 编程开发 .net 查看内容

RHCE认证(RedHat8)

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
考前说明:所有项目运行过程中出现红色字体的报错信息是正常的,运行完成后看 “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 提权已 经全部配置好,请勿修改。
初始化虚拟机:
  1. [root@foundation0 ~]# rht-vmctl all -y
  2. [root@foundation0 ~]# 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
准备工作:
  1. [root@foundation0 ~]# ssh root@workstation
  2. Activate the web console with: systemctl enable --now cockpit.socket
  3. [root@workstation ~]# ssh root@bastion "useradd devops; echo redhat |passwd --stdin
  4. devops"
  5. Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.
  6. Changing password for user devops.
  7. passwd: all authentication tokens updated successfully.
  8. [root@workstation ~]# for i in server{a..d} bastion;do ssh root@$i "echo 'devops
  9. ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devops";done
  10. Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
  11. Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
  12. Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
  13. Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.
  14. [root@workstation ~]# yum -y install ansible #若考试已经安装好了,则不需要安装了
  15. [root@workstation ~]# su - student #考试要求所有的配置都⽤⼀个普通⽤户进⾏配置
复制代码
开始:
  1. [student@workstation ~]$ mkdir ansible
  2. [student@workstation ~]$ cd ansible/
  3. [student@workstation ansible]$ vim inventory
  4. [dev]
  5. servera
  6. [test]
  7. serverb
  8. [prod]
  9. serverc
  10. serverd
  11. [balancers]
  12. bastion
  13. [webservers:children]
  14. prod
  15. [student@workstation ansible]$ cp /etc/ansible/ansible.cfg .
  16. [student@workstation ansible]$ vim ansible.cfg
  17. inventory      = /home/student/ansible/inventory        //取消注释并更改路径
  18. roles_path    = /home/student/ansible/roles                        //取消注释并更改路径
  19. remote_user = devops                                                                //取消注释并更改用户
  20. [privilege_escalation]
  21. become=True                                                                                        //取消注释即可
  22. become_method=sudo                                                                        //取消注释即可
  23. become_user=root                                                                        //取消注释即可
  24. become_ask_pass=False                                                                //取消注释即可
  25. [student@workstation ansible]$ mkdir -p /home/student/ansible/roles
  26. [student@workstation ansible]$ ansible all -m ping        //执行后呈现绿色的“ping  pong”即代表成功
复制代码
二、创建和运行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
存储库为开启状态
准备工作:
  1. [student@workstation ansible]$ for i in server{a..d} bastion; do ssh root@$i "rm -rf/etc/yum.repos.d/*"; done                                //因虚拟机自带 yum 源, 需要先删除, 考试时不需要操作
  2. Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
  3. Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
  4. Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
  5. Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.
  6. Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.
复制代码
开始:
  1. [student@workstation ansible]$ vim adhoc.sh
  2. #!/bin/bash
  3. 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"
  4. 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"
  5. [student@workstation ansible]$ chmod a+x adhoc.sh
  6. [student@workstation ansible]$ ./adhoc.sh
复制代码
三、安装软件包

创建⼀个名为 /home/student/ansible/packages.yml 的 playbook:
1.将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
2.将 Development Tools 软件包组安装到 dev 主机组中的主机上
3.将 dev 主机组中主机上的所有软件包更新为最新版本
开始:
  1. [student@workstation ansible]$ vim packages.yml
  2. ---
  3. - hosts: dev,test,prod
  4.   tasks:
  5.     - name: install mariadb php
  6.       yum:
  7.         name:
  8.           - php
  9.           - mariadb
  10.         state: present
  11. - hosts: dev
  12.   tasks:
  13.     - name: install Development Tools
  14.       yum:
  15.         name: "@Development Tools"
  16.         state: present
  17.     - name: update pkgs
  18.       yum:
  19.         name: '*'
  20.         state: latest
  21.         
  22. [student@workstation ansible]$ ansible-playbook packages.yml
复制代码
四、使用 RHEL 系统角色

安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/student/ansible/timesync.yml:
1.在所有受管节点上运行
2.使用 timesync 角色
3.配置该角色,以使用当前有效的 NTP 提供
4.配置该角色,以使用时间服务器 classroom.example.com
准备工作:
  1. [root@foundation0 ~]# ssh root@workstation
  2. Activate the web console with: systemctl enable --now cockpit.socket
  3. Last login: Wed Sep 21 07:51:44 2022 from 172.25.250.250
  4. [root@workstation ~]# yum -y install rhel-system-roles
  5. [root@workstation ~]# su - student
  6. [student@workstation ~]$ cd ansible/
  7. [student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync roles/timesync
复制代码
开始:
  1. [student@workstation ansible]$ vim timesync.yml
  2. ---
  3. - hosts: all
  4.   vars:
  5.     timesync_ntp_servers:
  6.       - hostname: classroom.example.com
  7.         iburst: yes
  8.     timesync_ntp_provider: chrony
  9.   roles:
  10.     - timesync
  11.   post_tasks:
  12.     - name: set timezone
  13.       timezone:
  14.         name: Asia/Shanghai
  15.       notify: restart crond
  16.   handlers:
  17.     - name: restart crond
  18.       service:
  19.         name: crond
  20.         state: restarted
  21.         
  22. [student@workstation ansible]$ ansible-playbook timesync.yml
  23. //说明:若考试没有要求设置时区,post_tasks 和 handlers 部分可以不⽤配置;如果重新设置了时区,建议重启⼀下
  24. crond 定时器,确保计划任务运⾏的时间是对的。
复制代码
五、使用 RHEL 系统角色

安装 RHEL 系统角色软件包,并使用 SeLinux 角色,要求在所有节点运行,将 SELINUX 设置为强制模式。
准备工作:
  1. [student@workstation ansible]$ sudo yum -y install rhel-system-roles
  2. [sudo] password for student: student
  3. [student@workstation ansible]$ ls
  4. adhoc.sh ansible.cfg inventory packages.yml roles timesync.yml
  5. [student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux
  6. roles/selinux
复制代码
开始:
  1. [student@workstation ansible]$ vim selinux.yml                 //看题目要求的文件名是什么
  2. ---
  3. - hosts: all
  4.   vars:
  5.     selinux_policy: targeted
  6.     selinux_state: enforcing
  7.   roles:
  8.     - role: selinux
  9.       become: true
  10.       
  11. [student@workstation ansible]$ 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
准备工作:
  1. 将 haproxy.tar.gz 和 phpinfo.tar.gz 下载下来放入 foundation0 下面的/content 目录下。
  2. 起一个新的终端:
  3. [root@foundation0 ~]# cd /content/
  4. [root@foundation0 content]# wget
  5. http://classroom.example.com/content/ansible2.8/haproxy.tar.gz
  6. [root@foundation0 content]# wget
  7. http://classroom.example.com/content/ansible2.8/phpinfo.tar.gz
复制代码
开始:
  1. [root@foundation0 content]# ssh workstation
  2. [student@workstation ~]$ cd ansible/
  3. [student@workstation ansible]$ ls
  4. adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml
  5. [student@workstation ansible]$ vim roles/requirements.yml
  6. - name: balancer
  7.   src: http://content.example.com/haproxy.tar.gz
  8. - name: phpinfo
  9.   src: http://content.example.com/phpinfo.tar.gz
  10.   
  11. [student@workstation ansible]$ ansible-galaxy install -r roles/requirements.yml -p
  12. 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 主机组中的主机上运行
开始:
  1. [student@workstation ansible]$ ls
  2. adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml
  3. [student@workstation ansible]$ cd roles/
  4. [student@workstation roles]$ ansible-galaxy init apache
  5. - apache was created successfully
  6. [student@workstation roles]$ vim apache/tasks/main.yml
  7. ---
  8. # tasks file for apache
  9. - name: install http
  10.   yum:
  11.     name: "{{ item }}"
  12.     state: present
  13.   loop:
  14.     - httpd
  15.     - firewalld
  16. - name: system service
  17.   service:
  18.     name: "{{ item }}"
  19.     state: started
  20.     enabled: yes
  21.   loop:
  22.     - httpd
  23.     - firewalld
  24. - name: firewalld service
  25.   firewalld:
  26.     service: http
  27.     zone: public
  28.     permanent: yes
  29.     immediate: yes
  30.     state: enabled
  31. - name: user templates
  32.   template:
  33.     src: index.html.j2
  34.     dest: /var/www/html/index.html
  35. [student@workstation roles]$ vim apache/templates/index.html.j2
  36. Welcome to {{ ansible_facts['fqdn'] }} on {{ ansible_facts['default_ipv4']['address'] }}
  37. [student@workstation roles]$ cd ..
  38. [student@workstation ansible]$ vim newrole.yml
  39. ---
  40. - hosts: webservers
  41.   roles:
  42.     - apache
  43.    
  44. [student@workstation ansible]$ ansible-playbook newrole.yml
  45. [student@workstation ansible]$ curl serverc                        //验证
  46. Welcome to serverc.lab.example.com on 172.25.250.12
  47. [student@workstation ansible]$ curl serverd                        //验证
  48. 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 版本等。
准备工作:
  1. [student@workstation ansible]$ ssh root@bastion 'systemctl stop httpd && systemctl
  2. disable httpd'                        //关闭 bastion 主机上的 httpd 服务,以免冲突,考试不需要做
  3. Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
复制代码
开始:
  1. [student@workstation ansible]$ vim roles.yml
  2. ---
  3. - hosts: webservers
  4.   gather_facts: false
  5.   tasks:
  6.     - name: test facts
  7.       setup:
  8. - hosts: balancers
  9.   roles:
  10.     - balancer
  11. - hosts: webservers
  12.   roles:
  13.     - phpinfo
  14. [student@workstation ansible]$ ansible-playbook roles.yml
  15. [student@workstation ansible]$ curl http://bastion.lab.example.com/                //验证
  16. Welcome to serverc.lab.example.com on 172.25.250.12
  17. [student@workstation ansible]$ curl http://bastion.lab.example.com/                //验证
  18. Welcome to serverd.lab.example.com on 172.25.250.13
  19. [student@workstation ansible]$ curl http://serverc.lab.example.com/hello.php        //验证
  20. Hello PHP World form serverc.lab.example.com
  21. [student@workstation ansible]$ curl http://serverd.lab.example.com/hello.php        //验证
  22. 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.不要以任何方式挂载逻辑卷。
准备工作:

[code][student@workstation ansible]$ vim lvm_pre.yml---- hosts: dev,test  tasks:    - 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: prod  tasks:    - 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[student@workstation ansible]$ ansible-playbook lvm_pre.yml[student@workstation ansible]$ 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-

举报 回复 使用道具