ansible.runner.HostVars object has no element

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.

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.

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]