由於本人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