--- - name: Add name/IP to hosts file lineinfile: dest: /etc/hosts regexp: '^{{ ansible_default_ipv4.address }}.*{{ ansible_hostname }}\.ansible\.kurs.*{{ ansible_hostname }}' line: '{{ ansible_default_ipv4.address }} {{ ansible_hostname }}.ansible.kurs {{ ansible_hostname }}' backup: yes - name: Clean/remove 127.0.1.1 lineinfile: dest: /etc/hosts regexp: '127.0.1.1.*{{ ansible_hostname }}' state: absent backup: yes - name: Clean/remove 127.0.0.1 ... Hostname lineinfile: dest: /etc/hosts regexp: '127.0.0.1.*{{ ansible_hostname }}' state: absent backup: yes - name: Ensure 127.0.0.1 localhost lineinfile: dest: /etc/hosts regexp: '127.0.0.1.*localhost' line: '127.0.0.1 localhost localhost.localdomain # from Ansible' insertbefore: BOF backup: yes