Score:-8

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

cn flag
KKE

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

<ipaddress>     <fqdn>     <hostname>
mfinni avatar
cn flag
What have you tried so far?
tilleyc avatar
us flag
What’s your question?
Score:2
ca flag

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.

KKE avatar
cn flag
KKE
Thanks you. This is what exactly looking for.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.