2021 RHCE(RH294)题目
模拟环境下载:链接:https://pan.baidu.com/s/1T0ny1EYZhJPHe4Vk_QF5Pw 提取码:770r
考试环境
RHCE考试共有6台虚拟机,1台是控制节点,做题时候都在这个控制节点操作;其他5台是被控制节点
机器信息
system | IP Addreess | Role |
---|---|---|
workstation.lab.example.com | 172.25.250.9 | Ansible managed node |
servera.lab.example.com | 172.25.250.10 | Ansible managed node |
serverb.lab.example.com | 172.25.250.11 | Ansible managed node |
serverc.lab.example.com | 172.25.250.12 | Ansible managed node |
serverd.lab.example.com | 172.25.250.13 | Ansible managed node |
bastion.lab.example.com | 172.25.250.254 | ansible control node |
登陆密码
- 6台虚拟机的root密码是redhat,请勿更改 root 密码。所有系统上已预装了 SSH 密钥,允许在不输入密码的前提下通过 SSH 进行 root 访问。请勿对系统上的 root SSH 配置文件进行任何修改。
- Ansible 控制节点上已创建了用户 greg。此帐户预装了 SSH密钥,允许在 Ansible 控制节点和各个 Ansible 受管节点之间进行SSH 登录。请勿对系统上的 greg SSH 配置文件进行任何修改。
RHCE考试环境还原:
-
快照里面选择“RHCE考试模拟环境”即可。
- 可能会提示这个错误:“此虚拟机的处理器所支持的功能不同于保存虚拟机状态的虚拟机的处理器所支持的功能,是否继续运行虚拟机”。选择否。然后会提示:“还原CPU状态时候出错”,选择放弃
-
启动虚拟机。修改root密码(方法参考RHCSA环境)。grub加密了:用户名root,密码Asimov
-
开机后开启虚拟机
[root@foundation0 ~]# virsh start bastion [root@foundation0 ~]# virsh start workstation [root@foundation0 ~]# virsh start servera [root@foundation0 ~]# virsh start serverb [root@foundation0 ~]# virsh start serverc [root@foundation0 ~]# virsh start serverd [root@foundation0 ~]# virsh list --all
-
考试时候不需要上面这些操作,会有一个虚拟机管理的小工具,可以对虚拟机启动,关机,重启,重置等
注意: 考试时候全部在控制节点操作
一、安装和配置ansible
按照下面描述,在控制节点172.25.250.254
上安装和配置ansible
-
1、安装所需的软件包
-
2、创建名为
/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
主机组的成员
-
3、创建名为
/home/greg/ansible/ansible.cfg
的配置文件,以满足以下要求:- 主机清单文件为
/home/greg/ansible/inventory
- playbook 中使用的角色的位置包括
/home/greg/ansible/roles
- 主机清单文件为
#连接控制节点,创建greg用户。考试时候不用操作,有这个用户
[root@foundation0 ~]# ssh [email protected] "useradd greg;echo redhat | passwd --stdin greg"
#考试时候操作
[root@foundation0 ~]# ssh [email protected]
[root@bastion ~]# yum install -y ansible #安装ansible
[root@bastion ~]# su - greg #切换到greg用户
#创建ansible目录
[greg@bastion ~]$ mkdir ansible
[greg@bastion ~]$ cd ansible
#编写清单文件
[greg@bastion ansible]$ cat /home/greg/ansible/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
#复制一份ansible配置文件
[greg@bastion ansible]$ cp -a /etc/ansible/ansible.cfg /home/greg/ansible/ansible.cfg
#修改清单文件里面的inventory文件目录,roles目录,和不检查主机key
[greg@bastion ansible]$ grep -Ev '^$|^#' /home/greg/ansible/ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles
host_key_checking = False
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
# 检查ansible版本,config目录
[greg@bastion ansible]$ ansible --version
ansible 2.8.0
config file = /home/greg/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-inventory --graph图形化展示inventory文件主机结构
[greg@bastion ansible]$ ansible-inventory --graph
#测试主机连通性
[greg@bastion ansible]$ ansible all -m ping
二、创建和运行ansible临时命令
请按照正文所述,创建一个名为 /home/greg/ansible/adhoc.sh
的 shell 脚本,该脚本将使用 Ansible 临时命令在各个受管节点上安装 yum 存储库:
存储库1:
- 存储库的名称为
EX294_BASE
- 描述为
EX294 base software
- 基础 URL 为
http://content/rhel8.0/x86_64/dvd/BaseOS
- GPG 签名检查为
启用状态
- 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/AppStream
- GPG 签名检查为
启用状态
- GPG 密钥 URL 为
http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
- 存储库为
启用状态
#准备工作,删除已有的repo文件(考试不用操作)
[greg@bastion ansible]$ ansible all -m shell -a 'rm -f /etc/yum.repos.d/*.repo'
#考试操作
[greg@bastion ansible]$ cat adhoc.sh
ansible all -m yum_repository -a 'name="EX294_BASE" description="EX294 base software" baseurl="http://content/rhel8.0/x86_64/dvd/BaseOS" gpgcheck="yes" gpgkey="http://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="http://content/rhel8.0/x86_64/dvd/AppStream" gpgcheck="yes" gpgkey="http://content/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release" enabled="yes"'
#给脚本加上执行权限
[greg@bastion ansible]$ chmod +x adhoc.sh
#执行脚本
[greg@bastion ansible]$ ./adhoc.sh
#检查是否执行成功
[greg@bastion ansible]$ ansible all -m shell -b -a 'cat /etc/yum.repos.d/*.repo'
[greg@bastion ansible]$ ansible all -m shell -b -a 'yum clean all'
[greg@bastion ansible]$ ansible all -m shell -b -a 'yum makecache'
三、安装软件包
创建一个名为 /home/greg/ansible/packages.yml
的 playbook :
- 将
php
和mariadb
软件包安装到dev
、test
和prod
主机组中的主机上 - 将
RPM Development Tools
软件包组安装到dev
主机组中的主机上 - 将
dev
主机组中主机上的所有软件包更新为最新版本
[greg@bastion ansible]$ cat /home/greg/ansible/packages.yml
- name: install packages
hosts: dev,test,prod
tasks:
- name: install php and mariadb
yum:
name:
- php
- mariadb
state: present
- name: install group packages
hosts: dev
tasks:
- name: install php and mariadb
yum:
name: "@Development Tools"
state: present
- name: update packages
hosts: dev
tasks:
- name: upgrade
yum:
name: "*"
state: latest
#部署测试
[greg@bastion ansible]$ ansible-playbook packages.yml
四、使用RHEL系统角色
安装 RHEL 系统角色软件包,并创建符合以下条件的 playbook /home/greg/ansible/timesync.yml
:
- 在
所有受管节点
上运行 - 使用
timesync
角色 - 配置该角色,以使用当前有效的 NTP 提供商
- 配置该角色,以使用时间服务器
172.25.254.254
- 配置该角色,以启用
iburst
参数
解法:
1、yum安装rhel-system-roles包,如果忘记包名字的话使用yum search rhel-*搜索报名
2、然后ansible-galaxy list查看安装的roles包目录,添加到roles_path里面
3、查看timesync role的README文档(/usr/share/ansible/roles/linux-system-roles.timesync/README.md),将里面的example案例复制过来
3、执行ansible-playbook脚本
4、使用ansible命令检查:
ansible all -m shell -b -a "grep iburst /etc/chrony.conf" ansible all -m shell -b -a "chronyc sources -v"
#1、安装RHEL系统角色软件包,使用root身份安装
[root@bastion ~]# yum install rhel-system-roles -y
#2、使用ansible-galaxy确认timesync角色是存在的
[root@bastion ~]# ansible-galaxy list
# /usr/share/ansible/roles
- linux-system-roles.kdump, (unknown version)
- linux-system-roles.network, (unknown version)
- linux-system-roles.postfix, (unknown version)
- linux-system-roles.selinux, (unknown version)
- linux-system-roles.timesync, (unknown version)
- rhel-system-roles.kdump, (unknown version)
- rhel-system-roles.network, (unknown version)
- rhel-system-roles.postfix, (unknown version)
- rhel-system-roles.selinux, (unknown version)
- rhel-system-roles.timesync, (unknown version)
#3、切换为greg用户,将系统的角色目录配置到ansible配置文件里面
[greg@bastion ansible]$ grep -Ev '^$|^#' ansible.cfg
[defaults]
inventory = /home/greg/ansible/inventory
roles_path = /home/greg/ansible/roles:/usr/share/ansible/roles
host_key_checking = False
...
#4、再次执行ansible-galaxy确认
[greg@bastion ansible]$ ansible-galaxy list
#5、编写timesync.yml,参考/usr/share/doc/rhel-system-roles/timesync/README.md里面的案例,复制过来改下即可
[greg@bastion ansible]$ cat /home/greg/ansible/timesync.yml
---
- name: ntp
hosts: all
vars:
timesync_ntp_servers:
- hostname: 172.25.254.254
iburst: yes
timesync_ntp_provider: chrony
roles:
- rhel-system-roles.timesync
#6、运行测试
[greg@bastion ansible]$ ansible-playbook timesync.yml
#7、检查
[greg@bastion ansible]$ ansible all -m shell -a 'grep ^server /etc/chrony.conf'
[greg@bastion ansible]$ ansible all -m shell -a 'chronyc sources -v'
五、使用ansible-galaxy安装角色
使用 Ansible Galaxy 和要求文件 /home/greg/ansible/roles/requirements.yml
。从以下 URL 下载角色并安装到 /home/greg/ansible/roles
:
http://materials/haproxy.tar
此角色的名称应当为balancer
http://materials/phpinfo.tar
此角色的名称应当为phpinfo
#编写/home/greg/ansible/roles/requirements.yml
[greg@bastion ansible]$ cat /home/greg/ansible/roles/requirements.yml
---
- name: balancer
src: http://materials/haproxy.tar
- name: phpinfo
src: http://materials/phpinfo.tar
#执行脚本
[greg@bastion ansible]$ ansible-galaxy install -r roles/requirements.yml -p /home/greg/ansible/roles/
#确认安装成功
[greg@bastion ansible]$ ls roles/
balancer phpinfo requirements.yml
六、创建和使用角色
根据下列要求,在 /home/greg/ansible/roles
中创建名为 apache
的角色:
-
httpd 软件包已安装,设为在
系统启动时启用
并启动
-
防火墙
已启用并正在运行,并使用允许访问Web
服务器的规则 -
模板文件
index.html.j2
已存在,用于创建具有以下输出的文件/var/www/html/index.html
:Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的
完全限定域名
(fqdn),IPADDRESS
则是受管节点的 IP 地址。 -
创建一个使用此角色的 playbook /home/greg/ansible/newrole.yml:
- 该 playbook 在 webservers 主机组中的主机上运行
# 创建apache角色。考试时候是ansible2.9版本,命令略有变化,使用ansible-galaxy role init xxx来创建
[greg@bastion ansible]$ ansible-galaxy init apache --init-path=/home/greg/ansible/roles/
- apache was created successfully
[greg@bastion ansible]$ ls roles/
apache balancer phpinfo requirements.yml
# 编写tasks
[greg@bastion ansible]$ cat roles/apache/tasks/main.yml
---
- name: install httpd
yum:
name: httpd
state: latest
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
- name: start firewalld service
service:
name: firewalld
state: started
enabled: yes
- name: firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
- name: copy index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
# 编写index.html.j2模版 相关变量可以使用setup模块查看:ansible dev -m setup -a filter='*fqdn*'
[greg@bastion ansible]$ cat roles/apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
# 编写/home/greg/ansible/newrole.yml
[greg@bastion ansible]$ cat /home/greg/ansible/newrole.yml
---
- name: use apache role
hosts: webservers
roles:
- apache
#部署测试
[greg@bastion ansible]$ ansible-playbook newrole.yml
#访问测试
[greg@bastion ansible]$ curl 172.25.250.12
Welcome to serverc.lab.example.com on 172.25.250.12
七、从ansible galaxy使用角色
上面下载的两个角色需要用到了。balancer和phpinfo。
balancer负责部署haproxy。phpinfo负责部署php。
结构: webserver机器上部署php,lobalancer机器部署haproxy,转发到webserver上
根据下列要求,创建一个名为 /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.12`
-
-
playbook 中包含一个 play, 该 play 在
webservers
主机组中的主机上运行并将使用phpinfo
角色。-
请通过 URL
/hello.php
浏览到webservers
主机组中的主机将生成以下输出:Hello PHP World from FQDN
-
其中,FQDN 是主机的完全限定名称。
Hello PHP World from serverb.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。
-
-
同样,浏览到
http://172.25.250.12/hello.php
会生成以下输出:Hello PHP World from serverc.lab.example.com
另外还有 PHP 配置的各种详细信息,如安装的 PHP 版本等。
# 编写/home/greg/ansible/roles.yml,其实就是每个主机组安装软件包就好了
# 注意执行顺序,据说考试时候顺序错了,执行时候会报变量找不到的错误。先执行apache,再执行phpinfo,再执行balancer
[greg@bastion ansible]$ cat /home/greg/ansible/roles.yml
---
- name: install httpd
hosts: webservers
roles:
- apache
- name: install php
hosts: webservers
roles:
- phpinfo
- name: install haproxy
hosts: balancers
roles:
- balancer
# 部署测试
[greg@bastion ansible]$ ansible-playbook roles.yml
#访问测试
[greg@bastion ansible]$ curl 172.25.250.13
Welcome to serverb.lab.example.com on 172.25.250.11
[greg@bastion ansible]$ curl 172.25.250.13
Welcome to serverc.lab.example.com on 172.25.250.12
[greg@bastion ansible]$ curl -I 172.25.250.13/hello.php
HTTP/1.1 200 OK
八、创建和使用逻辑卷
创建一个名为 /home/greg/ansible/lv.yml
的 playbook ,它将在所有受管节点
上运行以执行下列任务:
-
创建符合以下要求的逻辑卷:
- 逻辑卷创建在
research
卷组中 - 逻辑卷名称为
data
- 逻辑卷大小为
1500 MiB
- 逻辑卷创建在
-
使用
ext4
文件系统格式化逻辑卷 -
如果无法创建请求的逻辑卷大小,应显示错误信息
Could not fcreate logical volume of that size
,并且应改为使用大小
800 MiB
。 -
如果卷组
research
不存在,应显示错误信息Volume group done not exist
。 -
不要以任何方式挂载逻辑卷
[greg@bastion ansible]$ cat /home/greg/ansible/lv.yml
---
- name: create lv
hosts: all
tasks:
- block:
- name: Create a logical volume.
lvol:
vg: research
lv: data
size: 1500m
- name: Create a ext4 filesystem on /dev/research/data
filesystem:
fstype: ext4
dev: /dev/research/data
rescue:
- name: output error
debug:
msg: "Could not fcreate logical volume of that size"
when: ansible_lvm.vgs.research is defined
- name: Create a logical volume.
lvol:
vg: research
lv: data
size: 800m
when: ansible_lvm.vgs.research is defined
- name: Create a ext4 filesystem on /dev/research/data
filesystem:
fstype: ext4
dev: /dev/research/data
when: ansible_lvm.vgs.research is defined
- name: output error
debug:
msg: "Volume group done not exist"
when: ansible_lvm.vgs.research is undefined
#执行playbook测试
[greg@bastion ansible]$ ansible-playbook lv.yml
九、生成主机文件
- 将一个初始模板文件从
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 workstation
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd
注:清单主机名称的显示顺序不重要。
#hosts.yml文件写法一:
[greg@bastion ansible]$ cat /home/greg/ansible/hosts.yml
---
- name: copy template file
hosts: all
tasks:
- name: copy hosts.j2
template:
src: hosts.j2
dest: /etc/myhosts
when: "'dev' in group_names"
#hosts.yml文件写法二
[greg@bastion ansible]$ cat /home/greg/ansible/hosts.yml
---
- name: gather facts
hosts: all
- name: copy template file
hosts: dev
tasks:
- name: copy hosts.j2
template:
src: hosts.j2
dest: /etc/myhosts
#jinjia2模版文件
[greg@bastion ansible]$ cat /home/greg/ansible/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_default_ipv4']['address'] }} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }}
{% endfor %}
#投机取巧办法,实在想不到不出上面答案了可以试一试下面这个
[greg@bastion ansible]$ cat /home/greg/ansible/hosts.j2
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 workstation
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd
十、修改文件内容
按照下方所述,创建一个名为 /home/greg/ansible/issue.yml
的 playbook :
- 该 playbook 将在
所有清单主机
上运行 - 该 playbook 会将
/etc/issue
的内容替换为下方所示的一行文本:- 在
dev
主机组中的主机上,这行文本显示 为:Development
- 在
test
主机组中的主机上,这行文本显示 为:Test
- 在
prod
主机组中的主机上,这行文本显示 为:Production
- 在
[greg@bastion ansible]$ cat /home/greg/ansible/issue.yml
---
- name: modify issue file
hosts: all
tasks:
- name: write Development to dev
copy:
content: 'Development'
dest: /etc/issue
when: "'dev' in group_names"
- name: write Test to test
copy:
content: 'Test'
dest: /etc/issue
when: "'test' in group_names"
- name: write Production to prod
copy:
content: 'Production'
dest: /etc/issue
when: "'prod' in group_names"
#执行
[greg@bastion ansible]$ ansible-playbook issue.yml
#检查
[greg@bastion ansible]$ ansible all -m shell -a 'cat /etc/issue'
十一、创建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
#file模块和copy模块有个setype选项,可以设置selinux权限。不用记httpd_sys_content_t这个单词,直接semanage fcontext -l | grep nginx | grep share | grep http就可以找到
---
- name: create web dir
hosts: dev
tasks:
- name: create dir /webdev
file:
path: /webdev
state: directory
group: webdev
mode: '2775'
setype: 'httpd_sys_content_t'
- name: Create a symbolic link
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: copy content
copy:
content: 'Development'
dest: /webdev/index.html
setype: 'httpd_sys_content_t'
- name: install httpd
yum:
name: httpd
state: latest
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
- name: start firewalld service
service:
name: firewalld
state: started
enabled: yes
- firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
zone: public
#执行脚本
[greg@bastion ansible]$ ansible-playbook webcontent.yml
#访问测试
[greg@bastion ansible]$ curl 172.25.250.9/webdev/
Development
十二、生成硬件报告
创建一个名为 /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
[greg@bastion ansible]$ cat /home/greg/ansible/hwreport.yml
---
- name: create report file
hosts: all
tasks:
- name: create report file
get_url:
url: http://materials/hwreport.empty
dest: /root/hwreport.txt
- name: get inventory name
replace:
path: /root/hwreport.txt
regexp: 'inventoryhostname'
replace: '{{ inventory_hostname }}'
- name: get memory total size
replace:
path: /root/hwreport.txt
regexp: 'memory_in_MB'
replace: '{{ ansible_memfree_mb | string }}'
- name: get bios version
replace:
path: /root/hwreport.txt
regexp: 'BIOS_version'
replace: '{{ ansible_bios_version | string }}'
- name: get disk vda size
replace:
path: /root/hwreport.txt
regexp: 'disk_vda_size'
replace: '{{ ansible_devices.vda.size | default("NONE") }}'
- name: get disk vdb size
replace:
path: /root/hwreport.txt
regexp: 'disk_vdb_size'
replace: '{{ ansible_devices.vdb.size | default("NONE") }}'
#执行脚本
[greg@bastion ansible]$ ansible-playbook hwreport.yml
#检查是否成功
[greg@bastion ansible]$ ansible all -m shell -a "cat /root/hwreport.txt"
# Hardware report
HOST=172.25.250.9
MEMORY=139
BIOS=1.11.1-3.module+el8+2529+a9686a4d
DISK_SIZE_VDA=10.00 GB
DISK_SIZE_VDB=NONE
...
十三、创建密码库
按照下方所述,创建一个 Ansible 库来存储用户密码:
- 库名称为
/home/greg/ansible/locker.yml
- 库中含有两个变量,名称如下:
pw_developer
,值为Imadev
pw_manager
,值为Imamgr
- 用于加密和解密该库的密码为
whenyouwishuponastar
- 密码存储在文件
/home/greg/ansible/secret.txt
中
[greg@bastion ansible]$ cat /home/greg/ansible/locker.yml
pw_developer: Imadev
pw_manager: Imamgr
[greg@bastion ansible]$ ansible-vault encrypt locker.yml
New Vault password:
Confirm New Vault password:
Encryption successful
[greg@bastion ansible]$ echo whenyouwishuponastar > /home/greg/ansible/secret.txt
#确认
[greg@bastion ansible]$ ansible-vault view --vault-password-file=secret.txt locker.yml
pw_developer: Imadev
pw_manager: Imamgr
十四、创建用户账户
- 从
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
的成员(其实就是附加组,英文叫supplementary groups)
- 在
- 职位描述为
- 密码采用
SHA512
哈希格式。 - 您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件
/home/greg/ansible/secret.txt
正常运行。
#常见错误,loop和when位置写错
[greg@bastion ansible]$ cat users.yml
---
- name: create user in developer
hosts: dev,test
vars_files:
- locker.yml
- user_list.yml
tasks:
- name: Ensure group "devops" exists
group:
name: devops
state: present
- name: create user developer
user:
name: "{{ item.name }}"
groups: devops
append: yes
password: "{{ pw_developer | password_hash('sha512') }}"
loop: "{{ users }}"
when: item.job == "developer"
- name: create manager user
hosts: prod
tasks:
vars_files:
- locker.yml
- user_list.yml
tasks:
- name: Ensure group "opsmgr" exists
group:
name: opsmgr
state: present
- name: create user in manager
user:
name: "{{ item.name }}"
groups: opsmgr
append: yes
password: "{{ pw_manager | password_hash('sha512') }}"
loop: "{{ users }}"
when: item.job == "manager"
#执行playbook,测试是可用的
[greg@bastion ansible]$ ansible-playbook users.yml --vault-password-file=/home/greg/ansible/secret.txt
#检查用户是否创建成功
[greg@bastion ansible]$ ssh [email protected] "id bob"
[email protected]'s password:
uid=1002(bob) gid=1003(bob) groups=1003(bob),1001(devops)
[greg@bastion ansible]$ ssh [email protected] "id bob"
[email protected]'s password:
id: ‘bob’: no such user
[greg@bastion ansible]$ ssh [email protected] "id sally"
[email protected]'s password:
uid=1002(sally) gid=1003(sally) groups=1003(sally),1002(opsmgr)
十五、更新Ansible库的密钥
按照下方所述,更新现有 Ansible 库的密钥:
- 从
http://materials/salaries.yml
下载 Ansible 库到/home/greg/ansible
- 当前的库密码为
insecure8sure
- 新的库密码为
bbs2you9527
- 库使用
新密码
保持加密状态
[greg@bastion ansible]$ wget http://materials/salaries.yml
[greg@bastion ansible]$ ansible-vault rekey salaries.yml
Vault password:
New Vault password:
Confirm New Vault password:
Rekey successful
#确认修改密码成功
[greg@bastion ansible]$ ansible-vault view salaries.yml
Vault password:
haha
十六、计划任务
在控制节点创建playbook:/home/greg/ansible/cron.yml
文件,实现:
- 给natasha用户设置一个计划任务,每隔两分钟执行一次:
logger "EX200 in progress"
#考试时候每个机器都是有natasha用户的,不放心的可以ansible all -m shell -a 'id natasha'检查
#编写cron.yml
[greg@bastion ansible]$ cat /home/greg/ansible/cron.yml
---
- name: cron
hosts: all
tasks:
- name: crontab for user natasha
cron:
name: yum autoupdate
minute: "*/2"
user: natasha
job: "logger 'EX200 in progress'"
#执行检查
[greg@bastion ansible]$ ansible-playbook cron.yml
#检查每个机器上计划任务
[greg@bastion ansible]$ ansible all -m shell -a "crontab -u natasha -l"
十七、重启虚拟机
因为判分脚本会重启机器测试,所以需要重启机器虚机可以启动,以及确保配置和服务在重新引导后仍然保留
#考试时候通过虚拟机管理小工具重启,模拟环境可以使用命令重启
virsh reboot xxx