# Update nodes /etc/hosts file from a master file

**URL:** <https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207>\
**Category:** Ansible Project\
**Created:** [April 28, 2019, 9:39am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207 "2019-04-28T09:39:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecrosby](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecrosby](https://forum.ansible.com/u/ecrosby)\
**Post date:** [April 28, 2019, 9:39am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/1 "2019-04-28T09:39:07Z")

</div>

I hope I am posting this to the correct area…

I’m really new to Ansible and am in the process of trying to learn it.  
Question: Does Ansible have the ability to look at a master file that contains host file information and deploy information from that master file to the /etc/hosts file on all the nodes in my environment so that when I add a new host all that I need to do is update that master file and redeploy to all my nodes appending, or replacing, that existing /etc/hosts file?

Thanks.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 28, 2019, 9:42am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/2 "2019-04-28T09:42:44Z")

</div>

If I understand correctly the master file you are talking about is playbook term in ansible. The playbook has all information related to hosts and deployment instruction. You can give the Input in playbook and it will modify the stuff accordingly.

---

<div class="post-metadata">

**Author:** ![ecrosby](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecrosby](https://forum.ansible.com/u/ecrosby)\
**Post date:** [April 28, 2019, 9:46am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/3 "2019-04-28T09:46:17Z")

</div>

Actually, I am referring to the master file outside the playbook.  
In other words I just have a plain text master file with all my host entries. Then, I create a playbook that references that master file to send to all nodes as reference to update their /etc/hosts file.  
Is that possible?

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 28, 2019, 9:53am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/4 "2019-04-28T09:53:14Z")

</div>

Yes. Use copy module. I hope so you are referring to inventory file which has all node details.  
Like below

/etc/ansible/inventory  
[all]  
host1  
host2  
host3

/etc/ansible/playbook/master  
host entry files

/etc/ansible/playbook/test.yaml

host: all  
tasks:

- name: copying host file to all nodes  
copy:  
src: /etc/ansible/playbook/master  
dest: /etc/hosts  
mode: 0755

---

<div class="post-metadata">

**Author:** ![ecrosby](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecrosby](https://forum.ansible.com/u/ecrosby)\
**Post date:** [April 29, 2019, 8:57am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/5 "2019-04-29T08:57:56Z")

</div>

No, I’m not referring to the inventory file, it will be a separate file that will have other hosts. However, you have pointed me in the right direction, thank you for that.  
Here is what I have come up with and this works, but my problem now is that every time there are new servers added, the existing servers will get the same hosts records all the time so there will be duplicates in the /etc/hosts. I haven’t been able to figure out how to look at the existing servers in the host file and only add new servers.:

_— # Copy hosts file_  
_- hosts: all_  
_become: yes_  
_tasks:_  
_- name: update hosts template file_  
_copy:_  
_src: /home/ansible/hostfile_  
_dest: /tmp/hostfile_  
_owner: root_  
_group: root_  
_mode: 0644_  
_backup: yes_

_- name: Append template file to master host file_  
_shell: cat /tmp/hostfile \>\> /etc/hosts_

_- name: Delete temp file_  
_file:_  
_path: /tmp/hostfile_  
_state: absent_

This works, but my problem now is that every time there are new servers added, the existing servers will get the same hosts records all the time so there will be duplicates in the /etc/hosts. I haven’t been able to figure out how to look at the existing servers in the host file and only add new servers.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 29, 2019, 9:07am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/6 "2019-04-29T09:07:31Z")

</div>

Why do you need that to append. If you have hosts file you can use copy to replace that role in every hosts file. I assume you have DNS server

---

<div class="post-metadata">

**Author:** ![ecrosby](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecrosby](https://forum.ansible.com/u/ecrosby)\
**Post date:** [April 29, 2019, 9:30am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/7 "2019-04-29T09:30:12Z")

</div>

I do not have a DNS server, hence the need to manually update the /etc/hosts file. This a small network with less than 50 clients.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 29, 2019, 9:38am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/8 "2019-04-29T09:38:09Z")

</div>

Can you show me one example of your hosts file from one of node

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [April 29, 2019, 10:37am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/9 "2019-04-29T10:37:23Z")

</div>

Hello,

I think you should use templating and jinja2 a bit in here. Will make the file easier to control and avoid appending. You can also use NetworkManager (if you use that).

---

<div class="post-metadata">

**Author:** ![ecrosby](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecrosby](https://forum.ansible.com/u/ecrosby)\
**Post date:** [April 30, 2019, 7:40am UTC](https://forum.ansible.com/t/update-nodes-etc-hosts-file-from-a-master-file/30207/10 "2019-04-30T07:40:14Z")

</div>

I think I found a way to do what I want to do. It’s dirty but it works:

_— # Copy hosts file_  
_- hosts: all_  
_become: yes_  
_tasks:_  
_- name: update hosts template file_  
_copy:_  
_src: /home/ansible/hostfile_  
_dest: /tmp/hostfile_  
_owner: root_  
_group: root_  
_mode: 0644_  
_backup: yes_

_- name: create master template file_  
_shell: diff /tmp/hostfile /etc/hosts | grep '\< ’ | sed ‘s/\< //’ \> /tmp/masterhosts_

_- name: Append template file to master host file_  
_shell: cat /tmp/masterhosts \>\> /etc/hosts_

_- name: Delete temp file_  
_file:_  
_path: /tmp/hostfile_  
_state: absent_

_- name: Delete master temp file_  
_file:_  
_path: /tmp/masterhosts_  
_state: absent_
