I’m trying to work on a requirement to have the host pattern change depending on certain host groups or parameters being passed to the playbook :
This is what I need exactly :
When an external var cli_hosts is provided, this playbook runs on the provided host pattern
If not, it defaults to a group ‘new_instances’, I’ve used this group to indicate new instances that are spawned in a parent playbook, which includes this play via include statement.
I could have used cli_hosts ( by using set_fact ) in the parent playbook, expecting that this one will pick it up, but that doesn’t work. Passing it as a parameter in the include statement doesn’t work either.
From what I understand, this is because host patterns are not in the set_fact scope. Also, set_fact is host specific fact, so it gets set in the pattern of the original play (localhost in my case). I think it would help if there is a way to access the global scope somehow. I’m not sure why passing it as a parameter not work ( via include )
If either of the above two are not present, default to the entire cluster.
First, 'groups' is not available at the host pattern level, as it is
host dependent.
Second, the default filter does work, you can test this easily with:
- hosts: "{{cli_hosts|default('new_instances')}}"
having the ternary condition you want (if no new_instances) is a lot
harder as the group won't get evaluated until after the expression is.
I’m basically trying to reuse an exiting playbook, by passing a new host pattern to it, from another play. One use case is, I have a “common” play that runs on all hosts. I have another play that spawns new nodes. After spawning new nodes, I include this common play, and need a way to pass the new hosts to the common play. This common play cannot be a role, because there are certain decisions I make based on the pattern, and pass parameters to the role.
First, ‘groups’ is not available at the host pattern level, as it is
host dependent.
I was under the impression that groups was at the “current playbook” scope, as its available for new plays in the same playbook (operating on different hosts pattern).
Second, the default filter does work, you can test this easily with:
hosts: “{{cli_hosts|default(‘new_instances’)}}”
having the ternary condition you want (if no new_instances) is a lot
harder as the group won’t get evaluated until after the expression is.
The reason I have the ternary condition is I’m unable to pass the new host pattern to “cli_hosts”, if I could do that, than the pattern expression will be simpler. Is there a way I can do that? that is pass the host pattern via include.
I was under the impression that groups was at the "current playbook" scope,
as its available for new plays in the same playbook (operating on different
hosts pattern).
groups is available to every play but only once hosts/inventory is
loaded, which is not the case with the - hosts: directive.
The reason I have the ternary condition is I'm unable to pass the new host
pattern to "cli_hosts", if I could do that, than the pattern expression will
be simpler. Is there a way I can do that? that is pass the host pattern via
include.
I don't understand, if you are passing cli_hosts in the command line
it should be easy enough to pass a pattern -e
'cli_hosts=group1:!group2'.
As for the play needing to target new_hosts, just leave it at that, if
there are no new_hosts ti should be skipped.
I was under the impression that groups was at the “current playbook” scope,
as its available for new plays in the same playbook (operating on different
hosts pattern).
groups is available to every play but only once hosts/inventory is
loaded, which is not the case with the - hosts: directive.
Ok, if I could pass the pattern to the included play, I can use it directly in the hosts pattern. But I’m not able to pass that.
The reason I have the ternary condition is I’m unable to pass the new host
pattern to “cli_hosts”, if I could do that, than the pattern expression will
be simpler. Is there a way I can do that? that is pass the host pattern via
include.
I don’t understand, if you are passing cli_hosts in the command line
it should be easy enough to pass a pattern -e
‘cli_hosts=group1:!group2’.
As for the play needing to target new_hosts, just leave it at that, if
there are no new_hosts ti should be skipped.
That is exactly what I was using in fact. But now there is a new requirement of having a default cluster if none of them are defined, that is cli_cluster or new_hosts. and, that is why I started working with the defaults filter to come up with an expression to solve this.