# Creating /etc/hosts from ansible hosts

**URL:** <https://forum.ansible.com/t/creating-etc-hosts-from-ansible-hosts/31549>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [October 27, 2019, 2:49am UTC](https://forum.ansible.com/t/creating-etc-hosts-from-ansible-hosts/31549 "2019-10-27T02:49:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![budhac](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@budhac](https://forum.ansible.com/u/budhac)\
**Post date:** [October 27, 2019, 2:49am UTC](https://forum.ansible.com/t/creating-etc-hosts-from-ansible-hosts/31549/1 "2019-10-27T02:49:18Z")

</div>

I am trying to figure out if there is a way to use Ansible to automate the creation of /etc/hosts files depending on which group the Ansible host belongs to. I am creating multiple test Kubernetes clusters that will use hosts files for DNS resolution.

`

[Group 1]  
kube-g1-master1 ansible\_host=10.90.1.11  
kube-g1-master2 ansible\_host=10.90.1.12  
kube-g1-master3 ansible\_host=10.90.1.13  
kube-g1-node1 ansible\_host=10.90.1.14  
kube-g1-node2 ansible\_host=10.90.1.15  
kube-g1-lb ansible\_host=10.90.1.16  
[Group 2]  
kube-g2-master1 ansible\_host=10.90.1.21  
kube-g2-master2 ansible\_host=10.90.1.22  
kube-g2-master3 ansible\_host=10.90.1.23  
kube-g2-node1 ansible\_host=10.90.1.24  
kube-g2-node2 ansible\_host=10.90.1.25  
kube-g2-lb ansible\_host=10.90.1.26  
[Group 3]  
kube-g3-master1 ansible\_host=10.90.1.31  
kube-g3-master2 ansible\_host=10.90.1.32  
kube-g3-master3 ansible\_host=10.90.1.33  
kube-g3-node1 ansible\_host=10.90.1.34  
kube-g3-node2 ansible\_host=10.90.1.35  
kube-g3-lb ansible\_host=10.90.1.36  
etc…

`

When I use **[all]**, as Ansible iterates through the hosts file can it search which Group the host belongs to and create an **/etc/hosts** from the group hostnames and IPs.

For example:

1. Ansible interacts with: **kube-g2-master3**

`  
inventory\_hostname = kube-g2-master3

`

1. Finds **kube-g2-master3** belongs **[Group 2]**

2. Creates **/etc/hosts** on **kube-g2-master3** using the **inventory\_hostname** and **ansible\_hosts** from **[Group 2]**

`  
/etc/hosts

kube-g2-master1 10.90.1.21  
kube-g2-master2 10.90.1.22  
kube-g2-master3 10.90.1.23  
kube-g2-node1 10.90.1.24  
kube-g2-node2 10.90.1.25  
kube-g2-lb 10.90.1.26

`

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 27, 2019, 7:48am UTC](https://forum.ansible.com/t/creating-etc-hosts-from-ansible-hosts/31549/2 "2019-10-27T07:48:14Z")

</div>

> I am trying to figure out if there is a way to use Ansible to automate the  
> creation of /etc/hosts files depending on which group the Ansible host  
> belongs to. I am creating multiple test Kubernetes clusters that will use  
> hosts files for DNS resolution.
> 
> [Group 1]  
> kube-g1-master1 ansible\_host=10.90.1.11  
> kube-g1-master2 ansible\_host=10.90.1.12  
> kube-g1-master3 ansible\_host=10.90.1.13  
> kube-g1-node1 ansible\_host=10.90.1.14  
> kube-g1-node2 ansible\_host=10.90.1.15  
> kube-g1-lb ansible\_host=10.90.1.16  
> [Group 2]  
> kube-g2-master1 ansible\_host=10.90.1.21  
> kube-g2-master2 ansible\_host=10.90.1.22  
> kube-g2-master3 ansible\_host=10.90.1.23  
> kube-g2-node1 ansible\_host=10.90.1.24  
> kube-g2-node2 ansible\_host=10.90.1.25  
> kube-g2-lb ansible\_host=10.90.1.26  
> [Group 3]  
> kube-g3-master1 ansible\_host=10.90.1.31  
> kube-g3-master2 ansible\_host=10.90.1.32  
> kube-g3-master3 ansible\_host=10.90.1.33  
> kube-g3-node1 ansible\_host=10.90.1.34  
> kube-g3-node2 ansible\_host=10.90.1.35  
> kube-g3-lb ansible\_host=10.90.1.36  
> etc...
> 
> When I use\* [all]\*, as Ansible iterates through the hosts file can it  
> search which Group the host belongs to and create an\* /etc/hosts\* from the  
> group hostnames and IPs.

Space in group names is not allowed.

> For example:
> 
> 1. Ansible interacts with: \*kube-g2-master3\*
> 
> inventory\_hostname = kube-g2-master3
> 
> 2. Finds \*kube-g2-master3\* belongs\* [Group 2]\*

Groups in Ansible is just for grouping variables, and the grouping is lost when Ansible is running.  
What you can do is check if a host is in a group, but you would need to check every group.  
&nbsp;&nbsp;&nbsp;when: "'kube-g2-master3' in groups['group2']"

And the list of all group is in the special variable "group\_names".

> 3. Creates \*/etc/hosts\* on \*kube-g2-master3\* using the \*inventory\_hostname\*  
> and \*ansible\_hosts\* from\* [Group 2]\*
> 
> /etc/hosts
> 
> kube-g2-master1 10.90.1.21  
> kube-g2-master2 10.90.1.22  
> kube-g2-master3 10.90.1.23  
> kube-g2-node1 10.90.1.24  
> kube-g2-node2 10.90.1.25  
> kube-g2-lb 10.90.1.26

Since you are building a host list, all you need in the variable groups['group2'] this is a list of all members in that group.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 27, 2019, 10:35am UTC](https://forum.ansible.com/t/creating-etc-hosts-from-ansible-hosts/31549/3 "2019-10-27T10:35:15Z")

</div>

Use "group\_names: List of groups the current host is part of"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

It's possible to create lists of [host, ip]. For example (given a host may be  
a member of multiple groups) the tasks below

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts: "{{ my\_hosts|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups[item]|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip(groups[item]|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, 'ansible\_host')|list)| list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ group\_names }}"

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts: "{{ my\_hosts|unique }}"

give

&nbsp;&nbsp;&nbsp;&nbsp;"my\_hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"test\_01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"10.1.0.51"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"test\_02",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"10.1.0.52"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"test\_03",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"10.1.0.53"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;]

Then create /etc/hosts. For example

&nbsp;&nbsp;&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: hosts.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/hosts

&nbsp;&nbsp;$ cat hosts.j2  
&nbsp;&nbsp;{% for item in my\_hosts %}  
&nbsp;&nbsp;{{ item.0 }} {{ item.1 }}  
&nbsp;&nbsp;{% endfor %}

Cheers,

&nbsp;&nbsp;-vlado
