RHCE 9版本考试要求
01.安装和配置 Ansible
安装和配置 Ansible
按照下⽅所述,在控制节点 control上安装和配置 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
- ⾃定义的collection⽬录在 /home/greg/ansible/mycollection
解题方法:
[虚拟机:foundation]
# 使用greg用户远程登陆到control
[kiosk@foundation0 -]$ ssh greg@control[虚拟机:172.25.250.254|control]
# 安装ansible
[greg@control ~]$ sudo yum -y install ansible-core ansible-navigator# 创建文件夹
[greg@control ~]$ mkdir -p /home/greg/ansible/roles# 切换工作目录
[greg@control ~]$ cd /home/greg/ansible# 生成ansible.cfg配置⽂件
[greg@control ansible]$ ansible-config init --disabled > /home/greg/ansible/ansible.cfg# 创建集合存储⽬录
[greg@control ansible]$ mkdir /home/greg/ansible/mycollection# 编辑配置⽂件
[greg@control ansible]$ vim ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
remote_user = greg
host_key_checking = False
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles
collections_path=./mycollection/:.ansible/collections:/usr/share/ansible/collections
[privilege_escalation]
become=True# 确认生效的配置文件(必做操作)
[greg@control ansible]$ ansible --version
[greg@control ansible]$ ansible-galaxy list# 创建 inventory 主机清单
[greg@control ansible]$ vim /home/greg/ansible/inventory
[dev]
node1
[test]
node2
[prod]
node3
node4
[balancers]
node5
[webservers:children]
prod# 验证: 如果可以ping通所有节点,证明配置⽂件、账户、清单都没有问题。(必做操作)
[greg@control ansible]$ ansible-inventory --graph
[greg@control ansible]$ ansible all -m ping#验证:此命令可以验证podman的登录、执行环境下载正确,查看集合。(必做操作,不做后影响后续题
目使用doc以及跑剧本)
[greg@control ansible]$ podman login utility.lab.example.com -u admin -p redhat
[greg@control ansible]$ ansible-navigator images
[greg@control ansible]$ ansible-navigator collections
02.配置您的系统以使⽤默认存储库
配置您的系统以使用默认存储库
作为系统管理员,您需要在受管节点上安装软件。
请按照正⽂所述,创建⼀个名为/home/greg/ansible/yum_repo.yml ,在各个受管节点上安装 yum 存储库
存储库1:
- 存储库的名称为 EX294_BASE
- 描述为 EX294 base software
- 基础 URL 为 http://content/rhel9.0/x86_64/dvd/BaseOS
- GPG 签名检查为 启用状态
- GPG 密钥 URL 为 http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库为 启用状态
存储库2:
- 存储库的名称为 EX294_STREAM
- 描述为 EX294 stream software
- 基础 URL 为 http://content/rhel9.0/x86_64/dvd/AppStream
- GPG 签名检查为 启⽤状态
- GPG 密钥 URL 为 http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库为 启⽤状态
解题方法:
[虚拟机:172.25.250.254|control]
# 确认各个受管节点all
[greg@control ansible]$ ansible-inventory --graph# 查模块名称
[greg@control ansible]$ ansible-doc -l | grep yum# 查模块⽤法。==/EX==,在⼿册中搜索 EXAMPLE 示例
[greg@control ansible]$ ansible-doc yum_repository# 设置vim编辑器的缩进格式
[greg@control ansible]$ echo set nu ts=2 et cuc sw=2 autoindent > ~/.vimrc# 编辑剧本
[greg@control ansible]$ vim /home/greg/ansible/yum_repo.yml
---
- name: create repositoryhosts: alltasks:- name: add BaseOS repoyum_repository:name: EX294_BASEdescription: "EX294 base software"baseurl: http://content/rhel9.0/x86_64/dvd/BaseOSgpgcheck: yesgpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-releaseenabled: yes- name: add AppStream repoyum_repository:name: EX294_STREAMdescription: "EX294 stream software"baseurl: http://content/rhel9.0/x86_64/dvd/AppStreamgpgcheck: yesgpgkey: http://content/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-releaseenabled: yes# 运行剧本 (Ansible新的剧本运行方式,必须做第一题的44-47行命令)
[greg@control ansible]$ ansible-navigator run yum_repo.yml -m stdout# 验证两个仓库(BaseOS, AppStream, 必做操作)
# 验证:验证所有节点,若软件安装完毕,证明配置成功。
[greg@control ansible]$ ansible all -a 'yum repoinfo'
[greg@control ansible]$ ansible all -a 'yum -y install ftp'
[greg@control ansible]$ ansible all -a 'rpm -q ftp'
03.安装软件包
安装软件包
创建⼀个名为 /home/greg/ansible/packages.yml 的 playbook :
- 将 php 和 mariadb 软件包安装到 dev 、 test 和 prod 主机组中的主机上
- 将 RPM Development Tools 软件包组安装到 dev 主机组中的主机上
- 将 dev 主机组中主机上的 所有软件包更新为最新版本
解题方法:
[虚拟机:172.25.250.254|control][greg@control ansible]$ ansible-doc -l | grep yum
[greg@control ansible]$ ansible-doc yum
[greg@control ansible]$ vim /home/greg/ansible/packages.yml
---
- name: install1hosts: dev,test,prodtasks:- name: install packagesansible.builtin.yum:name: php,mariadbstate: present
- name: install2hosts: devtasks:- name: install groupansible.builtin.yum:name: "@RPM Development Tools"state: present- name: upgrade all packagesansible.builtin.yum:name: "*"state: latest# 验证:通过查看软件安装情况,确认题目正确。(必做操作)
[greg@control ansible]$ ansible-navigator run packages.yml -m stdout[greg@control ansible]$ ansible dev,test,prod -a 'rpm -q php mariadb'[greg@control ansible]$ ansible dev -a 'yum grouplist'
...
Installed Groups:
RPM Development Tools[greg@control ansible]$ ansible dev -a 'yum update'
...
Nothing to do.
04.使用 RHEL 系统角色
使⽤ RHEL 系统⻆⾊
安装 RHEL 系统⻆⾊软件包,并创建符合以下条件的 playbook
/home/greg/ansible/selinux.yml :
- 在 所有受管节点 上运⾏
- 使⽤ selinux ⻆⾊
- 配置该⻆⾊,配置被管理节点的 selinux 为 enforcing
解题方法:
[虚拟机:172.25.250.254|control]# 安装rhel-system-roles
[greg@control ansible]$ sudo yum -y install rhel-system-roles
[greg@control ansible]$ ansible-galaxy list# 指定roles的位置
[greg@control ansible]$ vim ansible.cfg
...
# additional paths to search for roles in, `colon` separated
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles# 查看角色
[greg@control ansible]$ ansible-galaxy list# 复制现有实例文件,拿来使用
[greg@control ansible]$ cp /usr/share/doc/rhel-system-roles/selinux/exampleselinux-playbook.yml /home/greg/ansible/selinux.yml
[greg@control ansible]$ vim /home/greg/ansible/selinux.yml
删除操作 :23,30d :9,19d
---
- hosts: allvars:selinux_policy: targetedselinux_state: enforcing
# prepare prerequisites which are used in this playbooktasks:- name: execute the role and catch errorsblock:- include_role:name: rhel-system-roles.selinuxrescue:
# Fail if failed for a different reason than selinux_reboot_required.- name: handle errorsfail:msg: "role failed"when: not selinux_reboot_required- name: restart managed hostshell: sleep 2 && shutdown -r now "Ansible updates triggered"async: 1poll: 0ignore_errors: true- name: wait for managed host to come backwait_for_connection:delay: 10timeout: 300- name: reapply the roleinclude_role:name: rhel-system-roles.selinux# 使用rpm包安装的角色,还是用ansible-playbook执行
# 使用集合安装的角色,要用ansible-navigator执行
[greg@control ansible]$ ansible-playbook selinux.yml# 验证:结果应为所有节点状态为Enforcing,
# 且配置⽂件/etc/selinux/config内为SELINUX=enforcing(必做操作)
[greg@control ansible]$ ansible all -m shell -a 'grep ^SELINUX=/etc/selinux/config; getenforce'
node2 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node4 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node5 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node3 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
node1 | CHANGED | rc=0 >>
SELINUX=enforcing
Enforcing
05.配置conllection
配置 conllection
- http://classroom/materials/
-
- redhat-insights-1.0.7.tar.gz
- community-general-5.5.0.tar.gz
- redhat-rhel_system_roles-1.19.3.tar.gz
- 上面3个安装在 /home/greg/ansible/mycollection 目录中
解题方法:
[虚拟机:172.25.250.254|control]
[greg@control ansible]$ vim requirements.yml
---
collections:
- name: http://classroom/materials/redhat-insights-1.0.7.tar.gz
- name: http://classroom/materials/community-general-5.5.0.tar.gz
- name: http://classroom/materials/redhat-rhel_system_roles-1.19.3.tar.gz[greg@control ansible]$ ansible-galaxy collection install -r requirements.yml -p /home/greg/ansible/mycollection# 验证(必做)
[greg@control ansible]$ ansible-navigator collections
[greg@control ansible]$ ansible-navigator doc community.general.filesystem -m stdout
06.使用 Ansible Galaxy 安装角色
使⽤ Ansible Galaxy 安装角色
使⽤ Ansible Galaxy 和要求⽂件 /home/greg/ansible/roles/requirements.yml 。从以下 URL 下载角色并安装到 /home/greg/ansible/roles :
- http://classroom/materials/haproxy.tar 此⻆⾊的名称应当为 balancer
- http://classroom/materials/phpinfo.tar 此⻆⾊的名称应当为 phpinfo
解题方法:
[虚拟机:172.25.250.254|control][greg@control ansible]$ vim /home/greg/ansible/roles/requirements.yml
---
- src: http://classroom/materials/haproxy.tar
name: balancer
- src: http://classroom/materials/phpinfo.tar
name: phpinfo
[greg@control ansible]$ ansible-galaxy install -r /home/greg/ansible/roles/requirements.yml#验证:查看到两个⻆⾊即可。(必做操作)
[greg@control ansible]$ ansible-galaxy list
# /home/greg/ansible/roles
...
- balancer, (unknown version)
- phpinfo, (unknown version)
...
07.创建和使用角色
创建和使用角色
根据下列要求,在 /home/greg/ansible/roles 中创建名为 apache 的⻆⾊:
- httpd 软件包已安装,设为在 系统启动时启动 并 启动
- 防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则
- 模板文件件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html :
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的 完全限定域名 , IPADDRESS 则是受管节点的 IP 地址。
创建⼀个名为 /home/greg/ansible/apache.yml 的 playbook:
- 该 play 在 webservers 主机组中的主机上运⾏并将使⽤ apache ⻆⾊
解题方法:
[虚拟机:172.25.250.254|control]
[greg@control ansible]$ ansible-galaxy role init --init-path /home/greg/ansible/roles apache[greg@control ansible]$ ansible-galaxy list[greg@control ansible]$ vim roles/apache/tasks/main.yml
---
- name: Install Apacheansible.builtin.yum:name: httpdstate: latest- name: Start httpdansible.builtin.systemd:name: httpdstate: startedenabled: yes- name: Start firewalldansible.builtin.systemd:name: firewalldstate: startedenabled: yes- name: Start apacheansible.posix.firewalld:# 注意防火墙中Apache服务名不是httpdservice: httppermanent: yesstate: enabledimmediate: yes- template:# path = roles/apache/templates/src: index.html.j2dest: /var/www/html/index.html[greg@control ansible]$ vim roles/apache/templates/index.html.j2
Welcome to {{ ansible_nodename }} on {{ ansible_default_ipv4.address }}[greg@control ansible]$ vim /home/greg/ansible/apache.yml
---
- name: webservers rolehosts: webserversroles:- apache[greg@control ansible]$ ansible-navigator run apache.yml -m stdout[greg@control ansible]$ ansible webservers --list-hosts
hosts (2):
node3
node4[虚拟机:foundation]
# 验证(必做操作)
#验证:通过访问node3、4返回网页结果(可用命令行或浏览器),判断角色是否执行正常。
[kiosk@foundation0 -]$ curl http://node3
Welcome to node3.lab.example.com on 172.25.250.11
[kiosk@foundation0 -]$ curl http://node4
Welcome to node4.lab.example.com on 172.25.250.12
08.从 Ansible Galaxy 使用角色
从 Ansible Galaxy 使用角色
根据下列要求,创建⼀个名为 /home/greg/ansible/roles.yml 的 playbook :
- playbook 中包含一个 play, 该 play 在 balancers 主机组中的主机上运行并将使用balancer 角色。
-
- 此角色配置一项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。
- 浏览到 balancers 主机组中的主机(例如 http://172.25.250.13 )将生成以下输出:
Welcome to node3.lab.example.com on 172.25.250.11 - 重新加载浏览器将从另一 Web 服务器生成输出:
Welcome to node4.lab.example.com on 172.25.250.12
- playbook 中包含一个 play, 该 play 在 webservers 主机组中的主机上运行并将使用 phpinfo 角色。
-
- 请通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
Hello PHP World from FQDN - 其中,FQDN 是主机的完全限定名称。
Hello PHP World from node3.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。
- 请通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:
- 同样,浏览到 http://172.25.250.12/hello.php 会生成以下输出:
Hello PHP World from node4.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等
解题方法:
[greg@control ansible]$ vim /home/greg/ansible/roles.yml
---
- name: Use role phpinfohosts: webserversroles:- phpinfo
- name: Use role balancerhosts: balancersroles:- balancer[greg@control ansible]$ ansible-navigator run roles.yml -m stdout
09.创建和使用逻辑卷
创建和使用逻辑卷
创建⼀个名为 /home/greg/ansible/lv.yml 的 playbook ,它将在 所有受管节点 上运行以执行下列任务:
- 创建符合以下要求的逻辑卷:
-
- 逻辑卷创建在 research 卷组中
- 逻辑卷名称为 data
- 逻辑卷大小为 1500 MiB
- 使用 ext4 文件系统格式化逻辑卷
- 如果无法创建请求的逻辑卷大小,应显示错误信息,并且应改为使用大小 800MiB。
Could not create logical volume of that size - 如果卷组 research 不存在,应显示错误信息
Volume group done not exist - 不要以任何方式挂载逻辑卷
解题方法:
[greg@control ansible]$ ansible-navigator doc community.general.lvol -m stdout[greg@control ansible]$ ansible-navigator doc community.general.filesystem -m stdout[greg@control ansible]$ ansible-doc debug[greg@control ansible]$ ansible-doc stat[greg@control ansible]$ vim /home/greg/ansible/lv.yml
---
- name: Create LVMhosts: alltasks:- block:- name: lv 1500Mcommunity.general.lvol:vg: researchlv: datasize: 1500- name: Create ext4community.general.filesystem:fstype: ext4dev: /dev/research/datarescue:- name: Could not create lvmansible.builtin.debug:msg: Could not create logical volume of that size- name: lv 800Mcommunity.general.lvol:vg: researchlv: datasize: 800- name: Create ext4community.general.filesystem:fstype: ext4dev: /dev/research/datawhen: ansible_lvm.vgs.research is defined- debug:msg: Volume group done not existwhen: ansible_lvm.vgs.research is not defined[greg@control ansible]$ ansible-navigator run /home/greg/ansible/lv.yml -m stdout# 观察ansible剧本执⾏过程,是否符合题意,结合blkid命令查询(必做操作)
[greg@control ansible]$ ansible all -m shell -a 'lvs'
[greg@control ansible]$ ansible all -m shell -a 'blkid /dev/research/data'
10.生成主机文件
生成主机文件
- 将一个初始模板文件从 http://classroom/materials/hosts.j2 下载到/home/greg/ansible
- 完成该模板,以便用它生成以下文件:针对每个清单主机包含一行内容,其格式与/etc/hosts 相同
- 将一个剧本从 http://classroom/materials/hosts.yml 下载到/home/greg/ansible ,它将使用此模板在 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.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]$ ansible dev -m debug -a 'var=groups'
[greg@control ansible]$ ansible dev -m debug -a "var=groups['all']"
[greg@control ansible]$ wget http://classroom/materials/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 i in groups['all'] %}
{{ hostvars[i].ansible_facts.default_ipv4.address }} {{ hostvars[i].ansible_fact
{% endfor %}
[greg@control ansible]$ vim hosts.yml
---
- name: gen host filehosts: alltasks:- name: Template a file to /etc/myhoststemplate:src: /home/greg/ansible/hosts.j2dest: /etc/myhostswhen: inventory_hostname in groups.dev[greg@control ansible]$ ansible-navigator run hosts.yml -m stdout
# 验证(必做操作)
[greg@control ansible]$ ansible dev -m shell -a 'cat /etc/myhosts'
11.修改文件内容
修改文件内容
按照下⽅所述,创建⼀个名为 /home/greg/ansible/issue.yml 的 playbook :
- 该 playbook 将在 所有清单主机 上运行
- 该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:
-
- 在 dev 主机组中的主机上,这行文本显示为: Development
- 在 test 主机组中的主机上,这行文本显示为: Test
- 在 prod 主机组中的主机上,这行文本显示为: Production
解题方法:
[greg@control ansible]$ ansible dev -m debug -a 'var=inventory_hostname'
[greg@control ansible]$ ansible dev -m debug -a 'var=groups'
[greg@control ansible]$ ansible dev -m debug -a 'var=groups.dev'
[greg@control ansible]$ ansible-doc copy
[greg@control ansible]$ ansible-doc stat
[greg@control ansible]$ vim /home/greg/ansible/issue.yml
---
- name: modify filehosts: alltasks:- name: Content 1ansible.builtin.copy:content: 'Development'dest: /etc/issuewhen: inventory_hostname in groups.dev- name: Content 2ansible.builtin.copy:content: 'Test'dest: /etc/issuewhen: inventory_hostname in groups.test- name: Content 3ansible.builtin.copy:content: 'Production'dest: /etc/issuewhen: inventory_hostname in groups.prod[greg@control ansible]$ ansible-navigator run issue.yml -m stdout# 验证(必做操作)
[greg@control ansible]$ ansible dev -a 'cat /etc/issue'
[greg@control ansible]$ ansible test -a 'cat /etc/issue'
[greg@control ansible]$ ansible prod -a 'cat /etc/issue'
12.创建 Web 内容目录
创建 Web 内容目录
按照下方所述,创建⼀个名为 /home/greg/ansible/webcontent.yml 的 playbook :
- 该 playbook 在 dev 主机组中的受管节点上运行
- 创建符合下列要求的目录 /webdev :
-
- 所有者为 webdev 组
- 具有常规权限:
owner=read+write+execute,
group=read+write+execute,
other=read+execute - 具有 特殊权限 :设置组 ID
- 用符号链接将 /var/www/html/webdev 链接到 /webdev
- 创建文件 /webdev/index.html ,其中包含如下所示的单行文件: Development
- 在 dev 主机组中主机上浏览此目录(例如 http://172.25.250.9/webdev/ )将生成以下输出:
Development
解题方法:
[greg@control ansible]$ ansible-doc file
[greg@control ansible]$ ansible-doc copy
[greg@control ansible]$ ansible dev -a 'ls -ldZ /var/www/html'
-rw-r--r--. 1 root root system_u:object_r:`httpd_sys_content_t`:s0 11 Apr 14 07:11 /var/www/html[greg@control ansible]$ vim /home/greg/ansible/webcontent.yml
---
- name: Create Web Directoryhosts: devroles:- apachetasks:- name: Create directoryansible.builtin.file:path: /webdevstate: directorygroup: webdevmode: '2775'- name: Create linkansible.builtin.file:src: /webdevdest: /var/www/html/webdevstate: link- name: Copy contentansible.builtin.copy:content: 'Development'dest: /webdev/index.htmlsetype: httpd_sys_content_t[greg@control ansible]$ ansible-navigator run webcontent.yml -m stdout
13.生成硬件报告
生成硬件报告
创建⼀个名为 /home/greg/ansible/hwreport.yml 的 playbook ,它将在所有受管节点上⽣成含有以下 信息的输出⽂件 /root/hwreport.txt :
- 清单主机名称
- 以 MB 表示的 总内存大小
- BIOS 版本
- 磁盘设备 vda 的大小
- 磁盘设备 vdb 的大小
- 输出文件中的每一行含有一个 key=value 对
您的 playbook 应当:
- 从 http://classroom/materials/hwreport.empty 下载文件,并将它保存为/root/hwreport.txt
- 使用 正确的值 改为 /root/hwreport.txt
- 如果硬件项不存在,相关的值应设为 NONE
解题方法:
[greg@control ansible]$ ansible all -m setup | grep mem
[greg@control ansible]$ ansible all -m setup | grep bios
[greg@control ansible]$ ansible all -m setup -a 'filter=*device*'
[greg@control ansible]$ curl http://materials/hwreport.empty[greg@control ansible]$ vim /home/greg/ansible/hwreport.yml
---
- name: Hardware reporthosts: alltasks:- name: Download hwreportansible.builtin.get_url:url: http://materials/hwreport.emptydest: /root/hwreport.txt- name: Report 1ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: '^HOST='line: HOST={{ inventory_hostname }}- name: Report 2ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: '^MEMORY='line: MEMORY={{ ansible_memtotal_mb }}- name: Report 3ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: '^BIOS='line: BIOS={{ ansible_bios_version }}- name: Report 4ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: '^DISK_SIZE_VDA='line: DISK_SIZE_VDA={{ ansible_devices.vda.size }}- name: Report 5ansible.builtin.lineinfile:path: /root/hwreport.txtregexp: '^DISK_SIZE_VDB='line: DISK_SIZE_VDB={{ ansible_devices.vdb.size | default('NONE', true) }}# 验证: 收集到的报告应和各个主机的事实对⽐结果⼀致即可,请自行核对。(必做操作)
[greg@control ansible]$ ansible-navigator run hwreport.yml -m stdout
[greg@control ansible]$ ansible all -a 'cat /root/hwreport.txt'
14.创建密码库
创建密码库
按照下方所述,创建⼀个 Ansible 库来存储用户密码:
- 库名称为 /home/greg/ansible/locker.yml
- 库中含有两个变量,名称如下:
-
- pw_developer ,值为 Imadev
- pw_manager ,值为 Imamgr
- 用于加密和解密该库的密码为 whenyouwishuponastar
-
- 密码存储在文件 /home/greg/ansible/secret.txt 中
解题方法:
[greg@control ansible]$ vim ansible.cfg
...
vault_password_file = /home/greg/ansible/secret.txt[greg@control ansible]$ echo whenyouwishuponastar > /home/greg/ansible/secret.txt
[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#验证:根据推荐指令验证到文件内容被加密,且可以通过配置⽂件secret.txt⽂件⾃动解密即可。
(必做操作)
[greg@control ansible]$ cat /home/greg/ansible/locker.yml
$ANSIBLE_VAULT;1.1;AES256
3863666662376132653636383666306664633665636166366465323533396130663431393932663
1
3038366162383733643633383935663431376163646639350a39343666356636663130306465393
3
6337303063633334336262313065363336363033646164626236323964333535346665353464313
5
6162383733353561640a38333362313764656566663636306434336134653739663365323430626
3
3332613836613538343064376365393166373865663339393334346661623533363138343137313
5
666161303763323761383731343138373166653730613932343
15.创建⽤户帐户
创建用户帐户
- 从 http://classroom/materials/user_list.yml 下载要创建的用户的列表,并将它保存到 /home/greg/ansible
- 在本次练习中使用在其他位置创建的密码库 /home/greg/ansible/locker.yml 。创建名为 /home/greg/ansible/users.yml 的 playbook ,从而按以下所述创建用户帐户:
-
- 职位描述为 developer 的用户应当:
-
-
- 在 dev 和 test 主机组中的受管节点上创建
- 从 pw_developer 变量分配密码
- 密码最长有效期 30 天
- 是补充组 devops 的成员
-
-
- 职位描述为 manager 的用户应当:
-
-
- 在 prod 主机组中的受管节点上创建
- pw_manager 变量分配密码
- 密码最长有效期 30 天
- 是补充组 opsmgr 的成员
-
- 密码采用 sha512 哈希格式。
- 您的 playbook 应能够在本次练习中使用在其他位置创建的库密码文件/home/greg/ansible/secret.txt 正常运行。
解题方法:
[greg@control ansible]$ wget http://classroom/materials/user_list.yml
[greg@control ansible]$ cat user_list.yml[greg@control ansible]$ vim /home/greg/ansible/users.yml
---
- name: Create User1hosts: dev,testvars_files:- /home/greg/ansible/locker.yml- /home/greg/ansible/user_list.ymltasks:- name: Add group1group:name: devopsstate: present- name: Add user1user:name: "{{ item.name }}"groups: devopspassword: "{{ pw_developer | password_hash('sha512') }}"password_expire_max: "{{ item.password_expire_max }}"loop: "{{ users }}"when: item.job == 'developer'
- name: Create User2hosts: prodvars_files:- /home/greg/ansible/locker.yml- /home/greg/ansible/user_list.ymltasks:- name: Add group2group:name: opsmgrstate: present- name: Add user2user:name: "{{ item.name }}"groups: opsmgrpassword: "{{ pw_manager | password_hash('sha512') }}"password_expire_max: "{{ item.password_expire_max }}"loop: "{{ users }}"when: item.job == 'manager'[greg@control ansible]$ ansible-navigator run users.yml -m stdout# 验证: 确保 dev,test 上有bob、fred 且与题目要求⼀致(必做操作)
# 确保 pord(node3 node4) 上有 sally 且与题目要求⼀致(必做操作)
[greg@control ansible]$ ansible dev,test -m shell -a 'id bob; id sally; id fred'
[greg@control ansible]$ ansible prod -m shell -a 'id fred; id bob; id sally'[greg@control ansible]$ ssh bob@172.25.250.9
bob@172.25.250.9\'s password: `Imadev`
<Ctrl-D>
[greg@control ansible]$ ssh sally@172.25.250.12
sally@172.25.250.12\'s password: `Imamgr`
<Ctrl-D>
16.更新 Ansible 库的密钥
更新 Ansible 库的密钥
按照下⽅所述,更新现有 Ansible 库的密钥:
- 从 http://classroom/materials/salaries.yml 下载 Ansible 库到/home/greg/ansible
- 当前的库密码为 insecure8sure
- 新的库密码为 bbs2you9527
- 库使用新密码保持加密状态
解题方法:
[greg@control ansible]$ wget http://classroom/materials/salaries.yml
[greg@control ansible]$ ansible-vault rekey --ask-vault-pass salaries.yml
Vault password: `insecure8sure`
New Vault password: `bbs2you9527`
Confirm New Vault password: `bbs2you9527`
Rekey successful[greg@control ansible]$ ansible-vault view salaries.yml
Vault password: `bbs2you9527`
haha# 验证: ⽤新密码验证是否可以解密查看内容。(必做操作)
[greg@control ansible]$ ansible-vault view --ask-vault-pass salaries.yml
Vault password: `bbs2you9527`
haha
17.配置 cron 作业
配置 cron 作业
创建⼀个名为 /home/greg/ansible/cron.yml 的 playbook :
- 该 playbook 在 test 主机组中的受管节点上运行
- 配置 cron 作业,该作业 每隔 2 分钟 运行并执行以下命令: logger "EX200 in progress" ,以用户 natasha 身份运行
解题方法:
[greg@control ansible]$ ansible-doc -l | grep cron
[greg@control ansible]$ ansible-doc cron[greg@control ansible]$ vim /home/greg/ansible/cron.yml
---
- name: cronhosts: testtasks:- name: cron jobcron:name: "cron job1"minute: "*/2"job: 'logger "EX200 in progress"'user: natasha[greg@control ansible]$ ansible-navigator run cron.yml -m stdout# 验证:通过crontab -l -u natasha命令验证任务是否存在并正确。(必做操作)
[greg@control ansible]$ ansible test -a 'crontab -l -u natasha'
node2 | CHANGED | rc=0 >>
#Ansible: check dirs*/2 * * * * logger "EX200 in progress"