# Scope of playbook variables

**URL:** <https://forum.ansible.com/t/scope-of-playbook-variables/13868>\
**Category:** Ansible Project\
**Created:** [November 8, 2013, 4:57am UTC](https://forum.ansible.com/t/scope-of-playbook-variables/13868 "2013-11-08T04:57:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 8, 2013, 4:57am UTC](https://forum.ansible.com/t/scope-of-playbook-variables/13868/1 "2013-11-08T04:57:57Z")

</div>

Hi All,

I’m trying to do the following:

- hosts: all  
vars:  
path: /some/path/here  
tasks:

- name: Create a group of all hosts by operating system  
action: group\_by key={{ansible\_distribution}}-{{facter\_operatingsystemrelease}}

- hosts: Suse-10\*  
vars:  
file: foo-sles10.tar.gz  
tasks:

- name: Copy foo  
local\_action: command rsync -a {{ path }}/{{ file }} $inventory\_hostname:/var/tmp

- hosts: Suse-11\*  
vars:  
file: foo-sles11.tar.gz  
tasks:

- name: Copy foo  
local\_action: command rsync -a {{ path }}/{{ file }} $inventory\_hostname:/var/tmp

Unfortunately the above doesn’t work because the ‘path’ variable defined in the ‘hosts: all’ part cannot be seen in the next ‘hosts’ blocks.

I could set the variable in group\_vars/all, but I was hoping to make the variable “global” to this playbook only.

Any ideas?

GS

---

<div class="post-metadata">

**Author:** ![ben3](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@ben3](https://forum.ansible.com/u/ben3)\
**Post date:** [November 8, 2013, 7:57am UTC](https://forum.ansible.com/t/scope-of-playbook-variables/13868/2 "2013-11-08T07:57:50Z")

</div>

I’ve also just run into this. In my case though, I’m creating a “register:” variable in one play of a playbook (running on localhost and storing Volume IDs of created EC2 Volumes), and I want to refer to that variable in a subsequent play of the same playbook (which runs on the machine itself).

What’s the idiomatic ansible way of doing something like this?

I presume I could use the first play to store the info into a local temp file and then make use of that from the subsequent play to push up the data, but that seems fairly ugly.

(I’m new to and overall very happy with Ansible so far).

–Ben  
P.S. the docs in “playbooks\_variables.html” say “Registered variables are valid on the host the remainder of the playbook run”. IIUC in ansible, a ‘playbook’ is one “.yml” file, and it may contain many ‘plays’. If my understanding is correct, then this is a slight bug in the docs as it appears that registered variables are valid only for the rest of the ‘play’. (Unless the “on the host” part is what I’m missing and it’s the fact that the hosts are different which causes it not to be in scope…)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 10, 2013, 9:36pm UTC](https://forum.ansible.com/t/scope-of-playbook-variables/13868/3 "2013-11-10T21:36:00Z")

</div>

“Unfortunately the above doesn’t work because the ‘path’ variable defined in the ‘hosts: all’ part cannot be seen in the next ‘hosts’ blocks.”

Include the vars\_files file in each play, or instead define the variable in group\_vars/all to make it global.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 10, 2013, 9:36pm UTC](https://forum.ansible.com/t/scope-of-playbook-variables/13868/4 "2013-11-10T21:36:56Z")

</div>

“P.S. the docs in “playbooks\_variables.html” say “Registered variables are valid on the host the remainder of the playbook run”. IIUC in ansible, a ‘playbook’ is one “.yml” file, and it may contain many ‘plays’. If my understanding is correct, then this is a slight bug in the docs”

No, because this is not a variable that was saved as the result of a register keyword. Facts also work the same way.

“vars\_files” pulls in variables only for the current play, by design, as do roles.
