RHCE考试一共分为两个部分,上午RHCSA考试2个半小时,下午RHCE4个小时,其中RHCE部分15道题,考试正常1个半小时到2个小时能做完,当然慢点也没事,RHCSA部分一共二十二道题,1月份新增容器两道题,后期会进行分享,考试正常1个多小时能做完,机构给的题和答案还有视频,讲解的也不透彻,这次分享一下自己的备注版本
1.设置主机组/home/student/ansible/inventory的静态清单文件 servera是dev主机组的成员 serverb是test主机组的成员 serverc和serverd是prod主机组的成员 bastion是balancers主机组的成员 prod组是webservers主机组的成员 2.将配置文件导入到student的家目录里, 3.并设置提权,修改主机组配置文件到student的家目录,设置远程用户为devops
1.解:
vim inventory #编辑主机清单
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
bastion
[webservers:children]
prod
2.解:
cp /etc/ansible/ansible.cfg /home/student/ansible/
3.解:
搜索指定文件名,并修改为如下位置
vim ansible.cfg
inventory = /home/student/ansible/inventory
remote_user = devops
roles_path = /home/stduent/ansible/roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
考点:ansible提权配置,ansible主机清单编写
请按照下方所述, 创建一个名为/home/student/ansible/adhoc.sh的shell脚本
脚本将使用Ansible临时命令在各个受管节点上安装yum存储库: 存储库1:
存储库2:
解法;
vim adhoc.sh
#bin/bash
ansible all -m yum_repository -a " name=rh294_BASE description='rh294 base software'file=redhat_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"
ansible all -m yum_repository -a " name=rh294_STREAM description='rh294 stream software'file=redhat_dvd baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release enabled=yes"
考点:yum_repository 模块使用,是一个用于配置yum源的模块
创建一个名为 /home/student/ansible/packages.yml的 playbook:
解:
vim packages.yml
- name: package insall
hosts: dev,test,prod
tasks:
- name: install php mariadb
yum:
name:
- php
- mariadb
state: installed
- name: package greup
hosts: dev
tasks:
- name: install dev tools
yum:
name: "@Development Tools"
state: installed
- name: package updata
hosts: dev
tasks:
- name: insall updata
yum:
name: '*'
state: latest
考点:yum模块的使用安装软件包组时需要在前面加@符号,还有注意的是,每隔一个等级空两格,installed 代表安装,latest代表安装最新版本
安装 RHEL 系统角色软件包,并创建符合以下条件的playbook /home/student/ansible/timesync.yml:
yum install -y rhel-system-roles.noarch #安装系统角色
[root@workstation ansible]# mkdir roles
[root@workstation ansible]# cp -r /usr/share/ansible/roles/rhel-system-roles.timesync/ roles/timesync #把下载的系统角色复制到目录下
[root@workstation ansible]# vim temisync.yml #创建剧本
- hosts: all #在所有节点上运行
vars:
timesync_ntp_servers:
- hostname: classroom.example.com
iburst: yes
roles:
- timesync
考点: redhat系统角色 在当前版本中,Red Hat提供了五类角色集,分别是:kdump,postfix,network,selinux和timesync,系统角色,简单来说就是系统自带的roles
使用Ansible Galaxy安装角色 使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml, 从以下 URL 下载 角色并安装到 /home/student/ansible/roles: http://classroom.example.com/content/haproxy.tar.gz 此角色的名称应当为 balancer http://classroom.example.com/content/phpinfo.tar.gz 此角色的名称应当为 phpinfo
解法:
[student@workstation ansible]$ vim roles/requirements.yml
- name: balancer
src: http://classroom.example.com/content/haproxy.tar.gz
- name: phpinfo
src: http://classroom.example.com/content/phpinfo.tar.gz
[student@workstation ansible]$ ansible-galaxy install -r roles/requirements.yml -p roles/ #下载角色
考点:将地址编写进yml并安装ansible-galaxy -r 指定源,-p指定目的
0.根据下列要求,在 /home/student/ansible/roles中创建名为 apache 的角色:
0.解
root@workstation roles]# ansible-galaxy init apache #生成apache角色文件
[root@workstation roles]# cd apache/
[root@workstation apache]# tree #查看自动生成的目录
.
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── index.html.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
1,2.解
[root@workstation apache]# vim tasks/main.yml #编辑任务文件
---
# testk file for apache
- name: config system service
service: name=httpd state=started enabled=yes #启动httpd服务
- name: config firewalld
firewalld: zone=public service=http state=enabled permanent=yes immediate=yes
#防火墙模块 设置区域 放通服务 状态启用 是否保存为永久 立刻启用
- name: user templates
template: src=index.html.j2 dest=/var/www/html/index.html
传输网页文件到指定位置
3.解
[root@workstation apache]# vim templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }} #调用系统变量 域名
4.解
[root@workstation ansible]# cat newrole.yml
- name: user apache role
hosts: webservers #对web server 操作
roles:
- apache #调用apache剧本
测试:
[root@workstation ansible]# curl serverc
Welcome to serverc.lab.example.com on 172.25.250.12
[root@workstation ansible]# curl serverd
Welcome to serverd.lab.example.com on 172.25.250.13
考点: ansble的service模块,firewall模块,template模块 防火墙模块的使用:
firewalld: zone=public service=http state=enabled permanent=yes immediate=yes
#防火墙模块 设置区域 放通服务 状态启用 是否保存为永久 立刻启用
Ansible roles使用,系统变量 ansible_fqdn ,ansible_default_ipv4.address fqdn 调用的是被执行playbook主机的域名,ipv4.address是主机的ip地址
根据下列要求,创建一个名为 /home/student/ansible/roles.yml 的 playbook:
别看他写了这么多其实都不用管,只需调用第六题下载的roles 即可肥肠的简单 针对balancers组使用 balancer角色 针对websers组使用phpinfo角色
解:
vim roles.yml
---
- name: config balancer
hosts: balancers
roles:
- balancer
- name: config php webserver
hosts: webservers
roles:
- phpinfo
创建一个名为/home/student/ansible/lv.yml 的playbook,它将在所有受管节点上运行以执行下列任务
解题步骤: 这里说了要我们创建逻辑卷,又说了如果创建逻辑卷大小超过了vg的大小,就应当缩小容量然后再次创建,如果vg卷组不存在的话就要显示错误信息 使用了block rescue always block、rescue和always之间的关系:block执行有误时执行rescue,而always无论block是否成功执行都会执行 这里我们进行when判断,如果research这个VG组存在就执行block,research和always的语句,不存在就返回一条信息,说明research这个VG组不存在
解:
- name: create LVM
hosts: all
tasks:
- name: create L - V
block:
- name: create LVM 1500M
lvol:
vg: research #vg的名字
lv: data #lv的名字
size: 1500M #lv的大小
rescue: #如果block任务执行失败则执行如下
- name: output fail msg
debug: #输出LVM无法创建
msg: Could not create logical volume of that size
- name: create lvm 800m #并尝试将容量修改为800m
lvol:
vg: research
lv: data
size: 800M
always: #始终执行的
- name: format lvm
filesystem:
fstype: ext4
dev: /dev/research/data
when: " 'research' in ansible_lvm.vgs" #有research卷组执行格式化
- name: seratch not existes
debug:
msg: Volume group done not exist
when: " 'research' not in ansible_lvm.vgs" #没有卷组就输出没有卷组
考点:
生成模板文件
1.解:编写j2文件
[student@workstation 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_enp1s0.ipv4.address }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }}
{% endfor %}
#for循环把所有主机名赋值给host
#第一个获取对应主机的ip地址
#第二个主机域名信息
#第三个主机名
hostvars是所有主机的信息,hostvars[host].ansible_hostname 实际上经过循环赋值后,会变成hostvars[servera].ansible_enp1s0.ipv4address 所有含义就是获取所有主机信息提取servera的ansible_hostname参数
2.解:编写yml文件
[student@workstation ansible]$ vim hosts.yml
- name: vars
hosts: all
- name: config myhosts
hosts: dev
tasks:
- name: copy hosts.j2 to dev
template:
src: hosts.j2 #拷贝编写好的j2
dest: /etc/myhosts #复制到对应路径下
调用所有hosts:all原因先引用所有主机的内置变量,因为我们在hosts.j2里面调用了所有主机的变量,但是如果不在playbook里先调用所有主机的变量的话,那playbook脚本将执行失败
运行yml
[root@workstation ansible]# ansible-playbook hosts.yml
考点: 1.jinja2模板 2.魔法变量 group_names: 列出当前受管理主机所属的所有组,gruop列出清单中所有的主机和组 hostsvars:列出当前所有受管理的主机信息 3.ansible facts 获取客户端的参数 ansible_fqdn是域名参数,ansible_enp1s0.ipv4.address 是ip地址参数 ansible_hostname是主机名
[root@workstation ansible]# ansible servera -m setup | grep ansible_hostname
"ansible_hostname": "servera",
[root@workstation ansible]# ansible servera -m setup | grep ansible_fqdn
"ansible_fqdn": "servera.lab.example.com",
按照下方所述,创建一个名为 /home/student/ansible/issue.yml 的 playbook:
该 playbook 将在所有清单主机上运行
该 playbook 会将 /etc/issue 的内容替换为下方所示的一行文本:
在 dev 主机组中的主机上,这行文本显示为:Development 在 test 主机组中的主机上,这行文本显示为:Test 在 prod 主机组中的主机上,这行文本显示为:Production
[root@workstation ansible]# vim issur.yml
- name: modify issue file
hosts: all #针对所有主机
tasks:
- name: write Development to dev
copy:
content: "Development" #内容
dest: /etc/issue #目的地
when: "'dev' in group_names" #如果当前主机属于dev主机组则执行copy
- name: write Test to test
copy:
content: "Test"
dest: /etc/issue
when: "'test' in group_names" #如果当前主机属于test主机组则执行copy
- name: write Production to pord
copy:
content: "Production"
dest: /etc/issue
when: "'pord' in group_names" #如果当前主机属于pord主机组则执行copy
按照下方所述,创建一个名为 /home/student/ansible/webcontent.yml 的 playbook:
该 playbook 在 dev 主机组中的受管节点上运行
创建符合下列要求的目录 /webdev:拥有组为 devops 组
具有常规权限:owner=read+write+execute,group=read+write+execute other=read+execute,具有特殊权限: set group ID
用符号链接将 /var/www/html/webdev 链接到 /webdev
创建文件 /webdev/index.html,其中包含如下所示的单行文本:Development
在 dev 主机组中主机上浏览此目录(例如 http://servera.lab.example.com/webdev/ )将生成以下输出:Development
[student@workstation ansible]$ vim webcontent.yml
- name: create web directory
hosts: dev
tasks:
- name: config system service #开启httpp与firewalld
service: name="{{ item }}}" state=started enabled=yes
loop: #给item传输变量
- httpd
- firewalld
- name: config firewalld #配置防火墙放行端口
firewalld: zone=public service=http state=enabled permanent=yes immediate=yes
- name: mkdir #创建目录并改权限
file: path=/webdev state=directory owner=root group=devops mode=2775 setype=httpd_sys_content_t
- name: links #创建软链接将webdev创建到/html/webdev
file: src=/webdev dest=/var/www/html/webdev state=link
- name: create file #创建webdev内容
copy: content="Development" dest=/webdev/index.html setype=httpd_sys_content_t
考点: firewalld模块,file模块创建软连接,传输文本,创建文件夹等操作
生成硬件报告 创建一个名为 /home/student/ansible/hwreport.yml的 playbook,它将在所有受管节点上生成含有以 下信息的输出文件 /root/hwreport.txt: 清单主机名称 以 MB 表示的总内存大小 BIOS 版本 磁盘设备 vda 的大小 磁盘设备 vdb 的大小 输出文件中的每一行含有一个 key=value 对。 您的 playbook 应当:
解题步骤:hwreport.empty 包含这些内容
hostname = inventoryhostname
memory = memory_in_MB
bios_version = BIOS_version
vda_size = disk_vda_size
vdb_size = disk_vdb_size
可以通过参数替换将=号中的内容替换为指定的参数 可以使用replace模块 匹配文档中关键字,然后替换为ansible 获取的设备的变量从而完成解题 当没有设备变量时,证明没有其设备,可以使用 “ | ” 正则逻辑或输出 NONE内容
解:
[root@workstation ansible]# cat hwreport.yml
- name: get hwreport info
hosts: all
tasks:
- name: get report file
get_url:
url: http://172.25.254.254/content/hwreport.empty
dest: /root/hwreport.txt
- name: get inventory_hostname
replace:
path: /root/hwreport.txt
regexp: 'inventoryhostname'
replace: "{{ ansible_hostname }}"
- name: get inventory_MB
replace:
path: /root/hwreport.txt
regexp: 'memory_in_MB'
replace: "{{ ansible_memtotal_mb | default('NONE') }}"
- name: get inventory_BIOS
replace:
path: /root/hwreport.txt
regexp: 'BIOS_version'
replace: "{{ ansible_bios_version }}"
- name: get inventory_VDA
replace:
path: /root/hwreport.txt
regexp: 'disk_vda_size'
replace: "{{ ansible_devices.vda.size | default ('NONE') }}"
- name: get inventory_VDB
replace:
path: /root/hwreport.txt
regexp: 'disk_vdb_size'
replace: "{{ ansible_devices.vdb.size | default ('NONE') }}"
考点: 1.replace 模块, 模块参数:path 文件位置,regexp;正则匹配文本的内容,repalce;替换文本的内容 2.get_url 模块,用于下载url位置文件,参数 url;网址 dest;目标位置和名字
按照下方所述,创建一个 Ansible 库来存储用户密码:
解
echo whenyouwishuponastar > secret.txx #创建密码文件
[student@workstation ansible]$ vim locker.yml #创建变量文件
pw_developer: Imadev
pw_manager: Imamgr
ansible-vult --vult-password-file=secret.txt encrypt locker.yml #加密配置
#将secret.txt当作 loker.yml的密码并加密
考点: ansible-vult 使用文件加密
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,从而按以下所述创建用户帐户:
3.密码应采用 SHA512 哈希格式。 4.您的 playbook 应能够在本次考试中使用在其他位置创建的库密码文件 /home/student/ansible/secret.txt 正常运行。
解: 下载变量
wget http://172.25.254.254/content/user_list.yml
查看文件
users:
- name: bob
job: developer
- name: sally
job: manager
- name: fred
job: developer
编写playbook
- name: create developer #名字创建开发人员用户
hosts: dev,test #针对dev,test组
vars_files: #导入变量
- locker.yml #13题的加密变量
- user_list.yml #刚才下载的变量
tasks:
- name: create group #创建student组
group:
name: student
- name: create user in devenloper #创建用户
user:
name: "{{ item.name }}" #取users变量中的name参数,创建他
group: student #在student组中
password: "{{ pw_developer | password_hash('sha512') }}" #指定密码调用locker.yml的变量并hash加密
loop: "{{ users }}" #循环调用user,目的是为了多次调用参数让上面的创建用户循环
when: item.job == "developer" #当users中job变量为开发者时,则执行以上所有操作
- name: create manager
hosts: prod
vars_files:
- locker.yml
- user_list.yml
tasks:
- name: create group
group:
name: prod
- name: create user in manager
user:
name: "{{ item.name }}"
group: student
password: "{{ pw_manager | password_hash('sha512') }}"
loop: "{{ users }}"
when: item.job == "manager",
考点: 1.loop循环,vars_files变量导入与调用,when的使用,user模块 2.group模块: 参数 :name 必须参数 指定要操作的组名称,state:两个选项 1.present 创建组 2.absent删除组 gid: 用于指定gid
按照下方所述,更新现有 Ansible 库的密钥:
解:
wget http://172.25.254.254/content/salaries.yml #下载
[student@workstation ~]$ ansible-vault rekey salaries.yml
Vault password: #老密码
New Vault password: #新密码
Confirm New Vault password: #新密码
Rekey successful
考点:ansible-vault 的修改密码