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

461 views
Skip to first unread message

Joel Sdc

unread,
Nov 4, 2014, 2:51:08 PM11/4/14
to ansible...@googlegroups.com
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.

Matt Martz

unread,
Nov 4, 2014, 3:22:24 PM11/4/14
to ansible...@googlegroups.com
To use a hostvar that contains a '.' you will need to do something such as:

{{ hostvars[inventory_hostname]['ansible_bond0.11']['ipv4']['address'] }}

--
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/15e60e0b-879b-4ce2-8935-717f5be40cf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Joel Sdc

unread,
Nov 4, 2014, 5:26:02 PM11/4/14
to ansible...@googlegroups.com
Hi Matt, 

Would this be valid?

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

This is the playbook:

---
- name: Setup SNMP on testservers
  hosts: testservers
  roles:
    - { role: snmp, snmp_rocommunity: comtestsrv, snmp_listen_ip: "{{ ansible_bond0.11['ipv4']['address'] }}" }


And then:

# cat templates/snmpd.conf.j2

# {{ ansible_managed }}
agentAddress  udp:{{ snmp_listen_ip }}:161
{% for server in snmp_authorized_hosts %}
rocommunity {{ snmp_rocommunity }} {{ server.ip }}/32
{% endfor %}
syslocation {{ snmp_syslocation }}
syscontact {{ snmp_syscontact }} <{{ snmp_syscontact_email }}>


testservers is a group with testserver1 and testserver2, so the "snmp_listen_ip" would be the bond0.11 IPv4 address for each host when creating the snmpd.conf file.


I also tried:

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

But they also failed..


Thanks again!
J.



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/mpB96y4Ee58/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Nov 5, 2014, 9:14:21 AM11/5/14
to ansible...@googlegroups.com
>> "{{ 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'] }}"

Joel Sdc

unread,
Nov 5, 2014, 5:06:12 PM11/5/14
to ansible...@googlegroups.com
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.

Matt Martz

unread,
Nov 5, 2014, 5:12:51 PM11/5/14
to ansible...@googlegroups.com
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.


For more options, visit https://groups.google.com/d/optout.

Joel Sdc

unread,
Nov 11, 2014, 6:23:51 AM11/11/14
to ansible...@googlegroups.com
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.

--



Reply all
Reply to author
Forward
0 new messages