So, I have a bit of an odd one here. Is it possible for host A to read values defined in the host_vars files for hosts X, Y, and Z? I’m trying to render templates on host A using information defined for X, Y, and Z. I could move all that information into a top-level group file, and do a dict keyed off of host name, but I’d rather not. Ideas?
through the hostvars variable you should have access to all vars from all hosts.
{{ hostvars['X']['vardefinedinhostsfile']}}
through the hostvars variable you should have access to all vars from all
hosts.{{ hostvars['X']['vardefinedinhostsfile']}}
Cool, thanks!
From initial testing, it seems that only includes the host_vars for hosts that
in the current run. In other words, if host A is in the vm group, and hosts
X, Y, ans Z are in other groups (my use case; X/Y/Z are guests) which are not
being contacted on this run, then their variables won't be in hostvars? Any
way to add their variables to hostvars even when they aren't being contacted?
is there a lookup I can do? Maybe a custom lookup plugin to get information
about them even when they aren't in the current run?
j
Vars should be there, facts on the other hand, either need to be
gathered or use a persistent fact cache.
Thanks! It is! I was doing this:
-m shell -a "echo '{{ hostvars }}'"
And it wasn't showing what I was looking for. Either 1) something odd was
going on where the echo was getting cut off due to shell rules or some such, or
2) (more likely) it wasn't actually loaded until I did this:
-m shell -a "echo '{{ hostvars['192.168.122.12'] }}'"
That will work great. Thank you!
j
hostvars is special and lazily loaded, you NEED to specify a host or
it might appear missing.
I want to say thank again. It turns out the very thing I needed was documented
right here:
I had read that page, but when it came time to use that knowledge, it didn't
click. Thanks again for your patience and instruction!
j
hostvars is special and lazily loaded, you NEED to specify a host or
it might appear missing.
I want to say thank again. It turns out the very thing I needed was
documented right here:
That page *doesn't* say two of the interesting/important things about the
hostvars dict, though: (1) The one Brian just mentioned; (2) One we ran
into a little while ago, that some of the keys in hostdict aren't hosts.
Those are both really important if you might otherwise think you could use
'with_dict: hostvars' to do something useful.
(If y'all like PRs for things in docsite, I'd be happy to try to add
something along those lines.)
-Josh (jbs@care.com)
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
Those would be useful additions. I've submitted document additions/corrections
before that were accepted.
j
if you just want to iterate over all the hosts, use this instead of hostvars:
with_items: groups['all']