ansible.runner.HostVars object has no element

1,267 views
Skip to first unread message

David Neudorfer

unread,
Mar 21, 2014, 10:53:39 AM3/21/14
to ansible...@googlegroups.com
Ansible is telling me that the HostVars object has no element, but when I run setup I see the object!

setup:

        "ansible_eth1": {
            "active": true, 
            "device": "eth1", 
            "ipv4": {
                "address": "10.209.169.109", 
                "netmask": "255.255.224.0", 
                "network": "10.209.160.0"


playbook run:

TASK: [hibbert_db | Create a new database user for hibbert_backend.* with name hibbert_user and all privileges from local] *** 
<10.209.169.109> ESTABLISH CONNECTION FOR USER: mozart on PORT 22 TO 10.209.169.109
fatal: [s-mysql01] => One or more undefined variables: ansible.runner.HostVars object has no element [0, 's-mysql01']

the task:

- name: Create a new database user for {{ db_name }}.* with name {{ db_user }} and all privileges from local
  tags:
    - user
    - permissions
  mysql_user: host={{ hostvars[item]['ansible_eth1']['ipv4']['address'] }} name={{ db_user }} password={{ db_password }} priv={{ db_root_name }}.*:ALL state=present
  with_indexed_items: groups['hibbert_db']
  sudo: yes

  Does anyone have any ideas? Google has been no help.

James Tanner

unread,
Mar 21, 2014, 1:55:43 PM3/21/14
to ansible...@googlegroups.com
Before the mysql_user task, add a debug task to check the hostvars data structure …

- debug: var=hostvars

That should tell you what is available during the play execution.

--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6cd2d2fb-fecb-4c2a-8048-64fc054a15b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Mar 21, 2014, 4:18:15 PM3/21/14
to ansible...@googlegroups.com
With_index_items returns a two item list.

The first value items[0] is the index, the value items[1] is the name of the host, which is what you actually do see in the output above.

Switching to "with_items" will resolve this with no changes, otherwise in the dereference of hostvars change items to items[1]




Reply all
Reply to author
Forward
0 new messages