accessing facts from from a group

Hello,

I just started playing with ansible a couple of weeks ago … and so far, I must say that it looks like The Tools I’ve been looking for a long time!

Being a newbie, I am facing newbie’s problem…
I would like to get the IP address of a group of server in my host file, and I really can’t figure out to do this properly.

This is the group in my host file:

[tools]
lot-devbld-01
lot-devtls-01
lot-jira-01

The part of the playbook:

  • name: test groups
    command: echo {{ hostvars[$item][ansible_eth0][ipv4][address] }}
    with_items: groups.tools

and, what I get …

REMOTE_MODULE command echo {{hostvars[lot-devtls-01][ansible_eth0][ipv4][address]}}
changed: [lmesdbx1-01] => (item=lot-devtls-01) => {“changed”: true, “cmd”: [“echo”, “{{hostvars[lot-devtls-01][ansible_eth0][ipv4][address]}}”], “delta”: “0:00:00.001929”, “end”: “2013-09-10 15:26:01.354081”, “item”: “lot-devtls-01”, “rc”: 0, “start”: “2013-09-10 15:26:01.352152”, “stderr”: “”, “stdout”: “{{hostvars[lot-devtls-01][ansible_eth0][ipv4][address]}}”}
REMOTE_MODULE command echo {{hostvars[lot-jira-01][ansible_eth0][ipv4][address]}}
changed: [lmesdbx1-01] => (item=lot-jira-01) => {“changed”: true, “cmd”: [“echo”, “{{hostvars[lot-jira-01][ansible_eth0][ipv4][address]}}”], “delta”: “0:00:00.002107”, “end”: “2013-09-10 15:26:05.325616”, “item”: “lot-jira-01”, “rc”: 0, “start”: “2013-09-10 15:26:05.323509”, “stderr”: “”, “stdout”: “{{hostvars[lot-jira-01][ansible_eth0][ipv4][address]}}”}
REMOTE_MODULE command echo {{hostvars[lot-devbld-01][ansible_eth0][ipv4][address]}}
changed: [lmesdbx1-01] => (item=lot-devbld-01) => {“changed”: true, “cmd”: [“echo”, “{{hostvars[lot-devbld-01][ansible_eth0][ipv4][address]}}”], “delta”: “0:00:00.002060”, “end”: “2013-09-10 15:26:09.461217”, “item”: “lot-devbld-01”, “rc”: 0, “start”: “2013-09-10 15:26:09.459157”, “stderr”: “”, “stdout”: “{{hostvars[lot-devbld-01][ansible_eth0][ipv4][address]}}”}

How should I do that to get the hostvars interpreted properly?
and more generally, could you point me to some documentation on the variable syntax? (ie: when do I need a $, when to use groups.tools rather than groups[‘tools’], etc …)

Thank you.

Fred

​leave out the $ sign:

​​

command: echo {{hostvars[item][ansible_eth0][ipv4][address] }}

Hi,

Thanks. In this case I get an error message:

fatal: [lmesdbx1-01] => One or more undefined variables: dict object has no element {u’macaddress’: u’00:50:56:a1:01:43’, u’module’: u’e1000’, u’mtu’: 1500, u’active’: True, u’ipv4’: {u’netmask’: u’255.255.255.0’, u’network’: u’xx.xx.xx.0’, u’address’: u’xx.xx.xx.xx’}, u’ipv6’: [{u’scope’: u’link’, u’prefix’: u’64’, u’address’: u’fe80::250:56ff:fea1:143’}], u’device’: u’eth0’, u’type’: u’ether’}

Fred

You'll want:

    command: echo {{ hostvars[item]['ansible_eth0']['ipv4']['address'] }}

Awesome! Thank you!

Fred