# Variable used to lookup inventory value

**URL:** <https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021>\
**Category:** Ansible Project\
**Created:** [November 28, 2013, 12:48am UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021 "2013-11-28T00:48:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 28, 2013, 12:48am UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/1 "2013-11-28T00:48:33Z")

</div>

Hi All,

This might be more of a Jinja question, but I couldn’t find an answer to this.

Say I have an inventory value that stores the main NIC name and I want to lookup the IP address for it from a fact. How can this be done in the one line? Non working example:

debug: msg=“ip address is {{ ansible\_{{ main\_nic }}.ipv4.address }}”

GS

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 28, 2013, 12:52am UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/2 "2013-11-28T00:52:03Z")

</div>

You can build variables dynamically through hostvars, which is a hash of all variables, host by host:

{{ hostvars[inventory\_hostname][“ansible\_” + main\_nic].ipv4.address }}

inventory\_hostname is the name of the current host in the host loop.

(Nesting templates in templates definitely will not work)

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 28, 2013, 12:54am UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/3 "2013-11-28T00:54:05Z")

</div>

Perfect. That works. Again, thank you 🙂

GS

---

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 1, 2014, 5:25pm UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/4 "2014-05-01T17:25:48Z")

</div>

Hi Michael,

I tried using the {{ hostvars[inventory\_hostname] trick but that variable  
space does not include variables defined in a role such as “myrole/vars/main.yaml.”  
How can I programmatically reference variables defined within a role?

Thank you,  
–Ed

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 1, 2014, 9:28pm UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/5 "2014-05-01T21:28:34Z")

</div>

I believe there was a ticket about hostvars not including _all_ of the variables, which it should, that should now be resolved.

If you still see this in 1.6 let me know.

---

<div class="post-metadata">

**Author:** ![Michael\_Hoglan](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Michael\_Hoglan](https://forum.ansible.com/u/Michael_Hoglan)\
**Post date:** [October 15, 2014, 6:18pm UTC](https://forum.ansible.com/t/variable-used-to-lookup-inventory-value/14021/6 "2014-10-15T18:18:35Z")

</div>

Sorry to drag up an old thread, but this was one of the closer conversations out of the ones I found for what I was seeing.

Looking through old tickets, it looks like there used to be an issue when accessing hostvars[inventory\_hostname] that you would not get all the vars associated with the host, such as ones added from add\_host call. I see this does work, however, if I debug just the hostvars itself (not hostvars[inventory\_hostname]) I don’t get the same results.

The var does exist, and it can be used in plays, I mainly keep running into this as I am debugging and trying to see what vars exist for all hosts

Given a host file of:

> [launched]
> 
> 10.0.3.234 test\_var=test
> 
> 10.0.3.162 test\_var=test

And a playbook of

> * * *
> 
> - name: Playbook testing inventory var debugging
> 
> hosts: launched
> 
> user: root
> 
> gather\_facts: yes
> 
> tasks:
> 
> - name: Output inventory var
> 
> debug: var=test\_var
> 
> - name: Output inventory var from hostvars
> 
> debug: var=hostvars[inventory\_hostname].test\_var
> 
> - name: Output hostvars[inventory\_hostname]
> 
> debug: var=hostvars[inventory\_hostname]
> 
> - name: Output hostvars
> 
> debug: var=hostvars

The last debug for var=hostvars will not output the vars from the inventory file. The var exists, so this is kind of cosmetic, but it threw me off for a bit as I was working with add\_host for dynamic EC2 provisioning and wasn’t seeing the vars I was expecting when doing debug hostvars to look at all the vars for all hosts.

> (ansible)[ec2-user@ip-10-0-0-226 playbooks]$ ansible-playbook -i launched test\_inventory\_var\_debug.yml
> 
> PLAY [Playbook testing inventory var debugging] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [10.0.3.162]
> 
> ok: [10.0.3.234]
> 
> TASK: [Output inventory var] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [10.0.3.234] =\> {
> 
> “test\_var”: “test”
> 
> }
> 
> ok: [10.0.3.162] =\> {
> 
> “test\_var”: “test”
> 
> }
> 
> TASK: [Output inventory var from hostvars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [10.0.3.234] =\> {
> 
> “hostvars[inventory\_hostname].test\_var”: “test”
> 
> }
> 
> ok: [10.0.3.162] =\> {
> 
> “hostvars[inventory\_hostname].test\_var”: “test”
> 
> }
> 
> TASK: [Output hostvars[inventory\_hostname]] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [10.0.3.234] =\> {
> 
> “hostvars[inventory\_hostname]”: {
> 
> “ansible\_all\_ipv4\_addresses”: [
> 
> “10.0.3.234”
> 
> ],
> 
> …
> 
> “ansible\_virtualization\_type”: “xen”,
> 
> **“group\_names”: [**
> 
> **“launched”**
> 
> **],**
> 
> **“inventory\_hostname”: “10.0.3.234”,**
> 
> **“inventory\_hostname\_short”: “10”,**
> 
> **“module\_setup”: true,**
> 
> **“test\_var”: “test”**
> 
> }
> 
> }
> 
> ok: [10.0.3.162] =\> {
> 
> “hostvars[inventory\_hostname]”: {
> 
> “ansible\_all\_ipv4\_addresses”: [
> 
> “10.0.3.162”
> 
> ],
> 
> …
> 
> “ansible\_virtualization\_type”: “xen”,
> 
> **“group\_names”: [**
> 
> **“launched”**
> 
> **],**
> 
> **“inventory\_hostname”: “10.0.3.162”,**
> 
> **“inventory\_hostname\_short”: “10”,**
> 
> **“module\_setup”: true,**
> 
> **“test\_var”: “test”**
> 
> }
> 
> }
> 
> TASK: [Output hostvars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [10.0.3.234] =\> {
> 
> “hostvars”: {
> 
> “10.0.3.162”: {
> 
> “ansible\_all\_ipv4\_addresses”: [
> 
> “10.0.3.162”
> 
> ],
> 
> …
> 
> “ansible\_virtualization\_type”: “xen”,
> 
> “module\_setup”: true
> 
> }
> 
> }
> 
> }
> 
> ok: [10.0.3.162] =\> {
> 
> “hostvars”: {
> 
> “10.0.3.162”: {
> 
> “ansible\_all\_ipv4\_addresses”: [
> 
> “10.0.3.162”
> 
> ],
> 
> …
> 
> “ansible\_virtualization\_type”: “xen”,
> 
> “module\_setup”: true
> 
> }
> 
> }
> 
> }
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> 10.0.3.162 : ok=5 changed=0 unreachable=0 failed=0
> 
> 10.0.3.234 : ok=5 changed=0 unreachable=0 failed=0

Anyways, let me know if I misunderstand this or if there is a different var to be looking at for the inventory vars. I can open an issue ticket also.

Thanks!  
Michael
