2022年rhce最新認證—(滿分通過)

RHCE認證

重要配置資訊

在考試期間,除了您就坐位置的台式機之外,還將使用多個虛擬系統。您不具有台式機系統的 root 訪問權,但具有對虛擬系統的完整 root 訪問權。

系統資訊

在本考試期間,您將操作下列虛擬系統:

系統 IP 地址 Ansible 角色
control 172.25.250.254 ansible control node
node1 172.25.250.9 ansible managed node
node2 172.25.250.10 ansible managed node
node3 172.25.250.11 ansible managed node
node4 172.25.250.12 ansible managed node
node5 172.25.250.13 ansible managed node

這些系統的 IP 地址採用靜態設置。請勿更改這些設置。

主機名稱解析已配置為解析上方列出的完全限定主機名,同時也解析主機短名稱。

帳戶資訊

所有系統的 root 密碼是 flectrag

請勿更改 root 密碼。除非另有指定,否則這將是用於訪問其他系統和服務的密碼。此外,除非另有指定,否則此密碼也應用於您創建的所有帳戶,或者任何需要設置密碼的服務。

為方便起見,所有系統上已預裝了 SSH 密鑰,允許在不輸入密碼的前提下通過 SSH 進行 root 訪問。請勿對系統上的 root SSH 配置文件進行任何修改。

Ansible 控制節點上已創建了用戶帳戶 greg。此帳戶預裝了 SSH 密鑰,允許在 Ansible 控制節點和各個 Ansible 受管節點之間進行 SSH 登錄。請勿對系統上的 greg SSH 配置文件進行任何修改。您可以從 root 帳戶使用 su 訪問此用戶帳戶。

重要資訊

除非另有指定,否則您的所有工作(包括 Ansible playbook、配置文件和主機清單等)應當保存在控制節點上的目錄 /home/greg/ansible 中,並且應當歸 greg 用戶所有。所有 Ansible 相關的命令應當由 greg 用戶從 Ansible 控制節點上的這個目錄運行。

其他資訊

一些考試項目可能需要修改 Ansible 主機清單。您要負責確保所有以前的清單組和項目保留下來,與任何其他更改共存。您還要有確保清單中所有默認的組和主機保留您進行的任何更改。

考試系統上的防火牆默認為不啟用,SELinux則處於強制模式。

如果需要安裝其他軟體,您的物理系統和 Ansible 控制節點可能已設置為指向 content 上的下述存儲庫:

一些項目需要額外的文件,這些文件已在以下位置提供:

產品文檔可從以下位置找到:

其他資源也進行了配置,供您在考試期間使用。關於這些資源的具體資訊將在需要這些資源的項目中提供。

重要資訊
請注意,在評分之前,您的 Ansible 受管節點系統將重置為考試開始時的初始狀態,您編寫的 Ansible playbook 將通過以 greg 用戶身份從控制節點上的目錄 /home/greg/ansible 目錄運行來應用。在 playbook 運行後,系統會對您的受管節點進行評估,以判斷它們是否按照規定進行了配置。

第一題:安裝和配置 Ansible

安裝和配置Ansible

按照下方所述,在控制節點 control 上安裝和配置 Ansible:

第一步

# 通過遠程連接greg用戶進入控制節點control
[kiosk@foundation0 ~]$ ssh greg@control
greg@control's password: 
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Nov 11 15:11:37 2022 from 172.25.250.250
[greg@control ~]$ 

第二步

# 安裝ansible軟體包
[greg@control ~]$ sudo yum -y install ansible

# 檢查軟體是否安裝成功
[greg@control ~]$ ansible --version
ansible 2.8.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/greg/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Apr  3 2019, 17:26:03) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]

第三步

# 創建角色路徑,並進入ansible目錄
[greg@control ~]$ mkdir -p /home/greg/ansible/roles
[greg@control ~]$ cd ansible/
[greg@control ansible]$ 

# 編輯清單文件
[greg@control ~]$ vim /home/greg/ansible/inventory
[dev]
node1

[test]
node2

[prod]
node3
node4

[balancers]
node5

[webservers:children]
prod

第四步

# 安裝完ansible會有一個默認的配置文件,拷貝到題中指定目錄進行修改
[greg@control ~]$ cp /etc/ansible/ansible.cfg /home/greg/ansible/ansible.cfg
[greg@control ~]$ vim /home/greg/ansible/ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory ## 首先找到inventory,改成題中指定的目錄
roles_path = /home/greg/ansible/roles ## 修改角色目錄為指定目錄
host_key_checking = False  ## 關掉主機key檢查
remote_user = greg  ## 更改遠端執行用戶為題目中指定的用戶,這裡時greg,考試時隨機應變
## 找到[privilege_escalation]標籤和這些become,把下面四行注釋去掉
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False

第五步

# 檢查
[greg@control ansible]$ ansible-inventory --graph
@all:
  |--@balancers:
  |  |--node5
  |--@dev:
  |  |--node1
  |--@test:
  |  |--node2
  |--@ungrouped:
  |--@webservers:
  |  |--@prod:
  |  |  |--node3
  |  |  |--node4
[greg@control ansible]$ ansible all -m ping -o
node3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node5 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node4 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}

第二題:創建和運行 Ansible 臨時命令

創建和運行 Ansible 臨時命令

作為系統管理員,您需要在受管節點上安裝軟體。

請按照正文所述,創建一個名為 /home/greg/ansible/adhoc.sh 的 shell 腳本,該腳本將使用 Ansible 臨時命令在各個受管節點上安裝 yum 存儲庫:

存儲庫1:

存儲庫2:

第一步

# ansible-doc查詢文檔
[greg@control ansible]$ ansible-doc -l | grep yum
yum                                                    Manages packages with the `yum' package manager                                                  
yum_repository                                         Add or remove YUM repositories    

第二步

# 創建shell腳本文件
[greg@control ansible]$ vim /home/greg/ansible/adhoc.sh

#!/bin/bash
ansible all -m yum_repository -a "name=EX294_BASE description='EX294 base software' baseurl=//content/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=//content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"
ansible all -m yum_repository -a "name=EX294_STREAM description='EX294 stream software' baseurl=//content/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=//content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"

第三步

# shell腳本文件添加執行許可權,並運行
[greg@control ansible]$ chmod +x /home/greg/ansible/adhoc.sh
[greg@control ansible]$ /home/greg/ansible/adhoc.sh

第四步

# 測試,驗證
[greg@control ansible]$ ansible all -a 'yum repolist'
...

第三題:安裝軟體包

安裝軟體包

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

第一步

# 設置行號顯示,設置Tab格式
[greg@control ansible]$ vim ~/.vimrc
set number ts=2 sw=2 et

第二步

# 創建playbook,編寫playbook
[greg@control ansible]$ vim /home/greg/ansible/packages.yml
---
- name: 安裝軟體包1
  hosts: dev,test,prod
  tasks: 
  - name: ensure a list of packages installed
    yum:
      name: "{{ packages }}"
    vars:
      packages:
      - php
      - mariadb  

- name: 安裝軟體包2
  hosts: dev,test,prod
  tasks: 
  - name: install the package group
    yum:
      name: "@RPM Development Tools"
      state: present
  - name: upgrade all packages
    yum:
      name: '*'
      state: latest

第四步

# 執行playbook
[greg@control ansible]$ ansible-playbook /home/greg/ansible/packages.yml

第五步

# 驗證
[greg@control ansible]$ ansible dev,test,prod -a "rpm -q php mariadb"
[greg@control ansible]$ ansible dev -a "yum grouplist"
[greg@control ansible]$ ansible dev -a "yum update"

第四題:使用 RHEL 系統角色(NEW)

使用 RHEL 系統角色

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

第一步

# 搜索軟體包
[greg@control ansible]$ yum search role
Last metadata expiration check: 1:39:45 ago on Tue 15 Nov 2022 10:03:09 AM GMT.
============================================ Name & Summary Matched: role ============================================
policycoreutils-newrole.x86_64 : The newrole application for RBAC/MLS
================================================= Name Matched: role =================================================
rhel-system-roles.noarch : Set of interfaces for unified system management
=============================================== Summary Matched: role ================================================
ansible-freeipa.noarch : Roles and playbooks to deploy FreeIPA servers, replicas and clients

第二步

# 安裝角色軟體包
[greg@control ansible]$ sudo yum -y install rhel-system-roles.noarch

第三步

# 查看角色路徑,角色路徑放到配置文件
[greg@control ansible]$ rpm -ql rhel-system-roles.noarch
/usr/share/ansible/roles
...

[greg@control ansible]$ vim ansible.cfg
roles_path    = /home/greg/ansible/roles:/usr/share/ansible/roles 

第四步

# 查找配置文件樣例,複製樣例到playbook,修改playbook
[greg@control ansible]$ rpm -ql rhel-system-roles.noarch | grep example
/usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml

[greg@control ansible]$ cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml /home/greg/ansible/selinux.yml
[greg@control ansible]$ vim /home/greg/ansible/selinux.yml
---
- hosts: all
  vars:
    selinux_policy: targeted
    selinux_state: enforcing

  # prepare prerequisites which are used in this playbook
  tasks:
    - name: execute the role and catch errors
      block:
        - include_role:
            name: rhel-system-roles.selinux
      rescue:
        # Fail if failed for a different reason than selinux_reboot_required.
        - name: handle errors
          fail:
            msg: "role failed"
          when: not selinux_reboot_required

        - name: restart managed host
          shell: sleep 2 && shutdown -r now "Ansible updates triggered"
          async: 1
          poll: 0
          ignore_errors: true

        - name: wait for managed host to come back
          wait_for_connection:
            delay: 10
            timeout: 300

        - name: reapply the role
          include_role:
            name: rhel-system-roles.selinux

第五步

# 執行playbook
[greg@control ansible]$ ansible-playbook selinux.yml 

第六步

# 驗證
[greg@control ansible]$ ansible all -a "grep ^SELINUX /etc/selinux/config"
node4 | CHANGED | rc=0 >>
SELINUX=enforcing
SELINUXTYPE=targeted
...

第四題:使用 RHEL 系統角色(OLD)

使用 RHEL 系統角色

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

第一步

# 搜索軟體包
[greg@control ansible]$ yum search role
Last metadata expiration check: 1:39:45 ago on Tue 15 Nov 2022 10:03:09 AM GMT.
============================================ Name & Summary Matched: role ============================================
policycoreutils-newrole.x86_64 : The newrole application for RBAC/MLS
================================================= Name Matched: role =================================================
rhel-system-roles.noarch : Set of interfaces for unified system management
=============================================== Summary Matched: role ================================================
ansible-freeipa.noarch : Roles and playbooks to deploy FreeIPA servers, replicas and clients

第二步

# 安裝角色軟體包
[greg@control ansible]$ sudo yum -y install rhel-system-roles.noarch

第三步

# 查看角色路徑,角色路徑放到配置文件
[greg@control ansible]$ rpm -ql rhel-system-roles.noarch
/usr/share/ansible/roles
...

[greg@control ansible]$ vim ansible.cfg
roles_path    = /home/greg/ansible/roles:/usr/share/ansible/roles 

第四步

# 查找配置文件樣例,複製樣例到playbook,並修改playbook
[greg@control ansible]$ rpm -ql rhel-system-roles.noarch
/usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml

[greg@control ansible]$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml /home/greg/ansible/timesync.yml
[greg@control ansible]$ vim /home/greg/ansible/timesync.yml
---
- hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: 172.25.254.254
        iburst: yes
  roles:
    - rhel-system-roles.timesync

第五步

# 運行playbook
[greg@control ansible]$ ansible-playbook timesync.yml

第六步

# 驗證
[greg@control ansible]$ ansible all -m shell -a 'chronyc sources'

第五題:使用 Ansible Galaxy 安裝角色

使用 Ansible Galaxy 安裝角色

使用 Ansible Galaxy 和要求文件 /home/greg/ansible/roles/requirements.yml 。從以下 URL 下載角色並安裝到 /home/greg/ansible/roles

第一步

# 編寫playbook文件
[greg@control ansible]$ vim /home/greg/ansible/roles/requirements.yml
---
- src: //materials/haproxy.tar
  name: balancer
- src: //materials/phpinfo.tar
  name: phpinfo

第二步

# 安裝角色
[greg@control ansible]$ ansible-galaxy role install -r  /home/greg/ansible/roles/requirements.yml

第三步

# 驗證
[greg@control ansible]$ ansible-galaxy list
# /home/greg/ansible/roles
- balancer, (unknown version)
- phpinfo, (unknown version)

第六題:創建和使用角色

創建和使用角色

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

Welcome to HOSTNAME on IPADDRESS

其中,HOSTNAME 是受管節點的完全限定域名IPADDRESS 則是受管節點的 IP 地址。

創建一個名為 /home/greg/ansible/apache.yml 的 playbook:

第一步

# 進入角色路徑,創建名為 apache 的角色
[greg@control ansible]$ cd roles/
[greg@control roles]$ ansible-galaxy init apache
- Role apache was created successfully

第二步

# 編寫任務tasks文件
[greg@control roles]$ vim apache/tasks/main.yml 
---
# tasks file for apache
- name: ensure a list of packages installed
  yum:
    name: "{{ packages }}"
  vars:
    packages:
    - httpd
    - firewalld

- name: Start service httpd, if not started
  service:
    name: httpd
    state: started
    enabled: yes

- name: Start service firewalld, if not started
  service:
    name: firewalld
    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

第三步

# 編寫模板文件
[greg@control roles]$ vim apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}

第四步

# 回到ansible路徑,編寫playbook文件
[greg@control roles]$ cd ..
[greg@control ansible]$ vim /home/greg/ansible/apache.yml
 ---
- name: 創建和使用角色
  hosts: webservers
  roles:
  - apache

第五步

# 執行playbook文件
[greg@control ansible]$ ansible-playbook /home/greg/ansible/apache.yml

第六步

# 驗證
[greg@control ansible]$ curl node3
Welcome to node3.lab.example.com on 172.25.250.11
[greg@control ansible]$ curl node4
Welcome to node4.lab.example.com on 172.25.250.12

第七題:從 Ansible Galaxy 使用角色

從 Ansible Galaxy 使用角色

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

  • Welcome to node3.lab.example.com on 172.25.250.11
    
  • Welcome to node4.lab.example.com on 172.25.250.12
    
  • Hello PHP World from FQDN
    
  • Hello PHP World from node3.lab.example.com
    

另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等。

Hello PHP World from node4.lab.example.com

另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等。

第一步

# 編寫playbook
[greg@control ansible]$ vim /home/greg/ansible/roles.yml

第二步

# 執行playbook
---
- name: 從 Ansible Galaxy 使用角色1
  hosts: webservers
  roles:
  - phpinfo
  - 
- name: 從 Ansible Galaxy 使用角色2
  hosts: balancers
  roles:
  - balancer

第三步

驗證

image-20221115114102144

image-20221115114135412

image-20221115114233310

image-20221115114314011

image-20221115114400473

第八題:創建和使用分區(NEW)

創建和使用分區

創建一個名為 /home/greg/ansible/partition.yml 的 playbook ,它將在所有受管節點上創建分區:

第一步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/partition.yml
---
- name: 創建和使用分區
  hosts: all 
  tasks:
  - name: Handle the error
    block:
    - name: Create a new primary partition
      parted:
        device: /dev/vdb
        number: 1
        state: present
        part_end: 1500MiB
    - name: Create a ext4 filesystem 
      filesystem:
        fstype: ext4
        dev: /dev/vdb1
    - name: Mount 
      mount:
        path: /data
        src: /dev/vdb1
        fstype: ext4
        state: mounted
      when: inventory_hostname in groups.prod
    rescue:
    - debug:
        msg: Could not create partition of that size
    - name: Create a new primary partition
      parted:
        device: /dev/vdb
        number: 1
        state: present
        part_end: 800MiB
    when: ansible_devices.vdb is defined
  - debug:
      msg: this disk is not exist
    when: ansible_devices.vdb is undefined

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook partition.yml 

第三步

# 驗證
[greg@control ansible]$ ansible all -a 'lsblk'

第八題:創建和使用邏輯卷(OLD)

創建和使用邏輯卷

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

Could not create logical volume of that size

,並且應改為使用大小 800 MiB。

Volume group done not exist

第一步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/lv.yml
---
- name: 創建和使用邏輯卷
  hosts: all 
  tasks:
  - block:
    - name: Create a logical volume
      lvol:
        vg: research
        lv: data
        size: 1500
    - name: Create a ext4 filesystem
      filesystem:
        fstype: ext4
        dev: /dev/research/data
    rescue:
    - debug:
        msg: Could not create logical volume of that size
    - name: Create a logical volume
      lvol:
        vg: research
        lv: data
        size: 800 
    when: ansible_lvm.vgs.research is defined
  - debug:
      msg: Volume group done not exist
    when: ansible_lvm.vgs.research is not defined

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook lv.yml 

第三步

# 驗證
[greg@control ansible]$ ansible all -a "lvs"

第九題:生成主機文件

生成主機文件

該 playbook 運行後, dev 主機組中主機上的文件 /etc/myhosts 應針對每個受管主機包含一行內容:

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6


172.25.250.9    node1.lab.example.com node1

172.25.250.10   node2.lab.example.com node2

172.25.250.11   node3.lab.example.com node3

172.25.250.12   node4.lab.example.com node4

172.25.250.13   node5.lab.example.com node5

註:清單主機名稱的顯示順序不重要。

第一步

# 下載初始模板文件
[greg@control ansible]$ wget //materials/hosts.j2

第二步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/hosts.yml
---
- name: 生成主機文件
  hosts: all 
  tasks:
  - name: Template a file to /etc/myhosts
    template:
      src: /home/greg/ansible/hosts.j2
      dest: /etc/myhosts
    when: inventory_hostname in groups.dev

第三步

# 編寫hosts.j2文件
[greg@control ansible]$ vim 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']['nodename'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}

第四步

# 執行playbook
[greg@control ansible]$ ansible-playbook hosts.yml

第五步

# 驗證
[greg@control ansible]$ ansible dev -a "cat /etc/myhosts"

第十題:修改文件內容

修改文件內容

按照下方所述,創建一個名為 /home/greg/ansible/issue.yml 的 playbook :

第一步

# 創建playbook,並編寫playbook
[greg@control ansible]$ vim /home/greg/ansible/issue.yml
---
- name: 修改文件內容
  hosts: all
  tasks:
  - name: Copy using inline content 1
    copy:
      content: 'Development'
      dest: /etc/issue
    when: inventory_hostname in groups.dev
  - name: Copy using inline content 2
    copy:
      content: 'Test'
      dest: /etc/issue
    when: inventory_hostname in groups.test
  - name: Copy using inline content 3
    copy:
      content: 'Production'
      dest: /etc/issue
    when: inventory_hostname in groups.prod

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook issue.yml 

第三步

# 驗證
[greg@control ansible]$ ansible all -a "cat  /etc/issue"

第十一題:創建 Web 內容目錄

創建 Web 內容目錄

按照下方所述,創建一個名為 /home/greg/ansible/webcontent.yml 的 playbook :

Development

第一步

# 檢查webdev 組是否存在
[greg@control ansible]$ ansible dev -a "grep webdev /etc/group"
node1 | CHANGED | rc=0 >>
webdev:x:1003:

第二步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/webcontent.yml
---
- name: 創建 Web 內容目錄
  hosts: dev
  tasks:
  - name: Create a directory if it does not exist
    file:
      path: /webdev
      state: directory
      group: webdev
      mode: u=rwx,g=rwxs,o=rx
  - name: Create a symbolic link
    file:
      src: /webdev
      dest: /var/www/html/webdev
      state: link
  - name: Copy using inline content
    copy:
      content: 'Development'
      dest: /webdev/index.html
      setype: httpd_sys_content_t
  - name: Start service httpd, if not started
    service:
      name: httpd
      state: started
      enabled: yes

第三步

# 執行playbook
[greg@control ansible]$ ansible-playbook webcontent.yml

第四步

驗證

image-20221115162049962

第十二題:生成硬體報告

生成硬體報告

創建一個名為 /home/greg/ansible/hwreport.yml 的 playbook ,它將在所有受管節點上生成含有以下資訊的輸出文件 /root/hwreport.txt

您的 playbook 應當:

第一步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/hwreport.yml

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook hwreport.yml
---
- name: 生成硬體報告
  hosts: all 
  tasks:
  - name: Download foo.conf
    get_url:
      url: //materials/hwreport.empty
      dest: /root/hwreport.txt
  - name: Ensure 1
    lineinfile:
      path: /root/hwreport.txt
      regexp: '^HOST='
      line: HOST={{ inventory_hostname }}
  - name: Ensure 2
    lineinfile:
      path: /root/hwreport.txt
      regexp: '^MEMORY='
      line: MEMORY={{ ansible_memtotal_mb }}
  - name: Ensure 3
    lineinfile:
      path: /root/hwreport.txt
      regexp: '^BIOS='
      line: BIOS={{ ansible_bios_version }}
  - name: Ensure 4
    lineinfile:
      path: /root/hwreport.txt
      regexp: '^DISK_SIZE_VDA='
      line: DISK_SIZE_VDA={{ ansible_devices.vda.size }}
  - name: Ensure 5
    lineinfile:
      path: /root/hwreport.txt
      regexp: '^DISK_SIZE_VDB='
      line: DISK_SIZE_VDB={{ ansible_devices.vdb.size | default('NONE', true) }}

第三步

# 驗證
[greg@control ansible]$ ansible all -a 'cat /root/hwreport.txt'

第十三題:創建密碼庫

創建密碼庫

按照下方所述,創建一個 Ansible 庫來存儲用戶密碼:

第一步

# 密碼導入密碼存儲文件
[greg@control ansible]$ echo "whenyouwishuponastar" > /home/greg/ansible/secret.txt

第二步

# 修改配置文件存儲路徑
[greg@control ansible]$ vim ansible.cfg
vault_password_file = /home/greg/ansible/secret.txt 

第三步

# 創建Ansible 庫,存儲用戶密碼
[greg@control ansible]$ ansible-vault create /home/greg/ansible/locker.yml
pw_developer: Imadev
pw_manager: Imamgr

第四步

# 驗證
[greg@control ansible]$ ansible-vault view /home/greg/ansible/locker.yml

---
pw_developer: Imadev
pw_manager: Imamgr

第十四題:創建用戶帳戶

創建用戶帳戶

第一步

# 查看組是否存在
[greg@control ansible]$ ansible dev,test -a "grep devops /etc/group"

第二步

# 下載要創建的用戶的列表
[greg@control ansible]$ wget //materials/user_list.yml

第三步

# 創建playbook,並編寫
[greg@control ansible]$ vim /home/greg/ansible/users.yml
---
- name: 創建用戶帳戶 1
  hosts: dev,test
  vars_files:
  - /home/greg/ansible/locker.yml
  - /home/greg/ansible/user_list.yml
  tasks:
  - name: Ensure group 1
    group:
      name: devops
      state: present
  - name: Add the user 1
    user:
      name: "{{ item.name }}"
      groups: devops
      password: "{{ pw_developer | password_hash('sha512') }}"
      append: yes
    loop: "{{ users }}"
    when: item.job == 'developer'

- name: 創建用戶帳戶 2
  hosts: prod
  vars_files:
  - /home/greg/ansible/locker.yml
  - /home/greg/ansible/user_list.yml
  tasks:
  - name: Ensure group 2
    group:
      name: opsmgr
      state: present
  - name: Add the user 2
    user:
      name: "{{ item.name }}"
      groups: opsmgr
      password: "{{ pw_manager | password_hash('sha512') }}"
      append: yes
    loop: "{{ users }}"
    when: item.job == 'manager'

第四步

# 執行playbook
[greg@control ansible]$ ansible-playbook users.yml

第五步

# 驗證
[greg@control ansible]$ ansible dev,test -m shell -a "id bob; id sally; id fred"

第十五題:更新 Ansible 庫的密鑰

更新 Ansible 庫的密鑰

按照下方所述,更新現有 Ansible 庫的密鑰:

第一步

# 下載Ansible 庫
[greg@control ansible]$ wget //materials/salaries.yml

第二步

# 重設密碼
[greg@control ansible]$ ansible-vault rekey  /home/greg/ansible/salaries.yml 
Vault password: 'insecure8sure'
New Vault password: 'bbs2you9527'
Confirm New Vault password: 'bbs2you9527'
Rekey successful

第三步

# 驗證
[greg@control ansible]$ ansible-vault view  /home/greg/ansible/salaries.yml 
Vault password: 'bbs2you9527'
haha

第十六題:配置 cron 作業(增加)

配置 cron 作業

創建一個名為 /home/greg/ansible/cron.yml 的 playbook :

第一步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/cron.yml
---
- name: cron
  hosts: test
  tasks:
  - name: Ensure a job 
    cron:
      name: "check dirs"
      minute: "*/2"
      job: 'logger "EX200 in progress"'
      user: bob

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook cron.yml 

第三步

# 驗證
[greg@control ansible]$ ansible test -a "grep EX200 /var/log/cron"

ontrol ansible]$ ansible-vault rekey /home/greg/ansible/salaries.yml
Vault password: ‘insecure8sure’
New Vault password: ‘bbs2you9527’
Confirm New Vault password: ‘bbs2you9527’
Rekey successful


==第三步==

~~~sh
# 驗證
[greg@control ansible]$ ansible-vault view  /home/greg/ansible/salaries.yml 
Vault password: 'bbs2you9527'
haha

第十六題:配置 cron 作業(增加)

配置 cron 作業

創建一個名為 /home/greg/ansible/cron.yml 的 playbook :

第一步

# 創建playbook
[greg@control ansible]$ vim /home/greg/ansible/cron.yml
---
- name: cron
  hosts: test
  tasks:
  - name: Ensure a job 
    cron:
      name: "check dirs"
      minute: "*/2"
      job: 'logger "EX200 in progress"'
      user: bob

第二步

# 執行playbook
[greg@control ansible]$ ansible-playbook cron.yml 

第三步

# 驗證
[greg@control ansible]$ ansible test -a "grep EX200 /var/log/cron"
Tags: