2022年RHCE认证考题解析最新版—RH294环境【转】

由于本人10.17已成功考过CSA,经过两周所学的ansible并结合题库整理出来的CE解析版
我也是11月月底就要考了,不过这套解析也是可以满足今年的redhat8题库

文中可能涉及一些命令的参数解释,如有不懂的伙伴可参考我的笔记Ansible

ps:一切模板似的题库考试,都需要经过大脑的理解 方可顺利上岸

正文开始:
文章目录
1、安装和配置Ansible
2、创建和运行Ansible临时命令
3、安装软件包
4、使用RHEL系统角色
5、使用Ansible Galaxy安装角色
6、创建和使用角色
7、从Ansible Galaxy使用角色
8、创建和使用逻辑卷
9、生成主机文件
10、修改文件内容
11、创建 Web 内容目录
12、生成硬件报告
13、创建密码库
14、创建用户账户
15、更新 Ansible 库的密钥
16、配置 cron 作业
1、安装和配置Ansible

创建主机清单

 

修改配置文件

//必须修改的配置文件
[grep@control ansible]$ vi ansible.cfg
[defaults]
inventory = /home/student/ansible/inventory
remote_user = greg ——自己所使用的用户
roles_path = /home/student/ansible/roles
host_key_checking = false ——主机之前传输文件不需要密钥认证
[privilege_escalation] 普通用户之间可以使用sudo模式
become = true
become_method = sudo
become_user = root
become_ask_pass = false

[greg@control ansible]$ mkdir roles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
测试是否可以ping通


2、创建和运行Ansible临时命令

考试时可以开启两台终端,另一半负责查看模块帮助文档,在练习当中需记住模块的使用就好
[greg@control ansible]$ ansible-doc yum_repository

 


node1进行验证

[greg@node1 yum. repos.d]$ yum list all | wc -l
6336

3、安装软件包

 

 

测试:


4、使用RHEL系统角色

首先下载系统角色


调用角色配置

 

写入主配置文件
roles: 用来调用timesync中的角色模块

执行
测试

5、使用Ansible Galaxy安装角色

balancer: 使用的是负载均衡
phpinfo: php测试

将两对角色下载到本地
-r 指定使用那个play下载角色 -p 指定下载目录


6、创建和使用角色

手动创建角色

 

 

[greg@control ansible]$ cat htttp.yml
---
- name: install
hosts: webservers
roles:
- apache

 


[greg@control ansible]$ curl 172.25.250.11
Welcome to node3.lab.example.com on 172.25.250.11
[greg@control ansible]$ curl 172.25.250.12
Welcome to node4.lab.example.com on 172.25.250.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
7、从Ansible Galaxy使用角色


这题可能说的有点绕,但是仔细想想其实就是通过利用balancers角色里的haproxy部署好负载均衡

然后webservers组里包含着node3、node4主机,然而在第六题已经在webservers主机组部署好httpd服务和默认网页

第一步实验目的是实现在node5上负载均衡到webservers主机组

清单

 

PHP角色测试的内容
直接使用webservers组的IP访问

 

8、创建和使用逻辑卷

任务执行流程:

当block任务执行成功的时候,则执行always任务
当block任务执行失败的时候,则执行resvue任务,最后执行always任务(block任务执行失败,但是playbook不中止)

 

when语句是用来判断research卷组是否在data逻辑卷里面,如果存在则执行操作


测试:

 

[greg@control ansible] ansible all -a 'lvs'

[root@node2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 10G 0 disk
└─vda1 252:1 0 10G 0 part /
vdb 252:16 0 5G 0 disk
└─research-data 253:0 0 1.5G 0 lvm

[greg@node3 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 10G 0 disk
└─vda1 252:1 0 10G 0 part /
vdb 252:16 0 5G 0 disk
└─vdb1 252:17 0 1G 0 part
└─research-data 253:0 0 800M 0 lvm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
9、生成主机文件


[greg@control ansible]$ wget http://materials/hosts.j2
--2022-11-09 11:07:41-- http://materials/hosts.j2
Resolving materials (materials)... 172.25.254.254
Connecting to materials (materials)|172.25.254.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 147
Saving to: ‘hosts.j2’

hosts.j2 100%[===================>] 147 --.-KB/s in 0s

2022-11-09 11:07:41 (37.9 MB/s) - ‘hosts.j2’ saved [147/147]

[greg@control ansible]$ ls
adhoc.sh hosts.j2 lv.yml roles timesync.yml
ansible.cfg inventory packages.yml roles.yml
[greg@control ansible]$ vim hosts.j2
[greg@control ansible]$ cat 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 }} ——匹配主机组中IP地址
{{ hostvars[host].ansible_fqdn }} ——匹配完全合格域名
{{ hostvars[host].ansible_hostname }} ——匹配主机名
{% endfor %} ——结束for循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[greg@control ansible]$ cat hosts.yml
---
- name: get all facts
hosts: all

- name: cp to myhosts
hosts: dev
tasks:
- name: cp file
template:
src: hosts.j2
dest: /etc/myhosts


//测试
[greg@control ansible]$ ansible-playbook hosts.yml

PLAY [get all facts] ***********************************************************

TASK [Gathering Facts] *********************************************************
ok: [node4]
ok: [node2]
ok: [node5]
ok: [node3]
ok: [node1]

PLAY [cp to myhosts] ***********************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [cp file] *****************************************************************
changed: [node1]

PLAY RECAP *********************************************************************
node1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node4 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node5 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

//node1
[greg@node1 ~]$ cat /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.13
node5.lab.example.com
node5
172.25.250.11
node3.lab.example.com
node3
172.25.250.12
node4.lab.example.com
node4

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
10、修改文件内容

//原内容

[greg@node1 ~]$ cat /etc/issue
\S
Kernel \r on an \m
1
2
3
content :直接以content给定的字符串或变量值作为文件内容保存到远程主机上,它会替代src选项

[greg@control ansible]$ cat issue.yml
---
- name: modify issue
hosts: all
tasks:
- name: input to issue
copy:
content: |
{% if 'dev' in group_names %} ——将dev组中的所有内容替换
Development
{% elif 'test' in group_names %}
Test
{% elif 'prod' in group_names %}
Production
{% endif %}
dest: /etc/issue

[greg@control ansible]$ ansible-playbook issue.yml

PLAY [modify issue] ************************************************************

TASK [Gathering Facts] *********************************************************
ok: [node5]
ok: [node4]
ok: [node3]
ok: [node2]
ok: [node1]

TASK [input to issue] **********************************************************
changed: [node4]
changed: [node5]
changed: [node3]
changed: [node2]
changed: [node1]

PLAY RECAP *********************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node4 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node5 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


//发生改变
[greg@node1 ~]$ cat /etc/issue
Development
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
38
39
40
41
42
43
44
45
46
11、创建 Web 内容目录


[greg@control ansible]$ cat webcontent.yml
---
- name: web station
hosts: dev
tasks:
- name: install rpm
yum:
name:
- httpd
- firewalld
state: present

- name: create group
group:
name: webdev
state: present

- name: create /webdev
file:
path: /webdev
state: directory
group: webdev
mode: 2775

- name: cp
copy:
content: Development
dest: /webdev/index.html

- name: set selinux ——修改为http的网页值
sefcontext:
target: /webdev(/.*)?
setype: httpd_sys_content_t

- name: shell
shell:
cmd: restorecon -Rv /webdev

- name: create link ——创建软链接实现共享
file:
src: /webdev
dest: /var/www/html/webdev
state: link

- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes

- name: restart firewalld
service:
name: firewalld
state: restarted
enabled: yes

- name: firewall for http _放行防火墙规则
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes


[greg@control ansible]$ ansible-playbook webcontent.yml

PLAY [web station] *************************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [install rpm] *************************************************************
ok: [node1]

TASK [create group] ************************************************************
ok: [node1]

TASK [create /webdev] **********************************************************
ok: [node1]

TASK [cp] **********************************************************************
ok: [node1]

TASK [set selinux] *************************************************************
ok: [node1]

TASK [shell] *******************************************************************
changed: [node1]

TASK [create link] *************************************************************
changed: [node1]

TASK [restart httpd] ***********************************************************
changed: [node1]

TASK [restart firewalld] *******************************************************
changed: [node1]

TASK [firewall for http] *******************************************************
changed: [node1]

PLAY RECAP *********************************************************************
node1 : ok=11 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


[greg@control ansible]$ curl http://node1.lab.example.com/webdev/
Development

————————————————
版权声明:本文为CSDN博主「Blue Dream~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cxyxt/article/details/127728055

 

posted @ 2023-12-17 12:24  paul_hch  阅读(37)  评论(0编辑  收藏  举报