$ cat hosts.yaml --- - name: File /etc/hosts hosts: local tasks: - name: Testing entries in the /etc/hosts file lineinfile: path: /etc/hosts line: "10.240.0.200 ansible-server.juntotelecom.com.br ansible-server" register: etc_hosts_result - name: Clear file /etc/hosts when: etc_hosts_result is changed shell: echo '' > /etc/hosts register: etc_hosts_clear tags: hosts - name: Config file /etc/hosts when: etc_hosts_clear is succeeded lineinfile: path: /etc/hosts line: "{{ item.ip }} {{ item.fqdn }} {{ item.alias }}" with_items: - { ip: '127.0.0.1', fqdn: 'localhost.localdomain', alias: 'localhost' } - { ip: '10.240.0.200', fqdn: 'ansible-server.juntotelecom.com.br', alias: 'ansible-server' } - { ip: '10.240.0.201', fqdn: 'ub-vm-01.juntotelecom.com.br', alias: 'ub-vm-01' } - { ip: '10.240.0.202', fqdn: 'ol-vm-02.juntotelecom.com.br', alias: 'ol-vm-02' } - { ip: '10.240.0.203', fqdn: 'de-vm-03.juntotelecom.com.br', alias: 'de-vm-03' }