---
- name: host file update - Local DNS update accross servers
  hosts: home
  gather_facts: yes
  become: true
  become_user: seandersont
  
  vars:
    ansible_become_pass: "{{ admin_password }}"

  vars_files:
    - group_vars/.vault_pass.txt

  tasks:
  - name: Update the /etc/hosts file with node name
    tags: etchostsupdate
    become: yes
    become_user: root 
    lineinfile:
      dest: /etc/hosts
      regexp: '.*{{ item }}$'
      line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}} {{ hostvars[item]['ansible_fqdn'] }}"
      state: present
    register: etchostsupdate
    when: hostvars[item]['ansible_facts']['default_ipv4']['address'] is defined
    with_items: "{{ groups['home'] }}"
