紅帽認證RHEL8考題 RHCE8考試題庫(RHCE8)

RHCE沒有滿分,僅供參考,可以評論建議答案,以方便需要考證的朋友。

點擊>>>RHCSA考試題庫RHEL8.0<<<查看內容

RHCE8.0考試

考試有6臺虛擬機,其中一臺是需要操作ansible的,考試要求在greg用戶下完成操作,其餘5臺是被控機器node1-5 所有機器密碼均爲123

安裝和配置 Ansible

按照下方所述,在控制節點 control.example.com 上安裝和配置 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

$ sudo yum install -y ansible
$ mkdir -p /home/greg/ansible/roles
$ cd /home/greg/ansible
$ cp /etc/ansible/ansible.cfg .
$ vim /home/greg/ansible/inventory
[all:vars]
ansible_password=123
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod
#保存,退出
$ vim ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
remote_user = root
#保存,退出

創建和運行 Ansible 臨時命令

爲系統管理員,您需要在受管節點上安裝軟件。
照正文所述,創建一個名爲 /home/greg/ansible/adhoc.sh 的 shell 腳本,該腳本將使用 Ansible 臨時命令在各個受管節點上安裝 yum 存儲庫:
儲存庫1:

  • 存儲庫的名稱爲 EX294_BASE
  • 描述爲 EX294 base software
  • 基礎 URL 爲 http://xxx.example.com.com/BaseOS
  • GPG 簽名檢查爲啓用狀態
  • GPG 密鑰 URL 爲 http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release
  • 存儲庫爲啓用狀態

存儲庫2:

  • 存儲庫的名稱爲 EX294_STREAM
  • 描述爲 EX294 stream software
  • 基礎 URL 爲 http://xxx.example.com.com/AppStream
  • GPG 簽名檢查爲啓用狀態
  • GPG 密鑰 URL 爲 http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release
  • 存儲庫爲啓用狀態
vim /home/greg/ansible/adhoc.sh

#!/bin/bash
ansible all -m yum_repository -a \
'name="EX294_BASE" \
description="EX294 base software" \
baseurl="http://xxx.example.com.com/BaseOS" \
gpgcheck=yes \
gpgkey="http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release" \
enable=yes'
ansible all -m yum_repository -a \
'name="EX294_STREAM" \
description="EX294 stream software" \
baseurl="xxx.example.com/AppStream" \
gpgcheck=yes \
gpgkey="http://xxx.example.com/RHEL/RPM-GPG-KEY-redhat-release" \
enable=yes'

安裝軟件包

創建一個名爲 /home/greg/ansible/packages.yml 的 playbook :

  • 將 php 和 mariadb 軟件包安裝到 dev、test 和 prod 主機組中的主機上
  • 將 RPM Development Tools 軟件包組安裝到 dev 主機組中的主機上
  • 將 dev 主機組中主機上的所有軟件包更新爲最新版本
vim /home/greg/ansible/packages.yml
---
- name: 安裝軟件包
  hosts: dev,test,prod 
  tasks:
  - name: install the latest version
    yum:
      name: "{{ item }}"
      state: latest
    loop:
    - php
    - mariadb
  - block:
    - name: install the '@RPM Development Tools' package group
      yum:
        name: "@RPM Development Tools"
        state: present
      when: "'dev' in group_names"
    - name: upgrade all packages
      yum:
        name: '*' 
        state: latest
    when: "'dev' in group_names"
ansible-playbook /home/greg/ansible/packages.yml

使用 RHEL 系統角色

安裝 RHEL 系統角色軟件包,並創建符合以下條件的 playbook /home/greg/ansible/timesync.yml :

  • 在所有受管節點上運行
  • 使用 timesync 角色
  • 配置該角色,以使用當前有效的 NTP 提供商
  • 配置該角色,以使用時間服務器 172.20.20.254
  • 配置該角色,以啓用 iburst 參數
$ sudo yum -y install rhel-system-roles
$ vim ansible.cfg
#roles_path  = /home/greg/ansible/roles:/usr/share/ansible/roles
$ ansible-galaxy list
$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml /home/greg/ansible/timesync.yml
$ vim /home/greg/ansible/timesync.yml
---
- hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: 172.20.20.254
        iburst: yes
  roles:
    - rhel-system-roles.timesync
ansible-playbook /home/greg/ansible/timesync.yml

創建和使用角色

根據下列要求,在 /home/greg/ansible/roles 中創建名爲 apache 的角色:

  • httpd 軟件包已安裝,設爲在系統啓動時啓用並啓動

  • 防火牆已啓用並正在運行,並使用允許訪問 Web 服務器的規則

  • 模板文件 index.html.j2 已存在,用於創建具有以下輸出的文件 /var/www/html/index.html :
    Welcome to HOSTNAME on IPADDRESS
    其中,HOSTNAME 是受管節點的完全限定域名,IPADDRESS 則是受管節點的 IP 地址。

  • 創建 playbook /home/greg/ansible/apache.yml ,使用apache 的角色,在 webservers 主機組。

$ cd roles/
$ ansible-galaxy init apache
$ cd ..
$ ansible-galaxy list
$ vim roles/apache/tasks/main.yml
---
- name: install the latest version of Apache
  yum:
    name: httpd
    state: latest
- name: Start service httpd, if not started
  service:
    name: httpd
    state: started
    enabled: yes
- firewalld:
    service: http
    permanent: yes
    state: enabled
    immediate: yes
- name: Template a file
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
$ vim roles/apache/templates/index.html.j2
# Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
vim /home/greg/ansible/apache.yml
---
- name: 使用角色
  hosts: webservers
  roles:
  - apache
$ ansible-playbook /home/greg/ansible/apache.yml

從 Ansible Galaxy 使用角色

根據下列要求,創建一個名爲 /home/greg/ansible/roles.yml 的 playbook :

  • playbook 中包含一個 play, 該 play 在 balancers 主機組中的主機上運行並將使用 balancer 角色。
  • 此角色配置一項服務,以在 webservers 主機組中的主機之間平衡 Web 服務器請求的負載。
  • 瀏覽到 balancers 主機組中的主機(例如 http://node5.example.com/)將生成以下輸出:
    Welcom to node3.example.com on 172.24.22.8
  • 重新加載瀏覽器將從另一 Web 服務器生成輸出:
    Welcom to node4.example.com on 172.24.22.9
  • playbook 中包含一個 play, 該 play 在 webservers 主機組中的主機上運行並將使用 phpinfo 角色。
  • 請通過 URL /hello.php 瀏覽到 webservers 主機組中的主機將生成以下輸出:
    Hello PHP World from FQDN
    其中,FQDN 是主機的完全限定名稱。
  • 例如,瀏覽到 http://node3.example.com/hello.php 會生成以下輸出:
    Hello PHP World from node3.example.com
    另外還有 PHP 配置的各種詳細信息,如安裝的 PHP 版本等。
  • 同樣,瀏覽到 http://node4.example.com/hello.php 會生成以下輸出:
    Hello PHP World from
    node4.example.com
    另外還有 PHP 配置的各種詳細信息,如安裝的 PHP 版本等
$ vim /home/greg/ansible/roles.yml
---
- name: one
  hosts: webservers
  roles:
  - apache

- name: two
  hosts: balancers
  roles:
  - balancer

- name:  san
  hosts: webservers
  roles:
  - phpinfo
$ ansible-playbook roles.yml

創建和使用邏輯卷

創建一個名爲 /home/greg/ansible/lv.yml 的 playbook ,它將在所有受管節點上運行以執行下列任務:

  • 創建符合以下要求的邏輯卷:
  • 邏輯卷創建在 research 卷組中
  • 邏輯卷名稱爲 data
  • 邏輯卷大小爲 1500 MiB
  • 使用 ext4 文件系統格式化邏輯卷
  • 如果無法創建請求的邏輯卷大小,應顯示錯誤信息
    Could not create logical volume of that size,並且應改爲使用大小 800 MiB。
  • 如果卷組 research 不存在,應顯示錯誤信息
    Volume group done not exist。
  • 不要以任何方式掛載邏輯卷
$ vim /home/greg/ansible/lv.yml
---
- name: 創建和使用邏輯卷
  hosts: all
  tasks:
  - block:
    - name: Create a logical volume of 1500m
      lvol:
        vg: research
        lv: data
        size: 1500
    - name: Create a ext4
      filesystem:
        fstype: ext4
        dev: /dev/research/data
    rescue:
    - debug:
        msg: Could not create logical volume of that size
    - name: Create a logical volume of 800m
      lvol:
        vg: research
        lv: data
        size: 800
      when: ansible_lvm.vgs.research is defined
      ignore_errors: yes
    - debug:
        msg: Volume group done not exist
      when: ansible_lvm.vgs.research is undefined
$ ansible-playbook /home/greg/ansible/lv.yml

生成主機文件

生成主機文件

  • 將一個初始模板文件從 http://rhgls.realm8.example.com/materials/hosts.j2 下載到 /home/greg/ansible
  • 完成該模板,以便用它生成以下文件:針對每個清單主機包含一行內容,其格式與 /etc/hosts 相同
  • 創建名爲 /home/greg/ansible/hosts.yml 的 playbook ,它將使用此模板在 dev 主機組中的主機上生成文件 /etc/myhosts 。
    該 playbook 運行後, dev 主機組中主機上的文件 /etc/myhosts 應針對每個受管主機包含一行內容:
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    172.242.6 node1.realm8.example.com node1
    172.242.7 node2.realm8.example.com node2
    172.242.8 node3.realm8.example.com node3
    172.242.9 node4.realm8.example.com node4
    172.242.10 node5.realm8.example.com node5
    注:清單主機名稱的顯示順序不重要。
$ wget http://materials/hosts.j2
$ vim hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
$ vim hosts.yml
---
- name: 生成主機文件
  hosts: all 
  tasks:
  - name: Template a file to /etc/myhosts
    template:
      src: /home/greg/ansible/hosts.j2
      dest: /etc/myhosts
    when: '"dev" in group_names'
$ ansible-playbook hosts.yml 

<–未完待續–>

點擊RHCSA考試題庫RHEL8.0查看考題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章