# Ansible variables acess in host\_vars/file

**URL:** <https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946>\
**Category:** Ansible Project\
**Created:** [December 3, 2014, 9:08am UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946 "2014-12-03T09:08:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amanda\_Galligan](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Amanda\_Galligan](https://forum.ansible.com/u/Amanda_Galligan)\
**Post date:** [December 3, 2014, 9:08am UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/1 "2014-12-03T09:08:08Z")

</div>

Currently have a playbook that is creating template configs for our cisco switches. Most generic variables are kept in group\_vars and we're currently using individual host\_var file per switch to store variables unique to each sw e.g hostname, IP address etc..  
Our host\_var structure is as follows:  
host\_vars/tor01.a01  
host\_vars/tor02.a01

Within the INI inventory we reference each switch as follows:  
[cisco\_tor]  
tor01.a01  
tor02.a01

Is it possible to create a single file instead in host\_vars that contains a dict of all switches and access those variables in ansible play instead?  
host\_vars/tor\_switches

Similiar to example looping over hashes given on Ansible documentaion?  
Ive tried to do this by including with\_dict: tor\_switches in playbook but it errors during play run with "with\_dict must contain dict"

Should this be referenced in role/task/main.yml instead?

Amanda

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 4, 2014, 10:31pm UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/2 "2014-12-04T22:31:18Z")

</div>

You can use inventory groups as lists to iterate over

- debug:  
with\_items: groups [‘cisco\_switches’

---

<div class="post-metadata">

**Author:** ![Amanda\_Galligan](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Amanda\_Galligan](https://forum.ansible.com/u/Amanda_Galligan)\
**Post date:** [December 8, 2014, 7:31pm UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/3 "2014-12-08T19:31:38Z")

</div>

Brian  
Can you elaborate please? Im relatively new to using Ansible so not sure where the with\_items should be placed? Does this go into playbook or task YAML file within role?

My playbook is very simple and looks like this currently my inventory file is underneath.

[vagrant@dev4 RTR-TEMPLATE]$ more site.yml

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 8, 2014, 7:39pm UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/4 "2014-12-08T19:39:55Z")

</div>

ansible has 2 iterators, the main one is the play, it iterates over hosts

- hosts: routers

iterates over every host in the routers group

the second iterator is with\_ which acts at task level

- debug: msg={{item}}  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 2

this will iterate over 1 and 2 for every host in the play.

sometimes you want to target hosts from a different group from the one  
you are targetting in play, most common examples is load balancers

- hosts: webnodes  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: disable node {{inventory\_hostname}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: "{{item}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: groups['loadbalancers']

As to the specifics on how this fits with your setup that requries not  
as much the structure of your files but what is your intent, what you  
want to accomplish.

---

<div class="post-metadata">

**Author:** ![Amanda\_Galligan](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Amanda\_Galligan](https://forum.ansible.com/u/Amanda_Galligan)\
**Post date:** [December 9, 2014, 6:01pm UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/5 "2014-12-09T18:01:52Z")

</div>

Brian so the basic idea is using ansible to build out switch templates leveraging variables that are specific to each switch device.

For the purpose of this test I’ve made the template very simple I need to populate the variable {{ from inventory\_hostname }} and {{ip}} into the template below for each switch.

The {{ inventory hostname }} variable is retrieved from iterating over the hosts:routers in the play as you mention above

The {{ip}} variable is retrieved from host\_vars/tor01.a01.yml and host\_vars/tor01.a02.yml file

See example below for one of these switches. Again this is being kept very simple for demo purposes.

[vagrant@dev4 host\_vars]$ more tor01.a01.yml  
hostname: tor01.a01  
ip : 10.1.1.1

[vagrant@dev4 host\_vars]$

The basic template is below:

[vagrant@dev4 templates]$ more base.j2  
!  
hostname {{ inventory\_hostname}}  
!  
!  
interface GigabitEthernet0/0  
ip access-group INTERNET in  
ip address {{ ip }}  
no ip redirects  
no ip proxy-arp  
ip nat outside  
ip virtual-reassembly  
duplex auto  
speed auto  
no cdp enable  
!

[vagrant@dev4 templates]$

When I run the playbook it combines the variables and the templates to create a populated file for each switch that can be uploaded to the device.

[vagrant@dev4 RTR-TEMPLATE]$ ansible-playbook -i test site.yml

PLAY [Generate router configuration files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [router | Generate configuration files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [tor01.a02]  
changed: [tor01.a01]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
tor01.a01 : ok=1 changed=1 unreachable=0 failed=0  
tor01.a02 : ok=1 changed=1 unreachable=0 failed=0

This is fine as currently I only have two devices to template tor01.a01 and tor01.a02 but if this was several hundred devices I’d prefer to keep variables in single YAML file below and loop over this to extract the {{ip}} for each hostname?

Is this currently possible with Ansible?  
I’ve tried to do this but can’t seem to access the variable {{ip}} within tor\_switches.yml and not sure how I load variables from this file into Ansible for use in playbook?

[vagrant@dev4 RTR-TEMPLATE]$ cat host\_vars/tor\_switches.yml

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [December 9, 2014, 11:06pm UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/6 "2014-12-09T23:06:46Z")

</div>

If I understand correctly, you want:

1) One file that holds variable data for several related hosts.  
2) In a play, you will create one file from a template on each of the  
hosts in a group  
3) The template will contain variables specific to the host that were  
defined in the variable data file mentioned earlier.

I think a solution would look something like this:

(1) If the group you're working with is named "cisco\_tor", create a  
group\_var file for cisco\_tor. Do this by creating a group\_vars/  
directory next to your host\_vars/ directory.  
Then adding a "cisco\_tor" file in there.

Put your data structure into the file. For instance, you can just" mv  
host\_vars/tor\_switches.yml group\_vars/cisco\_tor"

(2) Your playbook will look something like this:

- hosts: cisco\_tor  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Create config file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: router.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/router.cfg

(3) The template looks something like this:

host={{ inventory\_hostname }}  
ip={{ hostname[inventory\_hostname].ip }}

-Toshio

---

<div class="post-metadata">

**Author:** ![Amanda\_Galligan](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Amanda\_Galligan](https://forum.ansible.com/u/Amanda_Galligan)\
**Post date:** [December 15, 2014, 11:18am UTC](https://forum.ansible.com/t/ansible-variables-acess-in-host-vars-file/16946/7 "2014-12-15T11:18:39Z")

</div>

Hey Toshio,  
Thanks very much that is exactly what I need see below. I’m able to access the dict within group\_vars/cisco\_tor.yml and extract variable {{ tor\_switch[inventory\_hostname].ip }} as per instructions you provided.  
End result is 3 switch device configs. Will port this back to my main Ansible tempting engine but looks like this is a working solution to my problems

Amanda

interface GigabitEthernet0/0  
ip address {{ tor\_switch[inventory\_hostname].ip }}  
ip access-group INTERNET in  
no ip redirects  
no ip proxy-arp  
ip nat outside  
ip virtual-reassembly  
duplex auto  
speed auto  
no cdp enable

[vagrant@dev4 RTR-TEMPLATE]$ more group\_vars/cisco\_tor.yml
