-8

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

<ipaddress>     <fqdn>     <hostname>
KKE
  • 135
  • 1
  • 2
  • 11

1 Answers1

2

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.

U880D
  • 597
  • 7
  • 17