2024年8月12日 星期一

【IT Notes】RHCE測驗第十一題 Modify file content

 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
node5噴錯是正常的,因為它所屬群組並不在要被部署的主機內

[student@workstation ansible]$ ansible-playbook  issue.yml
看到node1到node4都成功的話就ok了

[student@workstation ansible]$ ansible all -m shell -a 'cat /etc/issue'
驗證並讀取/etc/issue看是否成功

三.恢復解題前的環境

[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




沒有留言:

張貼留言

【當兵回憶】八里連

 在我心目中新兵日記是天花板等級的軍教片,並不是說沒有比它更好的作品,只是因為它的在拍攝的背景和時間,也正是我在服兵役的同時,老婆很多次好奇問我,為什麼我總是放在YouTube的新兵日記直播,我都說我只放在有個聲音在,我沒有真正在看,但這行為仔細想想也好多年了吧!台詞聽到我都會背...