Hello, I'm try to generate some task using data from other host (eg.:
open port for internal application only to host in the same group):
hosts file (PS: only test, i use racksace inventory, so this is not static):
---8<-------8<-------8<-------8<-------8<----
[mongodb]
192.168.10.1
192.168.10.2
192.168.10.3
192.168.10.4
---8<-------8<-------8<-------8<-------8<----
Into playbook I try to cycle using hosts but i don't find how to do this:
I know that's possible use groups in template files, but is not
possible have this information in tasks? Is required to create a bash
script using template and execute it?
In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks. Maybe I misreading the docs. That would be you ideal solution. with_items: groups['mongodb']
Assuming that isn't an optionI'd create a perform a local task before the one you have that returns a list of the hosts as an ansible_fact and then reference that with the with_items directive. with_items: ${hostvars.localhosts.hosts_list}
In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks.
${host_vars.name_of_host.name_of_variable} works fine in playbooks
provided your host names don't have any dots in them. If they do,
that's a known issue and we need some way of escaping that in the
non-Jinja2 variable specification. (There's already a ticket on this)
In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks. Maybe I misreading the docs. That would be you ideal solution. with_items: groups['mongodb']
After some investigation I see that Task are invoked in:
I think proble is not only on dot, but Task don't have access to
variable (i think), all variables with $ are replaced and added into
variable without $ eg: ${mydata} are converted to {mydata}:
I was not sure that ${groups['somegroupname']} works in playbooks or that you could grab a list of $hostvars keys (aka host names) to use in an items loop.
The part that lets variables work in with_items is pretty naive, I
think it just throws away the "$" and sees if the top level variable
is available in the dict.
We'd want something like varLookup that just returned the actual
value, versus templating it out, but still used the same regex logic.