carlessanagustin
10/26/2016 - 8:31 AM

ANSIBLE: Generate /etc/hosts with Ansible

ANSIBLE: Generate /etc/hosts with Ansible from https://gist.github.com/rothgar/8793800

---
- hosts: all
  gather_facts: yes
  tasks:
    - name: Update /etc/hosts
      become: true
      tags:
        - dns
      lineinfile:
        dest: /etc/hosts
        regexp: '.*{{ item }}$'
        line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ hostvars[item].inventory_hostname }} {{ hostvars[item].ansible_fqdn }}"
        state: present
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups.all }}"