# Setting a global variable in a task

**URL:** <https://forum.ansible.com/t/setting-a-global-variable-in-a-task/23032>\
**Category:** Ansible Project\
**Created:** [November 15, 2016, 4:09pm UTC](https://forum.ansible.com/t/setting-a-global-variable-in-a-task/23032 "2016-11-15T16:09:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 15, 2016, 4:09pm UTC](https://forum.ansible.com/t/setting-a-global-variable-in-a-task/23032/1 "2016-11-15T16:09:13Z")

</div>

Is there a way to set a variable in a task such that it has global scope?  
I want to do something like this:

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;serial: 1  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo\_first\_host: True

&nbsp;&nbsp;&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: pause after the first host (expect "skipping" for subsequent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pause:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: Pausing after the first host; hit return to do the rest  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: foo\_first\_host

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo\_first\_host: False

The problem is that set\_fact seems to set the variable only in the current  
host, which means that when the next host runs, the variable isn't set any  
more. [http://docs.ansible.com/ansible/set\_fact\_module.html](http://docs.ansible.com/ansible/set_fact_module.html) says it right  
there: "Variables are set on a host-by-host basis just like facts  
discovered by the setup module."

Is there some other way to do what I'm trying to do there? I can think of  
some other ideas, like using add\_host to add a host to a group, and test  
if the group is empty, but that seems sort of hokey.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 15, 2016, 5:14pm UTC](https://forum.ansible.com/t/setting-a-global-variable-in-a-task/23032/2 "2016-11-15T17:14:14Z")

</div>

you can still access it via hostvars[‘hostitwassetin’][‘foo\_first\_host’]

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 15, 2016, 7:19pm UTC](https://forum.ansible.com/t/setting-a-global-variable-in-a-task/23032/3 "2016-11-15T19:19:15Z")

</div>

> you can still access it via hostvars['hostitwassetin']['foo\_first\_host']

Ja, but how do I know (when I'm the second host) who the first host was?

In my actual use case, I'm doing things like restarting a cluster of  
appservers, and I want to pause after the first, and then just do the rest  
without further prompting (but one at a time, so I don't want the  
(otherwise super cool) new serial: [1, "100%"] business).

FWIW, the add\_host trick seems to work:

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;serial: 1

&nbsp;&nbsp;&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=groups['foo\_completed\_hosts']

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: pause after the first host (expect "skipping" for subsequent hosts)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pause:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: Pausing after the first host; hit return to do the rest  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "'foo\_completed\_hosts' not in groups"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host: name={{ inventory\_hostname }} groups=foo\_completed\_hosts

So, if there's not a way to set a global variable, the add\_host way seems ok.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

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.
