Created
April 7, 2017 04:57
-
-
Save glennswest/6e43aa88f3de0a0cf4ecf00749a91fa1 to your computer and use it in GitHub Desktop.
Ansible automatically create /etc/hosts file for all host - to give nice short names using dnsmasq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
tasks: [] | |
- hosts: all | |
gather_facts: True | |
tasks: | |
- name: check connection | |
ping: | |
- name: setup | |
setup: | |
- name: "Build hosts file" | |
lineinfile: dest=/etc/hosts | |
state=present | |
dest=/etc/hosts | |
regexp='.*{{ item }}$' line="{{ hostvars[item].ansible_default_ipv4.address }} {{item}}" | |
when: hostvars[item].ansible_default_ipv4.address is defined | |
with_items: "{{ groups['all'] }}" | |
- hosts: localhost | |
gather_facts: True | |
tasks: | |
- name: check connection | |
ping: | |
- name: setup | |
setup: | |
- name: "Build hosts file" | |
lineinfile: dest=/etc/hosts | |
state=present | |
dest=/etc/hosts | |
regexp='.*{{ item }}$' line="{{ hostvars[item].ansible_default_ipv4.address }} {{item}}" | |
when: hostvars[item].ansible_default_ipv4.address is defined | |
with_items: "{{ groups['all'] }}" | |
Ive switched to a operator concept in 4.x
And support sematucs for cleanup
…Sent from my iPhone
On 1 Jun 2019, at 7:54 AM, Andrey Tkachenko ***@***.***> wrote:
What if you remove one host from inventory? How are you going to cleanup removed hosts?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
Little-bit nasty solution, but it works... (but slow as hell, version: Ansible 2.5.1)
- when: hostvars[item].ansible_default_ipv4.address is defined
+ when: (hostvars[item] is defined) and (hostvars[item]['ansible_default_ipv4'] is defined) and (hostvars[item]['ansible_default_ipv4']['address'] is defined)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if you remove one host from inventory? How are you going to cleanup removed hosts?