- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,051 Views
Hey everyone,
I'm trying to do something (fairly) simple - I would like to generate an inventory from a Jinja2 template using Ansible 2.8.
Namely, I'm looking for something like this
[groupA] host A ansible_host=10.0.0.10 host B ansible_host = 10.0.0.20 [group B] host C ansible_host = 10.1.0.0 host D ansible_host = 10.2.0.0.
I thought about using the following loop:
{% for group in groups %} [{{group}}] {% for entry in groups[group] %} {% for host in hostvars %} {% if entry == host %} {{ host|replace(name_prefix + "-", "") }} ansible_host={{ hostvars[host].ansible_host }} {% endif %} {% endfor %} {% endfor %} {% endfor %}
However, for some reason, the play fails with the following message:
{ "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_host'", "changed": false, "_ansible_no_log": false }
Notice that hosts have been added via the add_host directive.
When I query the hostvars hash, I have a dictionnary for each host - and in each dictionnary, I do see the ansible_host string.
Thanks for your help =)
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,038 Views
Hi Razique,
Editing my answer for the third time :smileylol:
In the end, I think that your problem comes from your 'localhost' host, which doesn't have an 'ansible_host' key in its hostvars map.
Can you please try either :
{% for host in hostvars | difference(['localhost']) %}
Or
{% if entry == host and 'ansible_host' in hostvars[host] %}
(I prefer the second one, as it covers all hosts which end up not having that 'ansible_host' key)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,039 Views
Hi Razique,
Editing my answer for the third time :smileylol:
In the end, I think that your problem comes from your 'localhost' host, which doesn't have an 'ansible_host' key in its hostvars map.
Can you please try either :
{% for host in hostvars | difference(['localhost']) %}
Or
{% if entry == host and 'ansible_host' in hostvars[host] %}
(I prefer the second one, as it covers all hosts which end up not having that 'ansible_host' key)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,021 Views
Thanks for much @littlebigfab, this was indeed my issue. The local host did not have this variable set. Your (secong) suggestion worked like a charm.
As for why using ansible_host instead of ansible_ssh_host, my understanding is that the later is deprecated in favor of the former. See this link.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,016 Views
Absolutely! :)
I was still using ansible_ssh_host, that's why I got confused in the first place ;)
This is what I love about this community, when you try and help someone and end up learning something on your own :D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 8,012 Views
I agree, this is what the community is for :-)