Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 4,367 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
Flight Engineer Flight Engineer
Flight Engineer
  • 4,354 Views

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

Jump to solution

Hi Razique,

Editing my answer for the third time Smiley LOL

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

Reply
Loading...
4 Replies
littlebigfab
Flight Engineer Flight Engineer
Flight Engineer
  • 4,355 Views

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

Jump to solution

Hi Razique,

Editing my answer for the third time Smiley LOL

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

Reply
Loading...
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 4,337 Views

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

Jump to solution

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
Reply
Loading...
littlebigfab
Flight Engineer Flight Engineer
Flight Engineer
  • 4,332 Views

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

Jump to solution

@Razique,

Absolutely! Smiley Happy

I was still using ansible_ssh_host, that's why I got confused in the first place Smiley Wink

This is what I love about this community, when you try and help someone and end up learning something on your own Smiley Very Happy

Reply
Loading...
Razique
Flight Engineer Flight Engineer
Flight Engineer
  • 4,328 Views

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

Jump to solution

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

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