Variable used to lookup inventory value

159 views
Skip to first unread message

Gonzalo Servat

unread,
Nov 27, 2013, 7:48:33 PM11/27/13
to ansible-project
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

Michael DeHaan

unread,
Nov 27, 2013, 7:52:03 PM11/27/13
to ansible...@googlegroups.com
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)



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Gonzalo Servat

unread,
Nov 27, 2013, 7:54:05 PM11/27/13
to ansible-project
Perfect. That works. Again, thank you :)

GS

edroz...@gmail.com

unread,
May 1, 2014, 1:25:48 PM5/1/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
May 1, 2014, 5:28:34 PM5/1/14
to ansible...@googlegroups.com
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.




Michael Hoglan

unread,
Oct 15, 2014, 2:18:35 PM10/15/14
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages