Template for /etc/hosts file replication

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:

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.

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.

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.

Hi,

As mentioned in the first email of the thread:

  • /etc/hosts file example to be replicated:

Hi everybody, any clue about this exercise with templates and ansible_facts?
your wisdom is truly appreciated :slight_smile:

Thank you in advance!

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;? 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

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.

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....

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.

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

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

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

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

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.