Is there any way to change a global variable (e.g. defined in the vars
section of a playbook) in a task for the current playbook run?
The use case is that I want to start with a boolean variable set to
True, and then run a command over a set of hosts. If a command at _any_
of the hosts returns a specific value in stdout, I want to set the
global boolean variable to False. This 'control' boolean variable will
be checked later to decide where to run (or not run) some other tasks,
so what I want actually is to create a conditional based on output from
multiple tasks or multiple runs in a task loop . I know about the
'set_fact' module, but this works on a per-host basis, not on global
vars, so it does not help. Perhaps, I need a 'set_var' or 'set_global'
module or something like that. I can think of some hacky ways to
workaround my case (e.g. use a temporary file instead of a global var),
but I think changing the value of a global var at will in the course of
a playbook run should be something that Ansible should allow us to do.
Am I missing something here?
Hmm… I see your point. It can be done by looping over the results of the previous task (which should run on all hosts) and run set_fact which will eventually set the same boolean value for all hosts. Thanks. However, I am thinking if a ‘set_global’ would be useful in other, more complicated cases. Perhaps, this is why you ask about different sets of hosts. I have a feeling that in such cases the ‘per-host’ nature of set_fact could become inconvenient. Also, it comes natural to me that setting and changing a var globally (for all hosts) in a single pass would be something possible.
Unfortunately this does not work. set_fact does not override variables defined in “vars” section. I used the following playbook to test it: The output: As you see, although set_fact sets test to “2”, the debug task prints “1”. I am not sure if this is a bug or not, but it surely destroys a possible workaround.
This is due to a (intentional) change in 1.6, to make sure variables from hosts that are tampered with don’t influence variables that describe whether or not a specific configuration is applied to them.
Thus we would need a “set_global” type action plugin (which would also need to be “bypass host loop” in order to be functional).