# Change a global var in a task

**URL:** <https://forum.ansible.com/t/change-a-global-var-in-a-task/15246>\
**Category:** Ansible Project\
**Created:** [May 15, 2014, 12:37pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246 "2014-05-15T12:37:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 15, 2014, 12:37pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/1 "2014-05-15T12:37:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 15, 2014, 1:17pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/2 "2014-05-15T13:17:30Z")

</div>

Using set\_fact should still achieve the same effect, you're using that  
variable over multiple plays with differing sets of hosts?

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 15, 2014, 2:27pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/3 "2014-05-15T14:27:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 15, 2014, 3:31pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/4 "2014-05-15T15:31:33Z")

</div>

It might be useful, if you’re interested in writing/contributing something like that we’d definitely consider it.

Thanks!

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 16, 2014, 12:39pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/5 "2014-05-16T12:39:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 16, 2014, 2:56pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/6 "2014-05-16T14:56:02Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 16, 2014, 2:56pm UTC](https://forum.ansible.com/t/change-a-global-var-in-a-task/15246/7 "2014-05-16T14:56:23Z")

</div>

Minor correction, change was made in the 1.5 updates.
