# define variables for multiple hosts in one file inside host\_vars

**URL:** <https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800>\
**Category:** Ansible Project\
**Created:** [April 28, 2020, 5:16pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800 "2020-04-28T17:16:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 28, 2020, 5:16pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/1 "2020-04-28T17:16:10Z")

</div>

Let’s say I have two groups of devices to configure, eos-group and junos-group.

Let’s focus on junos-group.  
I have 10 devices in the junos-group.  
Each device will be configured with a mgmt ip, hostname, interface, routes …etc

Inside of /etc/ansible/host\_vars/, I could create different files for each host: hostname1.yaml, hostname2.yaml …  
However, this doesn’t. scale, does it?

Can I just create one file (lets call it all\_junos\_hosts.yaml) inside /etc/ansible/host\_vars/, and inside of t his single file, I’ll use hierarchical structure to define variables for different hosts?  
E.g: my **_all\_junos\_hosts.yaml_** file will look like this:

10.10.10.10:  
hostname: hostname1  
data\_nexthop: 10.10.10.1  
interfaces:  
mgmt\_interface:  
name: em0  
ip: 10.10.10.10:

12.12.12.12:  
hostname: hostname2  
data\_nexthop: 12.12.12.1  
interfaces:  
mgmt\_interface:  
name: em0  
ip: 12.12.12.12

I read somewhere that if I create a file called all\_junos\_hosts.yaml inside of /etc/ansible/host\_vars/, ansible will think there is a host called all\_junos\_hosts.yaml.  
How do I get around this?

Basically all I want to do is, in my template, I’ll do something like _set interfaces management "{{ reference for mgmt interface for this host }}_" … etc

---

<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:** [April 28, 2020, 5:26pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/2 "2020-04-28T17:26:09Z")

</div>

First off, rename your groups to NOT have a hyphen. This is critical.  
Then, use group\_var/junos\_group/ and define the common stuff below there.  
Same for the group\_vars/eos\_group/

it's all laid out in  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)

Dick

---

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 28, 2020, 6:32pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/3 "2020-04-28T18:32:11Z")

</div>

@Dick … In fact, I have group\_vars/all.yaml and in there I have stuff like credentials and username/password, since this is common for all devices  
I can create group\_vars/eos\_group.yaml and group\_vars/junos\_group.yaml. However, this still does not solve the issue of having multiple variables for the multiple hosts in the same file.

Okay let’s say I now have **group\_vars/junos\_group.yaml**. Inside this file I have the defined variables for 10 different hosts.

10.10.10.10:  
hostname: hostname1  
data\_nexthop: 10.10.10.1  
interfaces:  
mgmt\_interface:  
name: em0  
ip: 10.10.10.10:

12.12.12.12:  
hostname: hostname2  
data\_nexthop: 12.12.12.1  
interfaces:  
mgmt\_interface:  
name: **em0**  
ip: 12.12.12.12

In my playbook, I can reference value **em0** with “{{ group\_vars[inventory\_hostname][‘interfaces’][‘mgmt\_interface’][‘name’] }}”

Is this correct?  
Thanks

---

<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:** [April 28, 2020, 6:57pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/4 "2020-04-28T18:57:02Z")

</div>

No. You are mixing up groups and hosts (and probably the inventory as well).  
All variables under group\_vars are meant to be specific to groups only  
- not hosts.  
Hosts specific variables go under their respective host\_vars files.

Host and group specific variables can be set in the inventory,  
group\_vars, and host\_vars.  
Setting stuff in the inventory is usually done for very simple structures.  
Given that your host specific data is already a nested dictionary, I  
would not use the inventory for that.

Your example indicates that the entire 'interfaces' dictionary is, in  
fact, not variable, it's the same for each host. You can add this to  
the group\_vars in this case:

&nbsp;&nbsp;interfaces:  
&nbsp;&nbsp;&nbsp;&nbsp;mgmt\_interface:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: em0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip: "{{ ansible\_hostname }}"

If your data is in fact different, please post a more accurate example.

---

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 28, 2020, 9:30pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/5 "2020-04-28T21:30:12Z")

</div>

That’s a nice suggestion and I agree it will solve the problem to an extent.  
I only showed a simple interface config here to keep things simple but the host specific configs is much more than just the mgmt em0 ip address. Let me give you a more realistic example:

10.10.10.10:  
hostname: hostname1  
data\_nexthop: 100.12.1.7  
interfaces:  
mgmt\_interface:  
name: em0  
ip: 10.10.10.10  
subnet: 31  
data\_interface:  
name: et-0/0/3  
ip: 100.12.1.6  
subnet: 31  
tor\_name: seattle\_router\_01

See the data\_interface is going to be different for each host, the data\_interface’s ip, next\_hop and it’s tor\_name are all specific to each host.

Would you recommend I put all of these host\_specific values in host file then? I mean the /etc/ansible/hosts?

Creating a yaml file for each host under /etc/ansible/host\_vars will be a bit too much.

---

<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:** [April 29, 2020, 7:53am UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/6 "2020-04-29T07:53:56Z")

</div>

What do you mean by "it will be a bit too much"?  
I'm not sure what problem is?

As I said before, the host\_vars directory is meant to house (at least)  
one file per host.  
That piece of YAML you posted is NOT going to work inside host\_vars  
(but see below).

The other way to define per-host variables is the inventory.  
What you have now already is almost exactly what a (YAML based)  
inventory file looks like.  
That can be a single file, so if that is what you are after (I'm  
deducting this from your statement about one file per host being "a  
bit too much" - but correct me if I'm wrong), then problem solved.

No matter how you look at it, you have a large number of hosts, each  
having an inherently complex (i.e. nested) set of specific host  
variables.

---

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 29, 2020, 9:00am UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/7 "2020-04-29T09:00:52Z")

</div>

Dick what I meant by “it will be a bit too much” is that I have over 100 hosts, and it will only increase. Do I really want to create a file for each of these hosts in the host\_vars directory?  
This does not seem like it will scale well. 1000 hosts means 1000 files in host\_vars directory.

Considering I have over 100 hosts, each with it’s own host-specific variables/values. And I need to use these variables/values in my jinja template to render a config for each host.  
You being the expert here, how do you think I should approach this?

- Create yaml files for each of the 100 hosts in /etc/ansible/host\_vars. ( host\_vars/host1, host2 …host100)
- Inside of my hosts file in /etc/ansible/hosts.yaml/ini, for each host in hosts, define variables/values for each host (I could use the hosts.ini too)
- or you’re thinking of something else? Or one the two above will work some tweaking?

Thanks for the support. My head hurts now.

---

<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:** [April 29, 2020, 9:54am UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/8 "2020-04-29T09:54:25Z")

</div>

^^  
This is the preferred choice in your case. To be clear, this is the  
"defining host variable in the inventory" approach.  
And because those variables are nested/complex, it's probably best to  
use a single YAML style inventory file.  
This is what it would be looking like:

dnmvisser@NUC8i5BEK ~$ cat inventory.yml

---

<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:** [April 29, 2020, 10:14am UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/9 "2020-04-29T10:14:56Z")

</div>

> Dick what I meant by "it will be a bit too much" is that I have over 100 hosts, and it will only increase. Do I really  
> want to create a file for each of these hosts in the host\_vars directory?  
> This does not seem like it will scale well. 1000 hosts means 1000 files in host\_vars directory.
> 
> Considering I have over 100 hosts, each with it's own host-specific variables/values. And I need to use these  
> variables/values in my jinja template to render a config for each host.  
> You being the expert here, how do you think I should approach this?  
> - Create yaml files for each of the 100 hosts in /etc/ansible/host\_vars. ( host\_vars/host1, host2 ...host100)  
> - Inside of my hosts file in /etc/ansible/hosts.yaml/ini, for each host in hosts, define variables/values for each host  
> (I could use the hosts.ini too)  
> - or you're thinking of something else? Or one the two above will work some tweaking?
> 
> Thanks for the support. My head hurts now.

Lots of files in host\_vars is probably not the solution, but otherwise you need to come up with a logic how this  
host values are determined. If you have such a logic, a dynamic inventory might be helpful.

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

---

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 29, 2020, 7:53pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/10 "2020-04-29T19:53:00Z")

</div>

Thanks for working with me Dick.  
I’ll I’ll follow your suggestion and report back.

Appreciated.

---

<div class="post-metadata">

**Author:** ![Momodou\_Fatty](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Momodou\_Fatty](https://forum.ansible.com/u/Momodou_Fatty)\
**Post date:** [April 29, 2020, 7:54pm UTC](https://forum.ansible.com/t/define-variables-for-multiple-hosts-in-one-file-inside-host-vars/32800/11 "2020-04-29T19:54:53Z")

</div>

Racke,  
Unfortunately there is no good pattern here for me to leverage. I’ll have to hardcode a lot of host-specific values for each host in an inventory file.  
And the template could pick up values from there.
