I have 50 servers. Want to update /etc/hosts file using ansible [closed]

Solution 1:

Such a task is a good starting point for learning. So it was one of the first tasks I've implement in my own playbooks.

- name: Make sure an entry in /etc/hosts exists
  lineinfile:
    path: /etc/hosts
    regexp: "^{{ ansible_default_ipv4.address }}"
    line: "{{ ansible_default_ipv4.address }} {{ inventory_hostname }} {{ ansible_hostname }}"
    state: present
  tags: network,hostname,dns

Depending on your environment and configuration you might be able to use also

{{ ansible_eth0.ipv4.address }}

Other useful variables in this case are

{{ ansible_domain }}
{{ ansible_default_ipv6.address }}

I leave further research and testing to you.