Scope of playbook variables

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

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

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

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