Problem setting variable with bond0.11 IPv4 address from the gathered facts.

Hi,

I am using Ansible v1.7.1.

I am having trouble using the gathered facts when an interface has bonding and VLANs enabled:

ansible -m setup testserver

[…]

“ansible_bond0.11”: {
“active”: true,
“device”: “bond0.11”,
“ipv4”: {
“address”: “192.168.11.66”,
“netmask”: “255.255.255.0”,
“network”: “192.168.11.0”
},
“macaddress”: “d4:ae:52:98:71:1f”,
“mtu”: 1500,
“promisc”: false,
“type”: “ether”
},

[…]

Then, I have a playbook that sets a variable:

Example from a different server that doesn’t have bonding or VLANs:

listen_ip: “{{ ansible_eth1.ipv4.address }}”

Example that doesn’t work:

listen_ip: “{{ ansible_bond0.11.ipv4.address }}”

Ansible output:

fatal: [testserver] => {‘msg’: ‘AnsibleUndefinedVariable: One or more undefined variables: dict object has no element 11’, ‘failed’: True}
fatal: [testserver] => {‘msg’: ‘AnsibleUndefinedVariable: One or more undefined variables: dict object has no element 11’, ‘failed’: True}

FATAL: all hosts have already failed – aborting

I have tried different possibilities but they all failed:

“{{ ansible_bond0.11.ipv4.address }}”

“{{ ansible_bond0\.11.ipv4.address }}”

“{{ [ansible_bond0.11].ipv4.address }}”

“{{ {ansible_bond0.11}.ipv4.address }}”

“{{ ‘ansible_bond0.11’.ipv4.address }}”

I think Ansible is taking the VLAN tag as part of the subgroup in the config…

What is the correct way of selecting the IPv4 address of a bond0.11 interface?

Thank you in advanced.

Best regards,
J.

To use a hostvar that contains a ‘.’ you will need to do something such as:

{{ hostvars[inventory_hostname][‘ansible_bond0.11’][‘ipv4’][‘address’] }}

Hi Matt,

Would this be valid?

"{{ hostvars[{{ ansible_host }}]['ansible_bond0.11']['ipv4']['address'] }}"

This is the playbook:

“{{ hostvars[{{ ansible_host }}][‘ansible_bond0.11’][‘ipv4’][‘address’] }}”

You’ll want to remove the template stuff inside the template stuff:

“{{ hostvars[ansible_host][‘ansible_bond0.11’][‘ipv4’][‘address’] }}”

Hi Michael,

Just tried, same problem…

“{{ hostvars[ansible_host][‘ansible_bond0.11’][‘ipv4’][‘address’] }}”:

fatal: [testserver1] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible_host’ is undefined”, ‘failed’: True}
fatal: [testserver1] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible_host’ is undefined”, ‘failed’: True}

fatal: [testserver2] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible_host’ is undefined”, ‘failed’: True}
fatal: [testserver2] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘ansible_host’ is undefined”, ‘failed’: True}

FATAL: all hosts have already failed – aborting

Just in case, tried this one also:

“{{ hostvars[‘ansible_host’][‘ansible_bond0.11’][‘ipv4’][‘address’] }}”

fatal: [testserver1] => {‘msg’: ‘AnsibleError: host not found: ansible_host’, ‘failed’: True}
fatal: [testserver1] => {‘msg’: ‘AnsibleError: host not found: ansible_host’, ‘failed’: True}

fatal: [testserver2] => {‘msg’: ‘AnsibleError: host not found: ansible_host’, ‘failed’: True}
fatal: [testserver2] => {‘msg’: ‘AnsibleError: host not found: ansible_host’, ‘failed’: True}

FATAL: all hosts have already failed – aborting

Any other ideas?

Thank you!

J.

According to that error, you have not defined a variable called ‘ansible_host’. Where is that variable supposed to be coming from?

The variable that ansible creates that references the current host is called inventory_hostname. Perhaps you were meaning ansible_hostname?

In either case inventory_hostname is safer to use, as that is what ansible knows about from the inventory, and ansible_hostname is not necessarily the same value as inventory_hostname.

Hi Matt,

I just wanted to say that it worked perfectly as you said.

The correct format is:

“{{ hostvars[inventory_hostname][‘ansible_bond0.11’][‘ipv4’][‘address’] }}”

Thank you very much.
J.