2024年7月25日 星期四

【IT Notes】RHCE測驗第十題 Generate a hosts file

  • Download an initial template file from http://example.classroom.com/cd/materials/hosts.j2 to /home/student/ansible

  • Complete the template so that it can be used to generate a file with a line for each inventory host in the same format as /etc/hosts

  • Download the file from http://example.classroom.com/cd/materials/hosts.yml to /home/student/ansible/hosts.yml. This playbook will use the template to generate the file /etc/myhosts on hosts in the dev host group.

    • Do not make any changes to the playbook.

When the playbook is run, the file /etc/myhosts on hosts in the dev host group should have a line for each managed host:

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.250.10 node1.example.com node1
172.25.250.11 node2.example.com node2
172.25.250.12 node3.example.com node3
172.25.250.13 node4.example.com node4
172.25.250.14 node5.example.com node5

NOTE: The order in which the inventory host names appear is not important.

【題前說明】

這一題要做的是去data主機下載兩個檔案,一個是hosts.j2作為template;另一個是hosts.yml作部署用檔案,比較困難的是又要用到查詢ansible facts的能力,將查詢到的node上的facts,寫在hosts.j2裡面當作變數,並且作一個迴圈,讓最後ansible跑部署的時候把五台node的IP address、FQDN和Hostname寫成一個myhost檔,然後只放在dev群組的主機指定路徑底下。


一.解題過程:

[student@workstation ansible]$ wget http://example.classoom.com/cd/exam_ehce8/hosts.j2
[student@workstation ansible]$ wget http://example.classoom.com/cd/exam_ehce8/hosts.yml
[student@workstation ansible]$ vim 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_facts.default_ipv4.address }} {{ hostvars[host].ansible_facts.fqdn }} {{ hostvars[host].ansible_facts.hostname }}
{% endfor %}


[student@workstation ansible]$ vim hosts.yml
---
- name: create myhosts
  hosts: all
  tasks:
    - name: create myhosts
      template:
        src: hosts.j2
        dest: /etc/myhosts
      when: ansible_hostname in groups.dev


二.驗證結果

[student@workstation ansible]$ ansible-play -C hosts.yml


[student@workstation ansible]$ ansible-play hosts.yml


[student@workstation ansible]$ ansible dev -m shell -a 'cat /etc/myhosts'

驗證myhosts有沒有出現在dev群組中主機所被指定的路徑下


三.恢復解題前的環境
[student@workstation ansible]$ vim 10-lab-hosts-stop.yml
---

- name: remove hosts.yml
  hosts: 127.0.0.1
  tasks:
    - name: remove hosts.yml
      file:
        path: /home/student/ansible/hosts.yml
        state: absent

- name: remove hosts.j2
  hosts: 127.0.0.1
  tasks:
    - name: remove hosts.j2
      file:
        path: /home/student/ansible/hosts.j2
        state: absent

- name: remove dev myhosts
  hosts: dev
  tasks:
    - name: remove dev myhosts
      file:
        path: /etc/myhosts
        state: absent


[student@workstation ansible]$ ansible-playbook 10-lab-hosts-stop.yml


沒有留言:

張貼留言

【當兵回憶】八里連

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