{% for service_name in groups|sort %}
{% for host in groups[service_name]|sort %}
{{ host }}
{% endfor %}
{% endfor %}
I need to fill in the part that is bolded in the J2 Template. Any help would be greatly appreciated. I feel like there should be something called “groupvars” to be able to access, but there doesn’t appear to be.
{% for service_name in groups|sort %}
{% if service_name not in ['all', 'ungrouped'] %}
{% if hostvars[groups[service_name][0]].route %}
{% else %}
{% endif %}
{% for host in groups[service_name]|sort %}
{{ host }}
{% endfor %}
{% endif %}
{% endfor %}
The idea is to have a route next to id in the xml it’s generating if it has a route, otherwise, do not add a route to the xml.
I get an odd error back stating:
[localhost] => {‘msg’: 'host not found: ', ‘failed’: True}
Keep in mind, my hosts I am running this against is localhost. The idea behind this playbook is to just generate an xml file via the services inventory file.
I figured out what was wrong. Some of my groups did not have servers.
It now runs, but everything in the generated xml has the same route. I will continue working on this, but the help so far was huge. Much further than I was before.
removing the [0] should return a list, not a single host, see if this is a bit cleaner
{% for service_name in groups|sort|difference([‘all’, ‘ungrouped’]) %}
{% set first_host = groups[service_name][0] %}
{% if ‘route’ in hostvars[first_host] %}
this also assumes that the first host in the group has the correct route var (it can be overridden by individual hosts)
Understood. The issue is all of the groups have the same host. What seems to be happening is the last route I assign to that host is what is used for all three. I will have to make the routevariables unique for each and call them all separately. Maybe something like this:
{% for service_name in groups|sort|difference([‘all’, ‘ungrouped’]) %}
{% set first_host = groups[service_name][0] %}
{% if ‘service_name_route’ in hostvars[first_host] %}
This is what I am working on now. Thanks again for all of your help.
I realize I am doing something out of the ordinary here. (Not using this inventory file as a typical inventory file.)