-8

I would like to update /etc/hosts file using ansible playbook to all my 50 servers.

<ipaddress>     <fqdn>     <hostname>
2
  • 1
    What have you tried so far?
    – mfinni
    Jun 7, 2021 at 21:52
  • 1
    What’s your question?
    – tilleyc
    Jun 7, 2021 at 21:59

1 Answer 1

3

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.

1
  • Thanks you. This is what exactly looking for.
    – KKE
    Jun 8, 2021 at 22:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.