红帽认证RHCE题目及答案分享(下),全面了解Ansible的生产实践

艺帆风顺 发布于 2025-04-07 18 次阅读


前一篇文章分享了1-8题:红帽认证RHCE题目及答案分享(上),全面了解Ansible的生产实践

今天分享一下9-16题

如果你是小白,请关注我,跟着李哥慢慢学!后续将对ansible的所有知识点进行详细讲解

9 生成主机文件

1、编写模板文件/home/student/ansible/hosts.j2 ,针对每个清单主机包含一行内容,其格式与 /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.254.10 servera.lab.example.com servera

172.25.254.11 serverb.lab.example.com serverb 172.25.254.12 serverc.lab.example.com serverc 172.25.254.13 serverd.lab.example.com serverd 172.25.250.254 bastion.lab.example.com bastion

参考答案:

$ wget hosts.j2
$ 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_enp0s1.ipv4.address }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}
{% endfor %}
$ vim hosts.yml
- name: create file
  hosts: dev
  tasks:
    template:
      src: hosts.j2
      dest: /etc/myhosts
$ ansible-playbook hosts.yml
$ ansible all -a “cat /root/myhosts”

10 修改文件内容

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

1、该 playbook 将在所有清单主机上运行

2、该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:

在 dev 主机组中的主机上,这行文本显示为:Development 在 test 主机组中的主机上,这行文本显示为:Test

在 prod 主机组中的主机上,这行文本显示为:Production

参考答案:

$ vim issue.yml
- name: config issue file
  hosts: all
  tasks:
   - name: write Development to dev
      copy:
        content: "Developmentn"
        dest: /etc/issue
      when: "'dev' in group_names"
    - name: write Test to test
      copy:
        content: "Testn"
        dest: /etc/issue
      when: "'test' in group_names"
    - name: write production to prod
      copy:
        content: "Productionn"
        dest: /etc/issue
      when: "'prod' in group_names"
$ ansible-playbook issue.yml
$ ansible all -a “cat /etc/issue”

11 创建Web内容目录

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

1、该 playbook 在 dev 主机组中的受管节点上运行

2、创建符合下列要求的目录 /webdev:

拥有组为 devops 组

具有常规权限:owner=read+write+execute,group=read+write+execute, other=read+execute

具有特殊权限: set group ID

3、用符号链接将 /var/www/html/webdev 链接到 /webdev

4、创建文件 /webdev/index.html,其中包含如下所示的单行文本:Development

5、在 dev 主机组中主机上浏览此目录(例如 http://servera.lab.example.com/webdev/ )将生成以 下输出:

Development

参考答案:

$ ansible dev -a "ls -lZ /var/www/html"
$ vim webcontent.yml
- name: config web content
  hosts: dev
  roles:
    - apache 
  tasks:
    - name: create group
      group:
        name: devops
        state: present
    - name: create dir
      file:
        path: /webdev
        state: directory
        owner: root
        group: devops
        mode: 2775
        setype: httpd_sys_content_t
    - name: write Development to index.hmtl
      copy:
        content: "Developmentn"
        dest: /webdev/index.html
        setype: httpd_sys_content_t
    - name: create soft link
      file:
      src: /webdev
      dest: /var/www/html/webdev
      state: link
# ansible-playbook webcontent.yml
# curl http://servera.lab.example.com/webdev/
# ansible dev -a "cat /webdev/index.html"

12 生成硬件报告

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

清单主机名称

以 MB 表示的总内存大小BIOS 版本

磁盘设备 vda 的大小磁盘设备 vdb 的大小

输出文件中的每一行含有一个 key=value 对。您的 playbook 应当:

1、从 http://172.25.254.254/content/hwreport.empty 下载文件,并将它保存为/root/hwreport.txt

2、使用正确的值修改 /root/hwreport.txt

3、如果硬件项不存在,相关的值应设为 NONE

参考答案:

$ ansible all -m setup grep mem
$ ansible all -m setup |grep biso
$ ansible all -m setup -a "filter=*.device*"
$ ansible-doc -l |grep -i download
$ ansible-doc get_url

$ vim hwreport.yml
- name: get hwreport info
  hosts: all
  tasks:
    - name: create report file
      get_url:
        url: http://172.25.254.254/content/hwreport.empty
        dest: /root/hwreport.txt
    - name: ensure 1
      replace:
        path: /root/hwreport.txt
        regexp: "inventoryhostname"
        replace: '{{ inventory_hostname }}'
    - name: ensure 2
      replace:
        path: /root/hwreport.txt
        regexp: "memory_in_MB"
        replace: "{{ ansible_memtotal_mb | string }}"
    - name: ensure 3
      replace:
        path: /root/hwreport.txt
        regexp: "bios_version"
        repalce: " {{ ansible_bios_version | default('NONE)' }}"
    - name: ensure 4
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vda_size"
        replace: "{{ ansible_device.vda.size | default('NONE)' }}"
    - name: ensure 5
      replace:
        path: /root/hwreport
        regexp: "disk_vdb_size"
        replace: "{{ ansible_device.vdb.size | default('NONE') }}"
$ ansible-playbook hwreport.yml
$ ansible all -a “cat /root/hwreport.txt”

13 创建密码库

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

1、库名称为  /home/student/ansible/locker.yml

2、库中含有两个变量,名称如下:

pw_developer,值为 Imadev

pw_manager,值为 Imamgr

3、用于加密和解密该库的密码为whenyouwishuponastar

4、密码存储在文件 /home/student/ansible/secret.txt中

参考答案:

$ vim ansible.cfg
...
vault_password_file: /home/student/ansible/secret.txt
echo whenyouwishuponastar > secret.txt
$ ansible-vault create locker.yml 或者:ansible-vault --vault-password-file=secret.txt encrypt locker.yml
pw_developer: Imadev
pw_manager: Imamgr
 
$ ansible-vault view locker.yml

14 创建用户账户

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,从而按以下所述创建用户帐户:职位描述为 developer 的用户应当:

在 dev 和 test 主机组中的受管节点上创建

从 pw_developer 变量分配密码是附加组 student 的成员

职位描述为 manager 的用户应当:

在 prod 主机组中的受管节点上创建从 pw_manager 变量分配密码

是附加组 opsmgr 的成员

3、密码应采用 SHA512 哈希格式。

4、您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件 /home/student/ansible/secret.txt 正常运行。

参考答案:

$ wget http://172.25.254.254/content/user_list.yml
$ vim users.yml
- name: create developer user_list
  hosts: dev,test
  vars_files:
    - locker.yml
    - user_list.yml
  tasks:
    - name: ensure student group
      group:
        name: student
         state: present
    - name: create user in developer
      user:
        name: "{{ item.name }}"
        groups: student
        password: "{{ pw_developer | password_hash('sha512') }}"
        append: yes
      loop: " {{ users }}"
      when: item.job == "developer"
- name: create manager user_list
  hosts: prod
  vars_files:
    - locker.yml
    - user_list.yml
  tasks:
    - name: ensure opsmgr group
      group:
        name: opsmgr
        state: present
    - name: create user in manager
      user:
        name: "{{ item.name }}"
        groups: opsmgr
        password: "{{ pw_manager | password_hash('sha512') }}"
        append: yes
      loop: "{{ users }}"
      when: item.job == "manager"
$ ansible-playbook users.yml
$ ansible dev,test -m shell -a "id bob; id sally; id fred"
$ ssh bob@172.25.250.9
Imadev
$ ansible prod -m shell -a "id bob; id sally; id fred"
$ ssh sally@172.25.250.12 

15 更新Ansible库的密钥

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

1、从 http://172.25.254.254/content/salaries.yml 下载 Ansible 库到 /home/student/ansible 2. 当前的库密码为 insecure4sure

2、新的库密码为 bbe2de98389b

3、库使用新密码保持加密状态

参考答案:

$ wget http://172.25.254.254/content/salaries.yml
$ ansible-vault rekey salaries.yml
旧密码:insecure4sure
新密码:bbe2de98389b
新密码确认:bbe2de98389b
$ ansible-vault view salaries.yml
$ ansible-vault edit salaries.yml

16 配置cron作业

创建一个名为/home/greg/ansible/cron.yml的playbook.

配置cron作业,该作业每隔2分钟运行并执行以下命令:

logger "EX294in progress",以用户natasha身份运行。

参考答案:

$ vim cron.yml
- name: config cron
  hosts: all
  tasks:
    - name: create user
      user:
        name: natasha
        state: present
    - name: config cron
      cron:
        name: "logger info"
        minute: "*/2"
        user: natasha
        job: logger "EX294 in progress"
$ ansible-playbook cron.yml
$ ansible all -m shell -a "crontab -l -u natasha"

如有帮助,请点个赞和“在看”!如有不足,敬请指出!感谢你的关注与支持。

路虽远,行则将至!

事虽难,做则必成!共勉!


往期精彩文章