安装和配置 Ansible

按照下方所述,在控制节点 172.25.250.254 上安装和配置 Ansible:安装所需的软件包创建名为 /home/greg/ansible/inventory 的静态清单文件,以满足以下要求:172.25.250.9 是 dev 主机组的成员172.25.250.10 是 test 主机组的成员172.25.250.11 和 172.25.250.12 是 prod 主机组的成员172.25.250.13 是 balancers 主机组的成员prod 组是 webservers 主机组的成员创建名为 /home/greg/ansible/ansible.cfg 的配置文件,以满足以下要求:主机清单文件为 /home/greg/ansible/inventoryplaybook 中使用的角色的位置包括 /home/greg/ansible/roles**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[greg@bastion ~]$ sudo yum install -y ansible
[greg@bastion ~]$ mkdir -p /home/greg/ansible
[greg@bastion ~]$ cd /home/greg/ansible
[greg@bastion ansible]$ vim inventory
[dev]
172.25.250.9
[test]
172.25.250.10
[prod]
172.25.250.11
172.25.250.12
[balancers]
172.25.250.13
[webservers:children]
prod
[all:vars]
ansible_user=root
ansible_password=redhat
[greg@bastion ansible]$ cp /etc/ansible/ansible.cfg ./
[greg@bastion ansible]$ mkdir roles
[greg@bastion ansible]$ vim /home/greg/ansible/ansible.cfg
总共只修改四行
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
remote_user = root
[greg@bastion ansible]$ ansible --version
[greg@bastion ansible]$ ansible-inventory --graph

创建和运行 Ansible 临时命令

作为系统管理员,您需要在受管节点上安装软件。请按照正文所述,创建一个名为 /home/greg/ansible/adhoc.sh 的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库:存储库1:存储库的名称为 EX294_BASE描述为 EX294 base software基础 URL 为 http://content/rhel8.0/x86_64/dvd/BaseOSGPG 签名检查为启用状态GPG 密钥 URL 为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release存储库为启用状态存储库2:存储库的名称为 EX294_STREAM描述为 EX294 stream software基础 URL 为 http://content/rhel8.0/x86_64/dvd/AppStreamGPG 签名检查为启用状态GPG 密钥 URL 为 http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release存储库为启用状态**
解题方法:

1
2
3
4
5
6
[greg@bastion ansible]$ ansible-doc yum_repository
[greg@bastion ansible]$ vim adhoc.sh
#!/bin/bash
ansible all -m yum_repository -a 'name="EX294_BASE" description="EX294 base software" baseurl="http://content/rhel8.0/x86_64/dvd/BaseOS" gpgcheck=yes enabled=1 gpgkey="http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release"'
ansible all -m yum_repository -a 'name="EX294_STREAM" description="EX294 stream software" baseurl="http://content/rhel8.0/x86_64/dvd/AppStream" gpgcheck=yes enabled=1 gpgkey="http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release"'
[greg@bastion ansible]$ chmod +x adhoc.sh

安装软件包

创建一个名为 /home/greg/ansible/packages.yml 的 playbook :将 php 和 mariadb 软件包安装到 dev、test 和 prod 主机组中的主机上将 RPM Development Tools 软件包组安装到 dev 主机组中的主机上将 dev 主机组中主机上的所有软件包更新为最新版本**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---
- name: 安装软件包A
hosts: dev,test,prod
tasks:
- name: one
yum:
name: php
state: latest
- name: two
yum:
name: mariadb
state: latest
- name: 安装软件包B
hosts: dev
tasks:
- name: one
yum:
name: "@RPM Development Tools"
state: latest
- name: two
yum:
name: "*"
state: latest

使用 RHEL 系统角色

安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/greg/ansible/timesync.yml :在所有受管节点上运行使用 timesync 角色配置该角色,以使用当前有效的 NTP 提供商配置该角色,以使用时间服务器 172.25.254.254配置该角色,以启用 iburst 参数**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[greg@bastion ansible]$ sudo yum install rhel-system-roles
[greg@bastion ansible]$ vim ansible.cfg
仅修改一行
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles
[greg@bastion ansible]$ ansible-galaxy list
[greg@bastion ansible]$ cp /usr/share/doc/rhel-system-roles/timesync/example-timesync-playbook.yml timesync.yml
[greg@bastion ansible]$ vim timesync.yml
---
- hosts: all
vars:
timesync_ntp_servers:
- hostname: 172.25.254.254
iburst: yes
roles:
- rhel-system-roles.timesync

使用 Ansible Galaxy 安装角色

使用 Ansible Galaxy 和要求文件 /home/greg/ansible/roles/requirements.yml 。从以下 URL 下载角色并安装到 /home/greg/ansible/roles :http://materials/haproxy.tar 此角色的名称应当为 balancerhttp://materials/phpinfo.tar 此角色的名称应当为 phpinfo**
解题方法:

1
2
3
4
5
6
7
8
9
10
[greg@bastion ansible]$ mkdir roles
[greg@bastion ansible]$ cd roles
[greg@bastion roles]$ vim requirements.yml
---
- src: http://materials/haproxy.tar
name: balancer
- src: http://materials/phpinfo.tar
name: phpinfo
[greg@bastion roles]$ ansible-galaxy install -r requirements.yml
[greg@bastion roles]$ ansible-galaxy list

创建和使用角色

根据下列要求,在 /home/greg/ansible/roles 中创建名为 apache 的角色:httpd 软件包已安装,设为在系统启动时启用并启动防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html :Welcome to HOSTNAME on IPADDRESS其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[greg@bastion roles]$ ansible-galaxy init apache

[greg@bastion roles]$ vim apache/tasks/main.yml
---
- name: one
yum:
name: httpd
state: latest
- name: two
service:
name: httpd
state: started
enabled: yes
- name: two2
service:
name: firewalld
state: started
enabled: yes
- name: three
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: four
template:
src: index.html.j2
dest: /var/www/html/index.html

[greg@bastion roles]$ vim apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}

从 Ansible Galaxy 使用角色

根据下列要求,创建一个名为 /home/greg/ansible/roles.yml 的 playbook :playbook 中包含一个 play, 该 play 在 balancers 主机组中的主机上运行并将使用 balancer 角色。此角色配置一项服务,以在 webservers 主机组中的主机之间平衡 Web 服务器请求的负载。浏览到 balancers 主机组中的主机(例如 http://172.25.250.13 )将生成以下输出:Welcom to serverb.lab.example.com on 172.25.250.11重新加载浏览器将从另一 Web 服务器生成输出:Welcom to serverc.lab.example.com on 172.25.250.12playbook 中包含一个 play, 该 play 在 webservers 主机组中的主机上运行并将使用 phpinfo 角色。请通过 URL /hello.php 浏览到 webservers 主机组中的主机将生成以下输出:Hello PHP World from FQDN其中,FQDN 是主机的完全限定名称。Hello PHP World fromserverb.lab.example.com另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。同样,浏览到 http://172.25.250.12/hello.php 会生成以下输出:Hello PHP World fromserverc.lab.example.com另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[greg@bastion ansible]$ vim roles.yml
---
- name: one
hosts: webservers
roles:
- phpinfo
- name: two
hosts: balancers
roles:
- balancer
- name: three
hosts: webservers
roles:
- apache

创建和使用逻辑卷

创建一个名为 /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。不要以任何方式挂载逻辑卷**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[greg@bastion ansible]$ vim lv.yml
---
- name: 创建和使用逻辑卷
hosts: all
tasks:
- block:
- name: one
lvol:
vg: research
lv: data
size: 1500
- name: two
filesystem:
fstype: ext4
dev: /dev/research/data
rescue:
- debug:
msg: Could not create logical volume of that size
- name: three
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 undefined

生成主机文件

将一个初始模板文件从 http://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.25.250.9 workstation.lab.example.com workstation172.25.250.10 servera.lab.example.com servera172.25.250.11 serverb.lab.example.com serverb172.25.250.12 serverc.lab.example.com serverc172.25.250.13 serverd.lab.example.com serverd注:清单主机名称的显示顺序不重要。**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[greg@bastion ansible]$ wget http://materials/hosts.j2

[greg@bastion ansible]$ 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 %}

[greg@bastion ansible]$ vim hosts.yml
---
- name: 生成主机文件
hosts: all
tasks:
- name: one
template:
src: /home/greg/ansible/hosts.j2
dest: /etc/myhosts
when: "inventory_hostname in groups.dev""

修改文件内容

按照下方所述,创建一个名为 /home/greg/ansible/issue.yml 的 playbook :该 playbook 将在所有清单主机上运行该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:在 dev 主机组中的主机上,这行文本显示 为:Development在 test 主机组中的主机上,这行文本显示 为:Test在 prod 主机组中的主机上,这行文本显示 为:Production**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[greg@bastion ansible]$ vim issue.yml
---
- name: 修改文件内容
hosts: all
tasks:
- name: one
copy:
content: 'Development'
dest: /etc/issue
when: "inventory_hostname in groups.dev"
- name: two
copy:
content: 'Test'
dest: /etc/issue
when: "inventory_hostname in groups.test"
- name: three
copy:
content: 'Production'
dest: /etc/issue
when: "inventory_hostname in groups.prod"

创建 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**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[greg@bastion ansible]$ vim webcontent.yml
---
- name: 创建 Web 内容目录
hosts: dev
tasks:
- name: one
file:
path: /webdev
state: directory
group: webdev
mode: '2775'
- name: two
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: three
copy:
content: 'Development'
dest: /webdev/index.html
setype: httpd_sys_content_t

生成硬件报告

创建一个名为 /home/greg/ansible/hwreport.yml 的 playbook ,它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt :清单主机名称以 MB 表示的总内存大小BIOS 版本磁盘设备 vda 的大小磁盘设备 vdb 的大小输出文件中的每一行含有一个 key=value 对。您的 playbook 应当:从 http://materials/hwreport.empty 下载文件,并将它保存为 /root/hwreport.txt使用正确的值改为 /root/hwreport.txt如果硬件项不存在,相关的值应设为 NONE**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[greg@bastion ansible]$ vim hwreport.yml
---
- name: 生成硬件报告
hosts: all
tasks:
- name: one
get_url:
url: http://materials/hwreport.empty
dest: /root/hwreport.txt
- name: two1
lineinfile:
path: /root/hwreport.txt
regexp: "^HOST"
line: HOST={{ inventory_hostname | default( 'NONR',true ) }}
- name: two2
lineinfile:
path: /root/hwreport.txt
regexp: "^MEMORY"
line: MEMORY={{ ansible_memtotal_mb | default( 'NONR',true ) }}
- name: two3
lineinfile:
path: /root/hwreport.txt
regexp: "^BIOS"
line: BIOS={{ ansible_biod_version | default( 'NONR',true ) }}
- name: two4
lineinfile:
path: /root/hwreport.txt
regexp: "^"
line: HOST={{ ansible_devices.vda.size | default( 'NONR',true ) }}
- name: two5
lineinfile:
path: /root/hwreport.txt
regexp: "^HOST"
line: HOST={{ ansible_devices.vdb.size | default( 'NONR',true ) }}

创建密码库

按照下方所述,创建一个 Ansible 库来存储用户密码:库名称为 /home/greg/ansible/locker.yml库中含有两个变量,名称如下:pw_developer,值为 Imadevpw_manager,值为 Imamgr用于加密和解密该库的密码为 whenyouwishuponastar密码存储在文件 /home/greg/ansible/secret.txt 中**
解题方法:

1
2
3
4
5
6
7
8
9
[greg@bastion ansible]$ vim ansible.cfg
仅修改一行
vault_password_file = /home/greg/ansible/secret.txt
[greg@bastion ansible]$ vim locker.yml
---
pw_developer: Imadev
pw_manager: Imamgr
[greg@bastion ansible]$ echo whenyouwishuponastar > secret.txt
[greg@bastion ansible]$ ansible-vault encrypt locker.yml

创建用户帐户

http://materials/user_list.yml 下载要创建的用户的列表,并将它保存到 /home/greg/ansible在本次考试中使用在其他位置创建的密码库 /home/greg/ansible/locker.yml 。创建名为 /home/greg/ansible/users.yml 的 playbook ,从而按以下所述创建用户帐户:职位描述为 developer 的用户应当:在 dev 和 test 主机组中的受管节点上创建从 pw_developer 变量分配密码是补充组 devops 的成员职位描述为 manager 的用户应当:在 prod 主机组中的受管节点上创建从 pw_manager 变量分配密码是补充组 opsmgr 的成员密码采用 SHA512 哈希格式。您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件 /home/greg/ansible/secret.txt 正常运行。**
解题方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
- name: 创建用户帐户
hosts: dev,test
vars_files:
- /home/greg/ansible/locker.yml
- /home/greg/ansible/user_list.yml
tasks:
- name: one
group:
name: devops
state: present
- name: two
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: one1
group:
name: opsmgr
state: present
- name: two1
user:
name: "{{ item.name }}""
groups: opsmgr
password: "{{ pw_manager | password_hash( 'sha512' ) }}"
append: yes
loop: "{{ users }}"
when: item.job == 'manager'

更新 Ansible 库的密钥

按照下方所述,更新现有 Ansible 库的密钥:从 http://materials/salaries.yml 下载 Ansible 库到 /home/greg/ansible当前的库密码为 insecure8sure新的库密码为 bbs2you9527库使用新密码保持加密状态**
解题方法:

1
2
3
4
5
6
[greg@bastion ansible]$ wget http://materials/salaries.yml
[greg@bastion ansible]$ ansible-vault rekey --ask-vault-pass salaries.yml
Vault password: 粘贴当前密码
New Vault password: 粘贴新密码
Confirm New Vault password: 粘贴新密码
[greg@bastion ansible]$ ansible-vault view salaries.yml