Hello,
I’ve been digging through the ansible source, and I believe my current approach to a problem is impossible without making changes in core, which I prefer not to do, so I’m looking for an alternative solution.
In my inventory, I define hosts based on datacenter:
[mia1]
host1
host2
[lax2]
host3
host4
[ams1]
host5
host6
[mia1:vars]
datacenter_name=Nap of the Americas
datacenter_location=Miami, Florida, USA
datacenter_key=mia1
[lax2:vars]
datacenter_name=One Wilshire
datacenter_location=Los Angeles, California, USA
datacenter_key=lax2
[ams1:vars]
datacenter_name=Nap of Amsterdam
datacenter_location=Amsterdam, Netherlands
datacenter_key=ams1
In a vars, I have a vars file defined which sets up a list of hosts:
cluster_name:
- host1
- host2
- host4
- host5
Given these values, I need to create to template an entry like:
Included Datacenters in {{ cluster_name }}:
- Nap of the Americas
- One Wilshire
- Nap of Amsterdam
Here’s the problem: I need this to work even when I have the play limited to a single host (which may or may not be one of the hosts in “cluster_name”. I’ve spent hours digging into this problem, and nothing of tried works. The problem is how to generate a list of “datacenter_names” knowing only the “cluster_name”.
Unfortunately, hostvars only contains information about the hosts that are currently part of the run. I understand the difficulties in accessing discovered facts about a node that’s not part of the play. But it seems like accessing defined facts about a node that’s not part of the play should be easy? I could naturally create a giant defined variable like “hostname_to_datacenter_map” and keep a mapping between hostnames and datacenters up to date. I’d really like to avoid this approach, however, because it won’t scale well, especially with dynamically generated inventory files.
Any other solutions that allow me to keep these facts about hosts associated with the inventory would be greatly appreciated.
All the best,
~ Christopher