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

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/70f5e68a-9512-4544-9c8b-263c4f20805b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to