Template for /etc/hosts file replication

88 views
Skip to first unread message

vi1...@gmail.com

unread,
Sep 14, 2020, 11:37:19 AM9/14/20
to Ansible Project
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:
------------------------
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.80 node3
10.0.1.80 node4
---------------------------

In this case, you count on a .J2 template with the header within, and you have to set the file (using of course Ansible facts) and spread it to the NODES 1 and 2.
J2 template in control node:
------------------------
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
------------------------  

How would you do that?

I've been thinking long time about this problem with no success ... I am avoiding the following options, as I don't think they are the best way to perform the task (as J2 template is not used as a real template):
- copy_paste of the hosts file content to J2 template, then spread
- 4 plays, one for each node - delegate facts to localhost - write into the J2 in localhost - spread the template in a last 5th play to nodes 1 and 2

Thank you so much in advance!!!! Really appreciate your help here!

Dick Visser

unread,
Sep 14, 2020, 12:05:53 PM9/14/20
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4096be9a-9102-4994-8cff-23d276a6c8f2n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Vicente Domínguez

unread,
Sep 15, 2020, 8:54:53 AM9/15/20
to ansible...@googlegroups.com
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.

El lun., 14 sept. 2020 a las 18:05, Dick Visser (<dick....@geant.org>) escribió:
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.


On Mon, 14 Sep 2020 at 17:37, vi1...@gmail.com <vi1...@gmail.com> wrote:
>
> 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:
> ------------------------
> 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

> ---------------------------
>
> In this case, you count on a .J2 template with the header within, and you have to set the file (using of course Ansible facts) and spread it to the NODES 1 and 2.
> J2 template in control node:
> ------------------------
> 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
> ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
> ------------------------
>
> How would you do that?
>
> I've been thinking long time about this problem with no success ... I am avoiding the following options, as I don't think they are the best way to perform the task (as J2 template is not used as a real template):
> - copy_paste of the hosts file content to J2 template, then spread
> - 4 plays, one for each node - delegate facts to localhost - write into the J2 in localhost - spread the template in a last 5th play to nodes 1 and 2
>
> Thank you so much in advance!!!! Really appreciate your help here!
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4096be9a-9102-4994-8cff-23d276a6c8f2n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Sep 15, 2020, 9:43:34 AM9/15/20
to ansible...@googlegroups.com
Hii

On Tue, 15 Sep 2020 at 14:54, Vicente Domínguez <vi1...@gmail.com> wrote:
>
> 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.

Vicente Domínguez

unread,
Sep 16, 2020, 8:34:38 AM9/16/20
to ansible...@googlegroups.com
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.

Thank you in advance!

Kind Regards,
Vicente.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Vicente Domínguez

unread,
Sep 21, 2020, 4:22:51 AM9/21/20
to ansible...@googlegroups.com
Hi everybody, any clue about this exercise with templates and ansible_facts?
your wisdom is truly appreciated :) 

Thank you in advance!

Werner Flamme

unread,
Sep 22, 2020, 4:34:48 AM9/22/20
to ansible...@googlegroups.com
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>? Like

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

Regards,
Werner
--


Vicente Domínguez

unread,
Sep 22, 2020, 6:41:44 AM9/22/20
to ansible...@googlegroups.com
Below:

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.

Dick Visser

unread,
Sep 22, 2020, 7:03:21 AM9/22/20
to ansible...@googlegroups.com
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....
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ_nbpDWL7_uvYf2Q94ScrwYPfpfa%2BRQBB1mxUd8scT1fzN3iw%40mail.gmail.com.

Stefan Hornburg (Racke)

unread,
Sep 22, 2020, 7:33:12 AM9/22/20
to ansible...@googlegroups.com
On 9/22/20 12:41 PM, Vicente Domínguez wrote:
> Below:
>
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
Racke


>  
>
>
> 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>? Like
>
> - name: Copy file with owner and permissions
>   copy:
>     src: /your/local/location/of/hosts
>     dest: /etc/hosts
>     owner: root
>     group: root
>     mode: '0644'
>
>
>
>
> Regards,
> Werner
>
> >
> > Thank you in advance!
> >
> > Kind Regards,
> > Vicente.
> >
> > El mar., 15 sept. 2020 a las 15:43, Dick Visser (<dick....@geant.org <mailto:dick....@geant.org>>)
> >> email to ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> >> To view this discussion on the web visit
> >>
> https://groups.google.com/d/msgid/ansible-project/CAL8fbwMScupTWFTwojYYd9wP1%3DHx7HqKZL6d8%3Dat4eBYDGWWqg%40mail.gmail.com
> >> .
> >>
> >
>
>
> --
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAJ_nbpDWL7_uvYf2Q94ScrwYPfpfa%2BRQBB1mxUd8scT1fzN3iw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAJ_nbpDWL7_uvYf2Q94ScrwYPfpfa%2BRQBB1mxUd8scT1fzN3iw%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vicente Domínguez

unread,
Sep 23, 2020, 5:47:54 AM9/23/20
to ansible...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3e64e689-aadd-cea0-72a4-cb4d1c688dc1%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Sep 23, 2020, 6:04:00 AM9/23/20
to ansible...@googlegroups.com
On 9/23/20 11:47 AM, Vicente Domínguez wrote:
> 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
Racke

>
> El mar., 22 sept. 2020 a las 13:33, Stefan Hornburg (Racke) (<ra...@linuxia.de <mailto:ra...@linuxia.de>>) escribió:
>
> On 9/22/20 12:41 PM, Vicente Domínguez wrote:
> > Below:
> >
> > El mar., 22 sept. 2020 a las 10:34, Werner Flamme (<werner...@ufz.de <mailto:werner...@ufz.de>
> <mailto:werner...@ufz.de <mailto:werner...@ufz.de>>>) escribió:
> <mailto:dick....@geant.org <mailto:dick....@geant.org>>>)
> >     > escribió:
> >     >
> >     >> Hii
> >     >>
> >     >> On Tue, 15 Sep 2020 at 14:54, Vicente Domínguez <vi1...@gmail.com <mailto:vi1...@gmail.com>
> <mailto:vi1...@gmail.com <mailto:vi1...@gmail.com>>> wrote:
> >     >>>
> >     >>> 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.
> >     >>
> >     >> --
> >     >> Dick Visser
> >     >> Trust & Identity Service Operations Manager
> >     >> GÉANT
> >     >>
> >     >> --
> >     >> You received this message because you are subscribed to the Google Groups
> >     >> "Ansible Project" group.
> >     >> To unsubscribe from this group and stop receiving emails from it, send an
> >     >> email to ansible-proje...@googlegroups.com
> <mailto:ansible-project%2Bunsu...@googlegroups.com> <mailto:ansible-project%2Bunsu...@googlegroups.com
> <mailto:ansible-project%252Buns...@googlegroups.com>>.
> >     >> To view this discussion on the web visit
> >     >>
> >   
>  https://groups.google.com/d/msgid/ansible-project/CAL8fbwMScupTWFTwojYYd9wP1%3DHx7HqKZL6d8%3Dat4eBYDGWWqg%40mail.gmail.com
> >     >> .
> >     >>
> >     >
> >
> >
> >     --
> >
> >
> >     --
> >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >     https://groups.google.com/d/msgid/ansible-project/d8f7487a-66a5-30e1-c402-2a9309ab83f6%40ufz.de.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/CAJ_nbpDWL7_uvYf2Q94ScrwYPfpfa%2BRQBB1mxUd8scT1fzN3iw%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAJ_nbpDWL7_uvYf2Q94ScrwYPfpfa%2BRQBB1mxUd8scT1fzN3iw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3e64e689-aadd-cea0-72a4-cb4d1c688dc1%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAJ_nbpAO170jts2p0gYua9XB69fChRj%2B77hLmN%3DFiAYKB7p_ag%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAJ_nbpAO170jts2p0gYua9XB69fChRj%2B77hLmN%3DFiAYKB7p_ag%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Vicente Domínguez

unread,
Sep 23, 2020, 7:40:47 AM9/23/20
to ansible...@googlegroups.com
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...


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.



To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/463de94d-f451-f149-fc98-ba8324000670%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages