# Template for /etc/hosts file replication

**URL:** <https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807>\
**Category:** Ansible Project\
**Created:** [September 14, 2020, 3:37pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807 "2020-09-14T15:37:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 14, 2020, 3:37pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/1 "2020-09-14T15:37:19Z")

</div>

Hi,

Id like to ask for help to solve the following case, as I don’t know how to solve it myself.  
Imagine that you have to **replicate “/etc/hosts” file** of the control node and spread it to specific managed nodes, **via J2 template**.  
“/etc/hosts” example:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 14, 2020, 4:05pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/2 "2020-09-14T16:05:15Z")

</div>

It looks like you want to make sure a number of hosts can consistently  
resolve each other's names.  
But I don't understand the problem you have. The file can just be the  
same (hence it's consistent)?

Either way, what is your source of truth?  
Start from there, for instance with a template.

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 15, 2020, 12:54pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/3 "2020-09-15T12:54:12Z")

</div>

Hi Dick,

Yes, the file already exists in the control node and it is consistent for just send it to the target nodes.  
But imagine for a second that you don’t have it (“/etc/hosts” file), and you want to create it in the target nodes following the conditions specified before:

- Using J2 template provided
- Following the same format of the “/etc/hosts” given
- To be forwarded to Nodes 1 & 2 only.

Thank you!  
Vicente.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 15, 2020, 1:42pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/4 "2020-09-15T13:42:57Z")

</div>

Hii

> Hi Dick,
> 
> Yes, the file already exists in the control node and it is consistent for just send it to the target nodes.  
> But imagine for a second that you don't have it ("/etc/hosts" file), and you want to create it in the target nodes following the conditions specified before:  
> - Using J2 template provided

So far you have not provided any template?

> - Following the same format of the "/etc/hosts" given

What is this format and how much "the same" is it compared to what else?

> - To be forwarded to Nodes 1 & 2 only.

Not entirely sure what you mean by 'forwarded' - but if it means  
'deploy a template', then it's a matter of just limiting your play to  
those hosts.

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 16, 2020, 12:34pm UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/5 "2020-09-16T12:34:09Z")

</div>

Hi,

As mentioned in the first email of the thread:

- /etc/hosts file example to be replicated:

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 21, 2020, 8:22am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/6 "2020-09-21T08:22:19Z")

</div>

Hi everybody, any clue about this exercise with templates and ansible\_facts?  
your wisdom is truly appreciated 🙂

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [September 22, 2020, 8:34am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/7 "2020-09-22T08:34:22Z")

</div>

Vicente Domínguez (16.09.20 14:34):

> Hi,
> 
> As mentioned in the first email of the thread:  
> - /etc/hosts file example to be replicated:  
> ------------------------  
> 127.0.0.1 localhost localhost.localdomain localhost4  
> localhost4.localdomain4  
> ::1 localhost localhost.localdomain localhost6  
> localhost6.localdomain6  
> 10.0.1.70 node1  
> 10.0.1.80 node2  
> 10.0.1.90 node3  
> 10.0.1.60 node4  
> ---------------------------  
> - Template hosts.j2  
> ------------------------  
> 127.0.0.1 localhost localhost.localdomain localhost4  
> localhost4.localdomain4  
> ::1 localhost localhost.localdomain localhost6  
> localhost6.localdomain6  
> ------------------------
> 
> Imagine that you have to reach the hosts example format from the template,  
> and to forward (exactly, "to deploy" the template) the file to the managed  
> nodes 1 & 2.

Hi Vicente,

I think I do not understand your problem at all.

As far as I get it, you have one file and you want to have the exact  
same file on two hosts. Is this correct?

If this is correct, why do you want to use a template and don't simply  
use the copy module as mentionend in  
\<[https://docs.ansible.com/ansible/latest/modules/copy\_module.html&gt;](https://docs.ansible.com/ansible/latest/modules/copy_module.html&gt;)? Like

- name: Copy file with owner and permissions  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;src: /your/local/location/of/hosts  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/hosts  
&nbsp;&nbsp;&nbsp;&nbsp;owner: root  
&nbsp;&nbsp;&nbsp;&nbsp;group: root  
&nbsp;&nbsp;&nbsp;&nbsp;mode: '0644'

Regards,  
Werner

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 22, 2020, 10:41am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/8 "2020-09-22T10:41:06Z")

</div>

Below:

> Vicente Domínguez (16.09.20 14:34):
> 
> > Hi,
> > 
> > As mentioned in the first email of the thread:
> > 
> > - /etc/hosts file example to be replicated:
> > 
> > * * *
> > 
> > ## 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.1.70 node1 10.0.1.80 node2 10.0.1.90 node3 10.0.1.60 node4
> > 
> > - Template hosts.j2
> > 
> > * * *
> > 
> > ## 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
> > 
> > Imagine that you have to reach the hosts example format from the template,  
> > and to forward (exactly, “to deploy” the template) the file to the managed  
> > nodes 1 & 2.
> 
> Hi Vicente,
> 
> I think I do not understand your problem at all.
> 
> As far as I get it, you have one file and you want to have the exact  
> same file on two hosts. Is this correct?

That is exactly the action that I have to perform. But, the point is that I do not have the file in my control node, I have the example of the file that’s supposed to be there, but it’s not… so I have to use the template given.

Let’s make it simple:

- premise: somebody has deleted the “/etc/hosts” file from the control node, but you have a picture of the last version and its content
- You have to use a template (given above) to set the file with the same content (provided above as well).  
\*\*\* All about IP’s and Hostnames to be obtained via Ansible-facts
- You have to install the template in the nodes 1 y 2 only (3-4 are up and running but you don’t have to install the template there)

Do you get my point? (btw, it’s not a real case, is an exercise proposed by a colleague to prepare Ansible Certification Exam)

Thank you!  
Vicente.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 22, 2020, 11:02am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/9 "2020-09-22T11:02:42Z")

</div>

It's still not very clear.

Let's make it even more simple.

1. Does the /etc/hosts file need to be exactly the same on each host?  
2. Is there any dynamic information at all in that /etc/hosts file?

If it wasn't clear, a template means that jinja variables or construct  
structures are used ( {{ blah }} or {% if blah %}).  
Your example is just static text....

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 22, 2020, 11:32am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/10 "2020-09-22T11:32:34Z")

</div>

> Below:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Vicente Domínguez (16.09.20 14:34):  
> &nbsp;&nbsp;&nbsp;&nbsp;\> Hi,  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> As mentioned in the first email of the thread:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - /etc/hosts file example to be replicated:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 127.0.0.1 localhost localhost.localdomain localhost4  
> &nbsp;&nbsp;&nbsp;&nbsp;\> localhost4.localdomain4  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ::1 localhost localhost.localdomain localhost6  
> &nbsp;&nbsp;&nbsp;&nbsp;\> localhost6.localdomain6  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 10.0.1.70 node1  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 10.0.1.80 node2  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 10.0.1.90 node3  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 10.0.1.60 node4  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ---------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - Template hosts.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 127.0.0.1 localhost localhost.localdomain localhost4  
> &nbsp;&nbsp;&nbsp;&nbsp;\> localhost4.localdomain4  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ::1 localhost localhost.localdomain localhost6  
> &nbsp;&nbsp;&nbsp;&nbsp;\> localhost6.localdomain6  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> Imagine that you have to reach the hosts example format from the template,  
> &nbsp;&nbsp;&nbsp;&nbsp;\> and to forward (exactly, "to deploy" the template) the file to the managed  
> &nbsp;&nbsp;&nbsp;&nbsp;\> nodes 1 & 2.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Hi Vicente,
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I think I do not understand your problem at all.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;As far as I get it, you have one file and you want to have the exact  
> &nbsp;&nbsp;&nbsp;&nbsp;same file on two hosts. Is this correct?
> 
> That is exactly the action that I have to perform. But, the point is that I do not have the file in my control node, I  
> have the example of the file that's supposed to be there, but it's not... so I have to use the template given.
> 
> Let's make it simple:  
> - premise: somebody has deleted the "/etc/hosts" file from the control node, but you have a picture of the last version  
> and its content  
> - You have to use a template (given above) to set the file with the same content (provided above as well).  
> \*\*\* All about IP's and Hostnames to be obtained via Ansible-facts  
> - You have to install the template in the nodes 1 y 2 only (3-4 are up and running but you don't have to install the  
> template there)
> 
> Do you get my point? (btw, it's not a real case, is an exercise proposed by a colleague to prepare Ansible Certification  
> Exam)
> 
> Thank you!  
> Vicente.

Vicente,

this exercise is really as far-fetched as it goes.

Please let us know where the information about the nodes (node1, node2, ...)  
comes from and how do you determine which nodes need to be updated.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 23, 2020, 9:47am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/11 "2020-09-23T09:47:17Z")

</div>

Hi Stefan, Dick,

About your questions:

1. Does the /etc/hosts file need to be exactly the same on each host? Afirmative (but only in 1 & 2, not in 3 & 4)
2. Is there any dynamic information at all in that /etc/hosts file? Afirmative. If you take a look to the “/etc/hosts” file that I want to install, there are both IP ADDR + HOSTNAME of the nodes. My template is static because it’s the static part of the file!! (let’s say, the header).

The point of the exercise is to fulfill the template (using of course {{ }} {% %} ), gathering information from 4 nodes using vars/ansible facts, BUT ONLY BEING INSTALLED IN THE NODES 1 & 2 (final folder or file name does not matter).

**Reminder** - final picture of the file installed in 1 & 2 (where the dynamic info is the IP and Hostname of each node):  
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6  
10.0.1.70 node1  
10.0.1.80 node2  
10.0.1.90 node3  
10.0.1.60 node4

Thank you in advance!  
Vicente

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 23, 2020, 10:03am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/12 "2020-09-23T10:03:07Z")

</div>

> Hi Stefan, Dick,
> 
> About your questions:
> 
> 1. Does the /etc/hosts file need to be exactly the same on each host? Afirmative (but only in 1 & 2, not in 3 & 4)  
> 2. Is there any dynamic information at all in that /etc/hosts file? Afirmative. If you take a look to the "/etc/hosts"  
> file that I want to install, there are both IP ADDR + HOSTNAME of the nodes. My template is static because it's the  
> static part of the file!! (let's say, the header).
> 
> The point of the exercise is to fulfill the template (using of course {{ }} {% %} ), gathering information from 4 nodes  
> using vars/ansible facts, \_BUT ONLY BEING INSTALLED IN THE NODES 1 & 2 (final folder or file name does not matter).\_  
> \_  
> \_  
> \*Reminder \*- final picture of the file installed in 1 & 2 (where the dynamic info is the IP and Hostname of each node):  
> 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4  
> ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6  
> 10.0.1.70 node1  
> 10.0.1.80 node2  
> 10.0.1.90 node3  
> 10.0.1.60 node4
> 
> Thank you in advance!  
> Vicente

Hello Vicente,

with the assumption that node1 ... node4 are in the group "mynodes", you can use the following in your template:

{% for host in groups['mynodes'] %}  
{{ hostvars[host]['ansible\_default\_ipv4']['address'] }} {{ host }}  
{% endfor %}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [September 23, 2020, 11:40am UTC](https://forum.ansible.com/t/template-for-etc-hosts-file-replication/33807/13 "2020-09-23T11:40:14Z")

</div>

Hi Racke,

That is a good one!

I did not know that you can retrieve ansible facts with hostvars magic variable…!!! I was studying magic variables present in Ansible Doc and it was not specified at a glance…  
hostvars

A dictionary/map with all the hosts in inventory and variables assigned to them

Now I ve gone deeper into the Doc and I discovered more detailed information about it… totally unknown for me until today…  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_vars\_facts.html#information-about-ansible-magic-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables)

I will give it a try in my test environment as soon as I can, I will let you know how It goes.  
Thank you so much for the idea and of course, your time.

Kind Regards,  
Vicente.
