红帽 RHCE 8版本最后一个月冲一波证书

系列文章目录



前言

本文记录一下RHCE8改版最后一个月冲一波证书,哈哈

RHCE模拟练习题目,请使用教室环境RH294:

在练习期间,您将操作下列虚拟系统:

systemIP AddressRole
workstation.lab.example.com172.25.250.9Ansible control node
servera.lab.example.com172.25.250.10Ansible managed node
serverb.lab.example.com172.25.250.11Ansible managed node
serverc.lab.example.com172.25.250.12Ansible managed node
serverd.lab.example.com172.25.250.13Ansible managed node
bastion.lab.example.com172.25.250.254Ansible managed node

这些系统的 IP 地址采用静态设置,主机名称解析已配置为解析上方列出的主机名。 请勿更改这些 设置。

帐户信息

宿主机的root密码为Asimov,宿主机上其他用户的密码均为redhat

宿主机里面所有虚拟系统的 root 密码是 redhat,请勿更改 root 密码。所有系统上已预装了 SSH 密钥, 允许在不输入密码的前提下通过 SSH 进行 root 访问。请勿对系统上的 root SSH 配置文件进行任何修改。

Ansible 控制节点上已创建了用户 student 。此帐户预装了SSH密钥,允许在 Ansible 控制节点和各 个 Ansible 受管节点之间进行SSH登录。请勿对系统上的 student SSH 配置文件进行任何修改。

Ansible 被管理节点上已创建了用户devops。用于控制节点连接使用,考试时ssh免密和sudo提权已经全部配置好,请勿修改。

说明:考试需要通过图形界面对虚拟机进行开机(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的配置文件, 以满足以下要求:
    1. 主机清单文件为/home/student/ansible/inventory
    2. playbook中使用的角色的位置包括/home/student/ansible/roles
  • 答案
# ssh到ansible控制节点,后续所有操作均使用student用户在ansible控制节点进行
[kiosk@foundation0 ~]$ ssh student@workstation

# 安装ansible
[student@workstation ~]$ sudo yum -y install ansible

# 创建ansible目录连同roles目录一起创建出来,后面会用,后面所有的操作都在ansible目录下执行
[student@workstation ~]$ mkdir -p /home/student/ansible/roles
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ pwd
/home/student/ansible

# 创建inventory清单目录文件
[student@workstation ansible]$ vim /home/student/ansible/inventory
[student@workstation ansible]$ cat /home/student/ansible/inventory
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
bastion
[webservers:children]
prod

# 创建ansible配置文件
[student@workstation ansible]$ vim /home/student/ansible/ansible.cfg
[student@workstation ansible]$ cat /home/student/ansible/ansible.cfg
[defaults]
inventory      = /home/student/ansible/inventory
ask_pass      = False
roles_path    = /home/student/ansible/roles
host_key_checking = False
remote_user = devops
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=false

# 验证结果
[student@workstation ansible]$ pwd
/home/student/ansible
[student@workstation ansible]$ ansible --version
[student@workstation ansible]$ ansible all -m ping
[student@workstation ansible]$ ansible all --list-hosts
[student@workstation ansible]$ ansible-inventory --graph

二、创建和运行Ansible临时命令

  • 题目

作为系统管理员, 您需要在受管节点上安装软件.
请按照下方所述, 创建一个名为/home/student/ansible/adhoc.sh的shell脚本, 该脚本将使用Ansible临时命令在各个受管节点上安装yum存储库:

  1. 存储库1:
    1. 存储库的名称为 rh294_BASE
    2. 描述为 rh294 base software
    3. 基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
    4. GPG签名检查为启用状态
    5. GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
    6. 存储库为开启状态
  2. 存储库2:
    1. 存储库的名称为 rh294_STREAM
    2. 描述为 rh294 stream software
    3. 基础URL为 http://content.example.com/rhel8.0/x86_64/dvd/AppStream
    4. GPG签名检查为启用状态
    5. GPG密钥URL为 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
    6. 存储库为开启状态
  • 答案
# 根据题意编写配置yum仓库的脚本
[student@workstation ansible]$ vim /home/student/ansible/adhoc.sh
[student@workstation ansible]$ cat /home/student/ansible/adhoc.sh
#!/bin/bash

ansible all -m yum_repository -a "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 yum_repository -a "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 /home/student/ansible/adhoc.sh
[student@workstation ansible]$ /home/student/ansible/adhoc.sh

# 验证结果,查看仓库是否配置正确
[student@workstation ansible]$ ansible all -m shell -a "ls /etc/yum.repos.d"
[student@workstation ansible]$ ansible all -m shell -a "yum clean all"
[student@workstation ansible]$ ansible all -m shell -a "yum makecache"
[student@workstation ansible]$ ansible all -m shell -a "yum repolist all"

三、安装软件包

  • 题目

创建一个名为 /home/student/ansible/packages.yml 的 playbook:

  1. 将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上
  2. 将 RPM Development Tools 软件包组安装到 dev主机组中的主机上
  3. 将 dev 主机组中主机上的所有软件包更新为最新版本
  • 答案
# 根据题意编写playbook
[student@workstation ansible]$ vim /home/student/ansible/packages.yml
[student@workstation ansible]$ cat /home/student/ansible/packages.yml
---
- name: Install a list of packages
  hosts: dev,test,prod
  tasks:
    - name: install php and mariadb
      yum:
        name:
          - php
          - mariadb
        state: present

- name: install package group
  hosts: dev
  tasks:
    - name: install "@RPM Development Tools"
      yum:
        name: "@RPM Development Tools"
        state: present

- name: upgrade all packages
  hosts: dev
  tasks:
    - name: upgrade all packages
      yum:
        name: '*'
        state: latest

# 检查playbook语法,并执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/packages.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/packages.yml

# 验证结果,查询软件包是否已安装
[student@workstation ansible]$ ansible all -m shell -a "rpm -qa | grep php"
[student@workstation ansible]$ ansible all -m shell -a "rpm -qa | grep mariadb"

四、使用RHEL系统角色

第一问:配置时间同步
  • 题目

安装 RHEL 系统角色软件包,并创建符合以下条件的playbook:

/home/student/ansible/timesync.yml

  1. 在所有受管节点上运行
  2. 使用 timesync 角色
  3. 配置该角色,以使用当前有效的 NTP 提供商
  4. 配置该角色,以使用时间服务器 classroom.example.com
  5. 配置该角色,以启用 iburst 参数
  • 答案
# 查询系统角色软件包名,并安装
[student@workstation ansible]$ yum list | grep role
[student@workstation ansible]$ sudo yum -y install rhel-system-roles

# 将系统自带角色timesync的roles目录及yml文件直接拷贝过来使用
[student@workstation ansible]$ rpm -ql rhel-system-roles
[student@workstation ansible]$ cp -a /usr/share/ansible/roles/rhel-system-roles.timesync /home/student/ansible/roles/timesync
[student@workstation ansible]$ cp -a /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml /home/student/ansible/timesync.yml

# 角色目录拷贝过来之后,查询是否已有timesync这个角色
[student@workstation ansible]$ ansible-galaxy list

# 根据题意在拷贝过来的yml文件内容基础上修改playbook
[student@workstation ansible]$ vim /home/student/ansible/timesync.yml
[student@workstation ansible]$ cat /home/student/ansible/timesync.yml
---
- hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: classroom.example.com
        iburst: yes
  roles:
    - timesync

# 检查playbook语法无误,并执行
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/timesync.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/timesync.yml

# 验证结果,查看时间是否已同步,及相关服务是不是开机自启动状态
[student@workstation ansible]$ ansible all -m shell -a "chronyc sources -v"
[student@workstation ansible]$ ansible all -m shell -a "systemctl status chronyd"
第二问:配置selinux
  • 题目

安装 RHEL 系统角色软件包,并创建符合以下条件的playbook:
/home/student/ansible/selinux.yml

  1. 在所有受管节点上运行
  2. 使用selinux角色
  3. 将该角色配置为以强制执行状态使用SELinux
  4. 配置selinux,允许http监听 82 端口
  5. 配置selinux,允许http访问 /var/www/html
  • 答案
# 查询系统角色软件包名,并安装
[student@workstation ansible]$ yum list | grep role
[student@workstation ansible]$ sudo yum -y install rhel-system-roles

# 将系统自带角色selinux的roles目录及yml文件直接拷贝过来使用
[student@workstation ansible]$ rpm -ql rhel-system-roles
[student@workstation ansible]$ cp -a /usr/share/ansible/roles/rhel-system-roles.selinux /home/student/ansible/roles/selinux
[student@workstation ansible]$ cp -a /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml /home/student/ansible/selinux.yml

# 角色目录拷贝过来之后,查询是否已有selinux这个角色
[student@workstation ansible]$ ansible-galaxy list

# 根据题意在拷贝过来的yml文件内容基础上修改playbook
[student@workstation ansible]$ vim /home/student/ansible/selinux.yml
[student@workstation ansible]$ cat /home/student/ansible/selinux.yml
---
- hosts: all
  vars:
    selinux_policy: targeted
    selinux_state: enforcing
    selinux_fcontexts:
      - { target: '/var/www/html(/.*)?', setype: 'httpd_sys_content_t', ftype: 'd' }
    selinux_restore_dirs:
      - /var/www/html
    selinux_ports:
      - { ports: '82', proto: 'tcp', setype: 'http_port_t', state: 'present' }
  roles:
    - selinux

# 检查playbook语法无误,并执行
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/selinux.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/selinux.yml


# 验证结果,查看是否已放开82端口,及/var/www/html目录下的文件的上下文环境
[student@workstation ansible]$ ansible all -m shell -a "getenforce"
[student@workstation ansible]$ ansible all -m shell -a "semanage port -l | grep http"
[student@workstation ansible]$ ansible all -m shell -a "ls -lZ /var/www/html"
[student@workstation ansible]$ ansible all -m shell -a "ls -lZ /var/www/html -d"

五、使用Ansible Galaxy安装角色

  • 题目

使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,从以下 URL 下载角色并安装到 /home/student/ansible/roles

  1. http://content.example.com/haproxy.tar.gz 此角色的名称应当为 balancer
  2. http://content.example.com/phpinfo.tar.gz 此角色的名称应当为 phpinfo
  • 答案
# 根据题意编写yml文件
[student@workstation ansible]$ vim /home/student/ansible/roles/requirements.yml
[student@workstation ansible]$ cat /home/student/ansible/roles/requirements.yml
---
- name: balancer
  src: http://content.example.com/haproxy.tar.gz
- name: phpinfo
  src: http://content.example.com/phpinfo.tar.gz

# 执行安装角色命令
[student@workstation ansible]$ ansible-galaxy install -r /home/student/ansible/roles/requirements.yml -p /home/student/ansible/roles

# 验证结果,查看这两个角色是否已安装
[student@workstation ansible]$ ansible-galaxy list
[student@workstation ansible]$ ls /home/student/ansible/roles

六、创建和使用角色

  • 题目

根据下列要求,在 /home/student/ansible/roles中创建名为 apache 的角色:

  1. httpd软件包已安装,设为在系统启动时启用
  2. 防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则
  3. 模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html
    1. Welcome to HOSTNAME on IPADDRESS
    2. 其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。
  4. 按照下方所述,创建一个使用此角色的 playbook:/home/student/ansible/newrole.yml
    1. 该 playbook 在 webservers 主机组中的主机上运行
  • 答案
# 进入roles目录初始化apache角色
[student@workstation ansible]$ cd /home/student/ansible/roles
[student@workstation roles]$ ansible-galaxy init apache

# 创建好apache角色之后,马上回到/home/student/ansible目录,并查看是否已有apache这个角色
[student@workstation roles]$ cd ..
[student@workstation ansible]$ pwd
[student@workstation ansible]$ ansible-galaxy list

# 编写apache的主任务文件main.yml
[student@workstation ansible]$ vim roles/apache/tasks/main.yml
[student@workstation ansible]$ cat roles/apache/tasks/main.yml
---
# tasks file for apache
- name: install httpd
  yum:
    name: httpd
    state: present

- name: install firewalld
  yum:
    name: firewalld
    state: present

- name: start and enable httpd
  service:
    name: httpd
    state: started
    enabled: yes

- name: start and enable firewalld
  service:
    name: firewalld
    state: started
    enabled: yes

- name: configure firewalld to permit http
  firewalld:
    zone: public
    service: http
    permanent: yes
    immediate: yes
    state: enabled

- name: Template a file to /var/www/html/index.html
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

# 编写apache的模板文件index.html.j2
[student@workstation ansible]$ vim roles/apache/templates/index.html.j2
[student@workstation ansible]$ cat roles/apache/templates/index.html.j2
Welcome to {{ ansible_facts.fqdn }} on {{ ansible_facts.default_ipv4.address }}

# 根据题意编写playbook,在webservers主机组使用apache这个角色
[student@workstation ansible]$ vim /home/student/ansible/newrole.yml
[student@workstation ansible]$ cat /home/student/ansible/newrole.yml
---
- name: use role apache
  hosts: webservers
  roles:
    - apache

# 检查playbook语法无误,并执行
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/newrole.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/newrole.yml

# 验证结果,查看apache的模板文件是否写入到webservers主机组下主机的/var/www/html/index.html文件中
[student@workstation ansible]$ curl serverc
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ 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 角色。
  2. 此角色配置一项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。
  3. 浏览到 balancers 主机组中的主机(例如http://bastion.lab.example.com/ )将生成以下输出:
    1. Welcome to serverc.lab.example.com on 172.25.250.12
  4. 重新加载浏览器将从另一 Web 服务器生成输出:
    1. Welcome to serverd.lab.example.com on 172.25.250.13

  1. playbook 中包含一个 play,该 play 在 webservers主机组中的主机上运行并将使用 phpinfo 角色。
  2. 通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
    1. Hello PHP World from FQDN,其中,FQDN是主机的完全限定名称。
  3. 例如,浏览到 http://serverc.lab.example.com/hello.php 会生成以下输出:
    1. Hello PHP World from serverc.lab.example.com
    2. 另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
  4. 同样,浏览到 http://serverd.lab.example.com/hello.php 会生成以下输出:
    1. Hello PHP World from serverd.lab.example.com
    2. 另外还有 PHP 配置的各种详细信息,如安装的PHP 版本等。
  • 答案
# 根据题意编写playbook,对指定主机组使用指定角色,完成相应功能
[student@workstation ansible]$ vim /home/student/ansible/roles.yml
[student@workstation ansible]$ cat /home/student/ansible/roles.yml
---
- name: use role apache
  hosts: webservers
  roles:
    - apache

- name: use role balancer
  hosts: balancers
  roles:
    - balancer

- name: use role phpinfo
  hosts: webservers
  roles:
    - phpinfo

# 检查playbook语法无误,并执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/roles.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/roles.yml

# 验证答案,curl拉取内容看是否如预期所示
[student@workstation ansible]$ curl bastion
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ curl bastion
Welcome to serverd.lab.example.com on 172.25.250.13
[student@workstation ansible]$ curl serverc
Welcome to serverc.lab.example.com on 172.25.250.12
[student@workstation ansible]$ curl serverd
Welcome to serverd.lab.example.com on 172.25.250.13
[student@workstation ansible]$ curl serverc/hello.php
Hello PHP World form serverc.lab.example.com
[student@workstation ansible]$ curl serverd/hello.php
Hello PHP World form serverd.lab.example.com

八、创建、格式化、挂载磁盘分区

第一问:创建和使用逻辑卷
  • 题目

创建一个名为 /home/student/ansible/lv.yml 的playbook,它将在所有受管节点上运行以执行下列任务:

  1. 创建符合以下要求的逻辑卷:
    1. 逻辑卷创建在 research 卷组中
    2. 逻辑卷名称为 data
    3. 逻辑卷大小为 1500MiB
  2. 使用 ext4 文件系统格式化逻辑卷
  3. 如果无法创建请求的逻辑卷大小,应显示错误消息
    1. Could not create logical volume of that size
    2. 并且应改为使用大小 800MiB
  4. 如果卷组research 不存在 ,应显示错误消息
    1. Volume group does not exist
  5. 不要以任何方式挂载逻辑卷。
  • 答案
# 根据题意编写playbook剧本
[student@workstation ansible]$ vim /home/student/ansible/lv.yml
[student@workstation ansible]$ cat /home/student/ansible/lv.yml
---
- name: create lvm
  hosts: all
  tasks:
    - name: create 1500m or 800m
      block:
        - name: create 1500m
          lvol:
            vg: research
            lv: data
            size: 1500m
      rescue:
        - name: output error msg
          debug:
            msg: Could not create logical volume of that size
        - name: create 800m
          lvol:
            vg: research
            lv: data
            size: 800m
      always:
        - name: make filesystem
          filesystem:
            fstype: ext4
            dev: /dev/research/data
      when: "'research' in ansible_facts.lvm.vgs"

    - name: vg is not exist
      debug:
        msg: Volume group does not exist
      when: "'research' not in ansible_facts.lvm.vgs"

# 检查playbook语法无误,然后执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/lv.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/lv.yml

# 验证结果,检查lvm分区是否如预期创建
[student@workstation ansible]$ ansible all -m shell -a "lsblk"
[student@workstation ansible]$ ansible all -m shell -a "lsblk -f"
[student@workstation ansible]$ ansible all -m shell -a "vgs"
[student@workstation ansible]$ ansible all -m shell -a "lvs"
第二问:创建和使用普通分区
  • 题目

创建一个名为 /home/student/ansible/partition.yml 的剧本,它将在所有受管节点上运行以执行下列任务:

  1. 在设备vdb上创建编号为1大小为1500MiB的单一主分区
  2. 使用 ext4 文件系统格式化分区
  3. 持久地挂载文件系统在 /newpart
  4. 如果无法创建请求的分区大小,则应显示错误消息
    1. Could not create partition of that size
    2. 并且应改为使用大小 800MiB
  5. 如果设备vdb不存在则应显示错误消息
    1. Disk does not exist
  • 答案
# 根据题意编写playbook剧本
[student@workstation ansible]$ vim /home/student/ansible/partition.yml
[student@workstation ansible]$ cat /home/student/ansible/partition.yml
---
- name: create partition
  hosts: all
  tasks:
    - name: create partition 1500MiB or 800MiB
      block:
        - name: Create a new primary partition with a size of 1500MiB
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 1500MiB
      rescue:
        - name: output error msg
          debug:
            msg: Could not create partition of that size
        - name: Create a new primary partition with a size of 800MiB
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 800MiB
      always:
        - name: make filesystem
          filesystem:
            fstype: ext4
            dev: /dev/vdb1
            force: true
        - name: mkdir /newpart
          file:
            path: /newpart
            state: directory
        - name: mount /dev/vdb1
          mount:
            path: /newpart
            src: /dev/vdb1
            fstype: ext4
            state: mounted
      when: "ansible_facts.devices.vdb is defined"

    - name: vdb does not exist and output error msg
      debug:
        msg: Disk does not exist
      when: "ansible_facts.devices.vdb is not defined"

# 检查playbook语法无误,然后执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/partition.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/partition.yml

# 验证结果,检查分区是否如预期创建,有没有自动挂载
[student@workstation ansible]$ ansible all -m shell -a "lsblk"
[student@workstation ansible]$ ansible all -m shell -a "lsblk -f"
[student@workstation ansible]$ ansible all -m shell -a "df -h"

九、生成主机文件

  • 题目

将一个初始模板文件从 http://content.example.com/hosts.j2 下载到 /home/student/ansible
完成该模板,以便用它生成以下文件:

  1. 针对每个清单主机包含一行内容,其格式与 /etc/hosts 相同
  2. 创建名为 /home/student/ansible/hosts.yml 的playbook,它将使用此模板在 dev 主机组中的主机上生成文件 /etc/myhosts。
  3. 该 playbook 运行后,dev 主机组中主机上的文件/etc/myhosts 应针对每个受管主机包含一行内容:

        127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
        ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

        172.25.250.10 servera.lab.example.com servera
        172.25.250.11 serverb.lab.example.com serverb
        172.25.252.250 bastion.lab.example.com bastion
        172.25.250.12 serverc.lab.example.com serverc
        172.25.250.13 serverd.lab.example.com serverd

注:清单主机名称的显示顺序不重要。

  • 答案
# 下载并根据题意编写模板文件hosts.j2
[student@workstation ansible]$ wget http://content.example.com/hosts.j2
[student@workstation ansible]$ vim /home/student/ansible/hosts.j2
[student@workstation ansible]$ cat /home/student/ansible/hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all %}
{{ hostvars[host].ansible_facts.default_ipv4.address }} {{ hostvars[host].ansible_facts.fqdn }} {{ hostvars[host].ansible_facts.hostname }}
{% endfor %}

# 编写playbook,通过上述模板生成一个包含特定内容的文件
[student@workstation ansible]$ vim /home/student/ansible/hosts.yml
[student@workstation ansible]$ cat /home/student/ansible/hosts.yml
---
- name: gather facts all
  hosts: all

- name: generate host file
  hosts: dev
  tasks:
    - name: Template a file to /etc/myhosts
      template:
        src: /home/student/ansible/hosts.j2
        dest: /etc/myhosts

# 检查playbook语法无误,然后执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/hosts.yml
[student@workstation ansible]$ ansible-playbook  /home/student/ansible/hosts.yml

# 验证答案,查看是否生成了/etc/myhosts文件,文件内容是否符合题目要求
[student@workstation ansible]$ ansible dev -m shell -a "cat /etc/myhosts"
[student@workstation ansible]$ ssh servera cat /etc/myhosts

十、修改文件内容

  • 题目

按照下方所述,创建一个名为 /home/student/ansible/issue.yml 的 playbook:

  1. 该 playbook 将在所有清单主机上运行
  2. 该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:
    1. 在 dev 主机组中的主机上,这行文本显示为:Development
    2. 在 test 主机组中的主机上,这行文本显示为:Test
    3. 在 prod 主机组中的主机上,这行文本显示为:Production
  • 答案
# 根据题目要求编写playbook文件
[student@workstation ansible]$ vim /home/student/ansible/issue.yml
[student@workstation ansible]$ cat /home/student/ansible/issue.yml
---
- name: modify file content
  hosts: all
  tasks:
    - name: copy content to a file
      copy:
        content: |
          {% if 'dev' in group_names %}
          Development
          {% elif 'test' in group_names %}
          Test
          {% elif 'prod' in group_names %}
          Production
          {% endif %}
        dest: /etc/issue

# 检查playbook语法无误,然后执行
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/issue.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/issue.yml

# 验证答案,查看/etc/issue文件是否如题目要求写入了相应内容
[student@workstation ansible]$ ansible all -m shell -a "cat /etc/issue"

十一、创建web内容目录

  • 题目

按照下方所述,创建一个名为 /home/student/ansible/webcontent.yml 的 playbook:

  1. 该 playbook 在 dev 主机组中的受管节点上运行
  2. 创建符合下列要求的目录 /webdev:
    1. 所有者为 devops 组
    2. 具有常规权限owner=read+write+execute,group=read+write+execute,other=read+execute
    3. 具有特殊权限:set group ID
  3. 用符号链接将 /var/www/html/webdev 链接到 /webdev
  4. 创建文件 /webdev/index.html,其中包含如下所示的单行文本:Development
  5. 在 dev 主机组中主机上浏览此目录(例如 http://servera.lab.example.com/webdev/ 将生成以下输出:
    1. Development
  • 答案
# 根据题意编写playbook,除题目中要求的几点外,还需配置http和防火墙放行等,可复制apache角色main.yml文件里面部分配置过来使用
[student@workstation ansible]$ vim /home/student/ansible/webcontent.yml
[student@workstation ansible]$ cat /home/student/ansible/webcontent.yml
---
- name: create web content directory
  hosts: dev
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: install firewalld
      yum:
        name: firewalld
        state: present

    - name: start and enable httpd
      service:
        name: httpd
        state: started
        enabled: yes

    - name: start and enable firewalld
      service:
        name: firewalld
        state: started
        enabled: yes

    - name: configure firewalld to permit http
      firewalld:
        zone: public
        service: http
        permanent: yes
        immediate: yes
        state: enabled

    - name: create directory /webdev
      file:
        path: /webdev
        state: directory
        group: devops
        mode: '2775'
        setype: httpd_sys_content_t

    - name: Create a symbolic link
      file:
        src: /webdev
        dest: /var/www/html/webdev
        state: link

    - name: copy content to a file
      copy:
        content: "Development"
        dest: /webdev/index.html
        setype: httpd_sys_content_t

# 检查playbook语法无误,然后执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/webcontent.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/webcontent.yml

# 验证答案,curl看是否能拉取到相关内容
[student@workstation ansible]$ curl http://servera.lab.example.com/webdev/

十二、生成硬件报告

  • 题目

创建一个名为 /home/student/ansible/hwreport.yml 的 playbook,它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt:

  1. 清单主机名称
  2. 以 MB 表示的总内存大小
  3. BIOS 版本
  4. 磁盘设备 vda 的大小
  5. 磁盘设备 vdb 的大小

输出文件中的每一行含有一个 key=value 对。

您的 playbook 应当:

  1. http://172.25.254.254/content/hwreport.empty 下载文件,并将它保存为/root/hwreport.txt
  2. 使用正确的值修改 /root/hwreport.txt
  3. 如果硬件项不存在,相关的值应设为 NONE
  • 答案
# 先curl查看一下这个文件的内容,清楚自己后面要替换哪部分的值
[student@workstation ansible]$ curl http://172.25.254.254/content/hwreport.empty
HOSTNAME='inventoryhostname'
TOTAL_MEM_MB='memory_in_mb'
BIOS_VERSION='BIOS_version'
DISK_VDA_SIZE='disk_vda_size'
DISK_VDB_SIZE='disk_vdb_size'

# 编写playbook文件,下载hwreport.txt并根据题目要求修改里面的内容
[student@workstation ansible]$ vim /home/student/ansible/hwreport.yml
[student@workstation ansible]$ cat /home/student/ansible/hwreport.yml
---
- name: generate hwreport
  hosts: all
  tasks:
    - name: get_url http://172.25.254.254/content/hwreport.empty
      get_url:
        url: http://172.25.254.254/content/hwreport.empty
        dest: /root/hwreport.txt

    - name: replace inventoryhostname
      replace:
        path: /root/hwreport.txt
        regexp: "inventoryhostname"
        replace: "{{ ansible_facts.hostname }}"

    - name: replace memory_in_mb
      replace:
        path: /root/hwreport.txt
        regexp: "memory_in_mb"
        replace: "{{ ansible_memtotal_mb | string }}"

    - name: replace BIOS_version
      replace:
        path: /root/hwreport.txt
        regexp: "BIOS_version"
        replace: "{{ ansible_bios_version }}"

    - name: replace disk_vda_size
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vda_size"
        replace: "{{ ansible_facts.devices.vda.size | default('NONE') }}"

    - name: replace disk_vdb_size
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vdb_size"
        replace: "{{ ansible_facts.devices.vdb.size | default('NONE') }}"

# 检查playbook语法无误,然后执行playbook
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/hwreport.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/hwreport.yml

# 验证结果,查看是否每台主机都有/root/hwreport.txt这个文件,且按题目要求替换了里面的内容
[student@workstation ansible]$ ansible all -m shell -a "cat /root/hwreport.txt"

十三、创建密码库

  • 题目

按照下方所述,创建一个 Ansible 库来存储用户密码:

  1. 库名称为 /home/student/ansible/locker.yml
  2. 库中含有两个变量,名称如下:
    1. pw_developer,值为 Imadev
    2. pw_manager,值为 Imamgr
  3. 用于加密和解密该库的密码为whenyouwishuponastar
  4. 密码存储在文件 /home/student/ansible/secret.txt 中
  • 答案
# 编写yml文件
[student@workstation ansible]$ vim /home/student/ansible/locker.yml
[student@workstation ansible]$ cat /home/student/ansible/locker.yml
pw_developer: Imadev
pw_manager: Imamgr

# 将密码保存至secret.txt文件
[student@workstation ansible]$ echo  whenyouwishuponastar > /home/student/ansible/secret.txt
[student@workstation ansible]$ cat /home/student/ansible/secret.txt
whenyouwishuponastar

# 使用secret.txt文件对locker.yml文件进行加密
[student@workstation ansible]$ ansible-vault --help
[student@workstation ansible]$ ansible-vault encrypt --vault-password-file=/home/student/ansible/secret.txt /home/student/ansible/locker.yml

# 验证答案,查看加密了的locker.yml文件
[student@workstation ansible]$ ansible-vault view  /home/student/ansible/locker.yml
[student@workstation ansible]$ ansible-vault view --vault-password-file=/home/student/ansible/secret.txt /home/student/ansible/locker.yml

十四、创建用户账户

  • 题目
  1. http://172.25.254.254/content/user_list.yml 下载要创建的用户的列表,并将它保存到 /home/student/ansible,用户密码来自于 /home/student/ansible/locker.yml 文件。
  2. 创建名为 /home/student/ansible/users.yml 的playbook,从而按以下所述创建用户帐户:
    1. 职位描述为 developer 的用户应当:
      1. 在 dev 和 test 主机组中的受管节点上创建
      2. 从 pw_developer 变量分配密码
      3. 密码有效期限为30天(新增要求)
      4. 为用户分配特定的UID(变量文件和题目中均未明确给出具体的UID值)
      5. 是附加组 student 的成员
    2. 职位描述为 manager 的用户应当:
      1. 在 prod 主机组中的受管节点上创建
      2. 从 pw_manager 变量分配密码
      3. 密码有效期限为30天(新增要求)
      4. 为用户分配特定的UID(变量文件和题目中均未明确给出具体的UID值)
      5. 是附加组 opsmgr 的成员
  3. 密码应采用 SHA512 哈希格式。
  4. 您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件 /home/student/ansible/secret.txt 正常运行。
  • 答案
# 下载用户列表文件,并查看里面内容,后续写playbook文件会用到
[student@workstation ansible]$ wget ​http://172.25.254.254/content/user_list.yml
[student@workstation ansible]$ cat /home/student/ansible/user_list.yml
---
users:
- name: bob
  job: developer
  password_max_expire: 30
- name: sally
  job: manager
  password_max_expire: 30
- name: fred
  job: developer
  password_max_expire: 30

#编写playbook,按题目要求创建用户,里面会涉及很多变量要记
[student@workstation ansible]$ vim /home/student/ansible/users.yml
[student@workstation ansible]$ cat /home/student/ansible/users.yml
---
- name: create user that job=developer
  hosts: dev,test
  vars_files:
    - /home/student/ansible/locker.yml
    - /home/student/ansible/user_list.yml
  tasks:
    - name: ensure group student exist
      group:
        name: student
        state: present

    - name: create user developer
      user:
        name: "{{ item.name }}"
        password: "{{ pw_developer | password_hash('sha512') }}"
        groups: student
        append: yes
      loop: "{{ users }}"
      when: item.job=="developer"

    - name: chage password_max_days
      shell: chage -M "{{ item.password_max_expire }}" "{{ item.name }}"
      loop: "{{ users }}"
      when: item.job=="developer"

- name: create user that job=manager
  hosts: prod
  vars_files:
    - /home/student/ansible/locker.yml
    - /home/student/ansible/user_list.yml
  tasks:
    - name: ensure group opsmgr exist
      group:
        name: opsmgr
        state: present

    - name: create user manager
      user:
        name: "{{ item.name }}"
        password: "{{ pw_manager | password_hash('sha512') }}"
        groups: opsmgr
        append: yes
      loop: "{{ users }}"
      when: item.job=="manager"

    - name: chage password_max_days
      shell: chage -M "{{ item.password_max_expire }}" "{{ item.name }}"
      loop: "{{ users }}"
      when: item.job=="manager"

# 检查playbook语法无误,然后执行playbook,创建用户
[student@workstation ansible]$ ansible-playbook --syntax-check --vault-password-file=secret.txt /home/student/ansible/users.yml
[student@workstation ansible]$ ansible-playbook --vault-password-file=secret.txt /home/student/ansible/users.yml

# 验证答案,查看用户附加组是否为题目要求,并切换用户验证密码是否正确
[student@workstation ansible]$ ansible all -m shell -a "id bob;id sally;id fred"
[student@workstation ansible]$ ssh fred@serverb
[student@workstation ansible]$ ssh  sally@serverd

十五、更新Ansible库的密钥

  • 题目

按照下方所述,更新现有 Ansible 库的密钥:

  1. http://172.25.254.254/content/salaries.yml 下载 Ansible 库到 /home/student/ansible
  2. 当前的库密码为 insecure4sure
  3. 新的库密码为 bbe2de98389b
  4. 库使用新密码保持加密状态
  • 答案
# 下载已加密的库文件
[student@workstation ansible]$ wget http://172.25.254.254/content/salaries.yml

# 更换密码前用旧密码试着查看一下里面的内容
[student@workstation ansible]$ cat salaries.yml
[student@workstation ansible]$ ansible-vault view salaries.yml

# 更换密码后再用新密码查看一下里面的内容
[student@workstation ansible]$ ansible-vault rekey salaries.yml
[student@workstation ansible]$ ansible-vault view salaries.yml

十六、cron定时任务(新增)

  • 题目

创建名为/home/student/ansible/cron.yml 的剧本,在所有托管主机上运行为用户natasha创建如下cron作业:
        用户natasha必须配置运行每隔2分钟执行logger “RH294 in progress” 的cron做作业

  • 答案
# 编写playbook剧本
[student@workstation ansible]$ vim /home/student/ansible/cron.yml
[student@workstation ansible]$ cat /home/student/ansible/cron.yml
---
- name: configure cron homework
  hosts: all
  tasks:
    - name: create a crontab work
      cron:
        name: "a job for logger"
        user: natasha
        minute: "*/2"
        job: 'logger "RH294 in progress"'
        state: present

# 检查playbook语法无报错,然后执行
[student@workstation ansible]$ ansible-playbook --syntax-check /home/student/ansible/cron.yml
[student@workstation ansible]$ ansible-playbook /home/student/ansible/cron.yml

# 查看natasha用户的cron日志,是否有输出这条信息
[student@workstation ansible]$ ansible all -m shell -a "cat /var/log/cron | tail - 10"

注意事项总结

RHCE考试时,所有操作均在ansible控制主机上完成,受管节点一般不需要登陆,除非有必要时上去验证一下配置

所有操作均使用student用户,在/home/student/ansible目录下做配置,千万别把配置敲到其它用户或其它目录了

一定要熟练使用ansible-doc 查询某些模块的作用,过RHCE就靠查询模块然后复制粘贴,不需要死记硬背,每道题考察哪些模块一定要牢记!!!

熟悉playbook语法格式,层级结构

一个playbook.yml文件可以包含多个play,每一个play下面需要指定主机组hosts和任务tasks,主机组一样的话,就可以写到一个play里面

所有的变量都要用双引号,大括号包裹起来,注意注意注意    "{{   变量    }}"

多敲多练,刷题能过,考试肯定能过,题库稳的一逼几年没怎么变过,反正我过了哈哈哈哈^_^

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值