cancel
Showing results for 
Search instead for 
Did you mean: 
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 7,077 Views

[Ansible] How to dynamically generate an inventory using groups and host names

Jump to solution

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 =)

Labels (3)
0 Kudos
1 Solution

Accepted Solutions
littlebigfab
Starfighter Starfighter
Starfighter
  • 7,064 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)

View solution in original post

4 Replies
littlebigfab
Starfighter Starfighter
Starfighter
  • 7,065 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)

Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 7,047 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.

 

 

0 Kudos
littlebigfab
Starfighter Starfighter
Starfighter
  • 7,042 Views

@Razique,

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

Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 7,038 Views

I agree, this is what the community is for :-)

0 Kudos
Join the discussion
You must log in to join this conversation.