accessing hostvars within group

Hi,

We are running Ansible 1.2.3 on CentOS 6.4 but we’re hitting an odd problem when trying to access variables within a group. We have a snippet of code like this;

{% for item in groups[‘target_group’] %}
{{ item }} : {{ hostvars[item] }}
{% endfor %}

When we run that from the build server against two servers in the group it lists the custom hostvars in addition to the ansible_* hostvars on the first server but the dictionary for the second server only contains custom entries, which prevents us from accessing the IP address of the second server from the first. When I run ansible -m setup against the two servers in the group from the build server it does return a complete JSON doc with the interfaces listed with their IP addresses.

Seeing as this is mentioned in http://www.ansibleworks.com/docs/playbooks2.html I assume there is something obvious that we are doing wrong but I’ve looked through the documentation and I can’t see the issue. Have I missed something?

Graham

Problem aside, don’t use ‘item’ as it might conflict with the ansible ‘built in’ for with_items:. In any tool try to shy away from reusing ‘internal’ var names.

I would anticipate what is happening is you haven’t gathered facts from those other servers yet, so should include them in a basic play so that they can be scanned.

In the near future, Ansible will cache facts so this will not be required if you have already talked to them.

For instance:

  • hosts: all
    tasks:

will force fact gathering on all hosts.

If this were the case, what you would observe is inventory variables would be defined for that particular host, but you wouldn’t have information about facts coming from the host like an IP address just yet.

The micro-play beforehand will ensure data is gathered from them.