---
- hosts: all
  tasks:
    - blockinfile:
        dest: /etc/hosts
        block: "{{ hostvars[item].ansible_host }}    {{ item }}"
        marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item }}"
      with_items: "{{ groups['all'] }}" 
      become: true

---
- hosts: all
  tasks:
    - authorized_key:
        user: vagrant
        key: "{{ lookup('file', '/vagrant_data/id_rsa.pub') }}"

    - lineinfile:
        dest: /etc/hosts
        regexp: '.*127\.0\.0\.1.*{{ inventory_hostname }}'
        state: absent
      become: true
      
    - blockinfile:
        dest: /etc/hosts
        block: "{{ hostvars[item].ansible_host }}    {{ item }}"
        marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item }}"
      with_items: "{{ groups['all'] }}" 
      become: true

Update utilities script for synchronizing /etc/hosts with inventory file
Update utilities script for synchronizing /etc/hosts with inventory file
