Skip to content

Instantly share code, notes, and snippets.

@cemo
Forked from rothgar/main.yml
Last active August 29, 2015 14:13
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 cemo/4e8ec243dc8318b6bbf4 to your computer and use it in GitHub Desktop.
Save cemo/4e8ec243dc8318b6bbf4 to your computer and use it in GitHub Desktop.
# Idempotent way to build a /etc/hosts file with Ansible using your Ansible hosts inventory for a source.
# Will include all hosts the playbook is run on.
# Inspired from http://xmeblog.blogspot.com/2013/06/ansible-dynamicaly-update-etchosts.html
- name: "Build hosts file"
lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[item].ansible_default_ipv4.address }} {{item}}" state=present
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