Install and configure Ansible on the control node workstation.example.com as follows:
Install the required packages
Create a static inventory file called /home/student/ansible/inventory so that:
- node1 is a member of the dev host group
- node2 is a member of the test host group
- node3 and node4 are members of the prod host group
- node5 is a member of the balancers host group
- The prod group is a member of the webservers host group
Create a configuration file called /home/student/ansible/ansible.cfg so that:
- The host inventory file is /home/student/ansible/inventory
- The default content collections directory is /home/student/ansible/mycollections
- The default roles directory is /home/student/ansible/roles
【題前說明】
第一題只是很單純地依題目要求把環境和路徑都先設定好,唯一不同於上一個版本的地方就是多了mycollections的設定,這會影響到第三題和後面很多會用到roles的題目,記得當時寫解題的時候我把config檔內的mycollections路徑打錯了,因為卡住幾分鐘的時間被迫回頭檢查兩次後才發現,當時也算是冒了一點冷汗。
一.解題過程:
1.下載並安裝ansible
[student@workstation ~]sudo dnf install ansible-core -y
2.建立檔案資料夾
[student@workstation ~]$ mkdir ansible
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ touch inventory
[student@workstation ansible]$ mkdir mycollections
[student@workstation ansible]$ mkdir roles
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ touch inventory
[student@workstation ansible]$ mkdir mycollections
[student@workstation ansible]$ mkdir roles
3.依題目要求建立inventory
[student@workstation ansible]$ vim inventory
[dev]
node1
[test]
node2
[prod]node3
node4
[balancers]
node5
[webservers:children]
prod
用ping模組驗證看看是否都連線成功
[student@workstation ansible]$ ansible all -m ping
4.建立ansible.cfg(以前舊版的ansible有範本可以直接複製,但RHCE9之後只能用背的)
[student@workstation ansible]$ vim ansible.cfg
[defaults]inventory = /home/student/ansible/inventoryroles_path = /home/student/ansible/rolescollections_paths = /home/student/ansible/mycollections \\這是考RHCE9和8最大不同之處,8版並沒有這一個設定,所以也是很容易遺忘的重點,collection沒有設定的話後面題目就沒得做了remote_user = studentask_passwd = False
[privilege_escalation]become= Truebecome_method = sudobecome_user = rootbecome_passwd = False
二.恢復解題前的環境
為了要反覆練習,我在每一題都做了一個yml,可以讓環境恢復到解題以前的情況。例如本次題目的還原yml命名為 01-lab-config-stop.yml,這裡面移除ansible和所有的設定檔。
[student@workstation ansible]$ vim 01-lab-config-stop.yml
---
- name: remove epel-release and ansible
hosts: 127.0.0.1
tasks:
- name: remove epel-release and ansible-core
yum:
name: "{{ item }}"
state: absent
loop:
- epel-release
- ansible-core
- name: remove config and inventory
hosts: 127.0.0.1
tasks:
- name: remove config, inventory and mycollections
shell:
'for i in {ansible.cfg,inventory,mycollections}
do
rm -rf ~/ansible/$i
done'
[student@workstation ansible]$ ansible-playbook 01-lab-config-stop.yml
沒有留言:
張貼留言