Create a playbook called /home/student/ansible/issue.yml as follows:
The playbook runs on all inventory hosts
The playbook replaces the contents of /etc/issue with a single line of text as follows:
- On hosts in the dev host group, the line reads: Development
- On hosts in the test host group, the line reads: Test
- On hosts in the prod host group, the line reads: Production
【題前說明】
這一題的重點是變數的運用,先建立一個變數用的資料夾,然後在裡面分別建立Development、Test和Production三個群組的檔案,檔案內容則是變數,最後再回到外面建立yml檔,內容要能夠去讀取變數檔,並且部署到三個群組的/etc/issue底下。整體來說是比較簡單的題目,感覺是給考生稍微放鬆和給分用的。
一.解題過程:
建立一個group_vars資料夾,然後裡面再分別新建變數檔案。
[student@workstation ansible]$ mkdir group_vars
[student@control ansible]$ echo "message: Development" > group_vars/dev
[student@control ansible]$ echo "message: Test" > group_vars/test
[student@control ansible]$ echo "message: Production" > group_vars/prod
[student@workstation ansible]$ ll group_vars/
total 12
-rw-r--r--. 1 student student 21 Sep 1 2023 dev
-rw-r--r--. 1 student student 20 Sep 1 2023 prod
-rw-r--r--. 1 student student 14 Aug 12 22:51 test
[student@workstation ansible]$ vim issue.yml \\部署的yml內要寫入可讀取變數的腳本
---
- name: create issue
hosts: all
tasks:
- name: create issue file
copy:
content: "{{ message }}"
dest: /etc/issue
二.驗證結果
[student@workstation ansible]$ ansible-playbook -C issue.yml
[student@workstation ansible]$ ansible-playbook issue.yml
三.恢復解題前的環境
[student@workstation ansible]$ vim 11-lab-issue-stop.yml
---
- name: remove issue files
hosts: all
tasks:
- name: remove issue files
file:
path: /etc/issue
state: absent
- name: remove issue.yml
hosts: 127.0.0.1
tasks:
- name: remove issue.yml
file:
path: /home/student/ansible/issue.yml
state: absent
[student@workstation ansible]$ ansible-playbook 11-lab-issue-stop.yml
沒有留言:
張貼留言