Skip to content

Instantly share code, notes, and snippets.

@coolpalani
Forked from glennswest/setup-ip.yml
Created November 29, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolpalani/289a125557535d249aa42e5638f6cda1 to your computer and use it in GitHub Desktop.
Save coolpalani/289a125557535d249aa42e5638f6cda1 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
---
- 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'] }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment