--- - name: Static DNS Lookup hosts: all gather_facts: true become: true tasks: - name: Add all hosts to /etc/hosts ansible.builtin.lineinfile: dest: /etc/hosts regexp: "^{{ hostvars[item]['ansible_facts']['default_ipv4']['address'] }}.+{{ hostvars[item].ansible_facts.fqdn }}.*$" line: "{{ hostvars[item]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[item].ansible_facts.fqdn }} {{ hostvars[item].ansible_facts.hostname }}" state: present with_items: "{{ groups.all }}"