YJWANG

Ansible /etc/hosts 자동화 본문

91.IaC

Ansible /etc/hosts 자동화

왕영주 2020. 11. 2. 14:02

파일 준비

[root@yjwang1102-deploy-1 pre-ansible]# cat inventory 
[ceph]
ceph01 ansible_host=10.10.90.30
ceph02 ansible_host=10.10.90.31
ceph03 ansible_host=10.10.90.32
[root@yjwang1102-deploy-1 pre-ansible]# cat hosts.yml 
---
- name: gen etc hosts
  hosts: all
  tasks:
          - name: lineinfile
            lineinfile:
                    path: /etc/hosts
                    line: "{{ hostvars[item]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[item]['ansible_facts']['hostname'] }}"
            loop: "{{ ansible_play_hosts }}"
...

or

---
- name: gen etc hosts
  hosts: all
  tasks:
          - name: lineinfile
            lineinfile:
                    path: /etc/hosts
                    line: "{{ hostvars[item]['ansible_host'] }} {{ hostvars[item]['inventory_hostname'] }}"
            loop: "{{ ansible_play_hosts }}"

 

수행

[root@yjwang1102-deploy-1 pre-ansible]# ansible-playbook -i inventory hosts.yml 

 

 

반응형