Q on hostvars access to inventory variables ad-hoc vs playbook

34 views
Skip to first unread message

countr...@gmail.com

unread,
May 2, 2019, 12:48:08 PM5/2/19
to Ansible Project
I seem to be able to query hostvars for a particular host from the command line and get at the inventory variables
but I haven't figured out how to do this in a playbook. Might it be from hostvars being lazily loaded - or my lack of
understanding how to access hostvars inventory variables successfully (or both).

I've search and haven't found anything quite this specific - or actually simple (just give me the inventory vars for this host via hostvars).

thx
jim

As an example, this works:

$ ansible prtmpftp -m debug -a "var=tags"
prtmpftp | SUCCESS => {
    "tags": {
        "Veeam_Backup": [
            "Linux_Production_Servers"
        ]
    }
}

I'm trying to do this:

---
# tasks file for is-veeam-backup

- name: inventory_hostname
  debug:
    var: inventory_hostname

- name: show tags
  debug:
    var: tags

- name: show tags
  debug:
    var: hostvars[inventory_hostname]

My aim is to do something special if a specific VMware tag and/or tag category exists.                               

With this in _meta for the sample host (in my inventory.json from a vmware_inventory.py run):

      "prtmpftp": {
        "ansible_ssh_host": "prtmpftp",
        "name": "prtmpftp",
        "parent": {
          "name": "Services",
          "parent": {
            "name": "Production",
            "parent": {
              "name": "Unix"
            }
          }
        },
        "tags": {
          "Veeam_Backup": [
            "Linux_Production_Servers"
          ]
        },
        "ansible_uuid": "d930e98b-e272-40ca-af60-34cfd946d917",
        "runtime": {
          "powerstate": "poweredOn"
        },
        "config": {
          "name": "prtmpftp",
          "template": false,
          "uuid": "421c76a6-4f0e-441d-e6c5-13cafddbe1c9"
        },
        "ansible_host": "prtmpftp",
        "guest": {
          "guestid": "centos6_64Guest",
          "guestfamily": "linuxGuest",
          "gueststate": "running",
          "hostname": "prtmpftp"
        }
      },

I get this from the playbook output:

$ ansible-playbook -i prtmpftp, is-veeam-backup.yml | more

PLAY [all] ***************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [prtmpftp]

TASK [is-veeam-backup : inventory_hostname] ******************************************************************************************************************************
ok: [prtmpftp] => {
    "inventory_hostname": "prtmpftp"
}

TASK [is-veeam-backup : show tags] ***************************************************************************************************************************************
ok: [prtmpftp] => {
    "tags": "VARIABLE IS NOT DEFINED!"
}

TASK [is-veeam-backup : show tags] ***************************************************************************************************************************************
ok: [prtmpftp] => {
    "hostvars[inventory_hostname]": {
        "ansible_all_ipv4_addresses": [
 
... trimmed off the rest of the facts ...

J Davis

unread,
May 15, 2019, 2:52:26 PM5/15/19
to Ansible Project
Let me simplify my issue. My inventory json includes the snippet above for a specific host. I'm looking for any vmware tags present. Ad-hoc debug gives this:

$ ansible prtmpftp -m debug -a "var=tags"
prtmpftp | SUCCESS => {
    "tags": {
        "Veeam_Backup": [
            "Linux_Production_Servers"
        ]
    }
}

$ ansible prtmpftp -m debug -a "var=hostvars[inventory_hostname]['tags']"
prtmpftp | SUCCESS => {
    "hostvars[inventory_hostname]['tags']": {
        "Veeam_Backup": [
            "Linux_Production_Servers"
        ]
    }
}

$ ansible prtmpftp -m debug -a "var=hostvars[inventory_hostname]['tags']['Veeam_Backup']"
prtmpftp | SUCCESS => {
    "hostvars[inventory_hostname]['tags']['Veeam_Backup']": [
        "Linux_Production_Servers"
    ]
}

However, in a playbook I can't reference the tags in hostvars for this host:

--
- hosts: all 

  tasks:
    - name: show tags
      debug:
        var: hostvars[inventory_hostname]['tags']

$ ansible-playbook -i prtmpftp, hostvarstest.yml

PLAY [all] ***************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [prtmpftp]

TASK [show tags] *********************************************************************************************************************************************************
ok: [prtmpftp] => {
    "hostvars[inventory_hostname]['tags']": "VARIABLE IS NOT DEFINED!"
}

In fact, if I dump all of hostvars via the playbook the 'tags' are not even there as if it isn't getting any vars from the inventory for this host.

Am I misunderstanding how inventory variables and hostvars works in a playbook vs ad-hoc? The documentation is not very helpful in this area.

flowerysong

unread,
May 15, 2019, 3:19:16 PM5/15/19
to Ansible Project
On Wednesday, May 15, 2019 at 2:52:26 PM UTC-4, J Davis wrote:
$ ansible-playbook -i prtmpftp, hostvarstest.yml

You told ansible-playbook to use 'prtmpftp,' as the inventory source; this does not include any host vars, since it's a simple comma-separated list of hostnames. You should instead use the same inventory you used in your adhoc command by running `ansible-playbook hostvarstest.yml`

J Davis

unread,
May 15, 2019, 3:50:20 PM5/15/19
to Ansible Project
That was it. Thanks!
Reply all
Reply to author
Forward
0 new messages