Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generate /etc/hosts with Ansible
# 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
- 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']
Copy link

irigon commented Dec 9, 2021

@dywanik , I am not sure if that is what you are searching for... in my case, I wanted simply to add an entry to /etc/hosts.
The following worked for me:


  - name: "myplatformname"
      "repo": ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment