# Access a variable defined in a playbook - within a role

**URL:** <https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739>\
**Category:** Ansible Project\
**Created:** [March 13, 2015, 2:31pm UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739 "2015-03-13T14:31:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 13, 2015, 2:31pm UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/1 "2015-03-13T14:31:23Z")

</div>

Hello,

I have this playbook running a role. The role runs a script that runs with the role-parameter: “whentorun”  
I reuse the role with different arguments bun I need to run the second command (/scripts/nonReg/expoNonRegTest.sh diff) when “whentorun” has a certain value.  
For example:

- name: bla bla  
hosts: ALL  
remote\_user: root  
roles:
- role: nonreg  
whentorun:
- post

---

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [March 14, 2015, 4:12pm UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/2 "2015-03-14T16:12:14Z")

</div>

This may not directly answer your question, but this how I am handling that same issue. We have definitions of packages and rpms defined in group\_vars and host\_vars:

# inventory/dse/group\_vars/dse/package\_versions.yml

package\_versions:  
app:  
app: 4.6.1  
jre: jre-1.7.0\_75-fcs.x86\_64

Then as part of the role of “app” we check if the packages are already installed:

# roles/datastax\_enterprise/tasks/main.yml

- name: Lookup installed jre version  
shell: rpm -q jre || true  
register: installed\_jre\_version  
ignore\_errors: true  
changed\_when: installed\_jre\_version.stdout != package\_versions.app.jre

- name: Install jre rpm  
yum: pkg={{ path\_to\_rpm }}/{{ package\_versions.app.jre }} state=present  
sudo: yes  
when: installed\_jre\_version | changed

So “installed\_jre\_version | changed” is True only when jre doesn’t match our version (not installed or earlier version). You can take the same action as my “Lookup installed jre version” to register a variable based on the output of “1” or of a different play that double-checks “1” and then run “2” based on that variable.

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 14, 2015, 4:30pm UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/3 "2015-03-14T16:30:17Z")

</div>

Thanks for the reply but if I run a role 10 times in a playbook and only on the 5th run of that role I want it to run the second shell cmd from within that role. How can I address that ? If I can insert some variable within the playbook (outside the role) in the beggining something like:  
vars:  
some\_variable: 0

and on each run of the role within the playbook increment the variable: some\_variable:

some\_variable = some\_variable + 1 … or some\_variable ++ ? 🙂

and at the 5th run I can have a

shell: the 5th run cmd  
when: “{{ some\_variable }}” == 5

How can I do that ?  
How can I declare a variable and assign a value to it (during the run o a role/playbook)? What is the syntax ? In the ansible documentation, at variables, there isn’t a simple example of how can you assign a value to a variable (not with register :P)

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [March 16, 2015, 12:44am UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/4 "2015-03-16T00:44:39Z")

</div>

Adrian,

Interesting idea. Ansible is tricky when it comes to assigning values to variables and having them persist through roles, plays, or between hosts. I don’t know about how you could manipulate variable values, but one way I’ve found to keep track of things between plays and hosts is by working with inventory groups.

Using this, you can add a fake host to a fake group on every iteration of a role. The later roles will be able to access this group and see it’s contents/length so that the role knows how many iterations have passed. Here’s an example:

#site.yml

- name: play1  
hosts: all  
role:
- { role: myrole, myvar: foo }
- { role: myrole, myvar: bar }
- { role: myrole, myvar: foobar }

#myrole/tasks/main.yml

- name: run script  
shell: /path/to/script.sh {{myvar}}

- name: run other script on 5th run  
shell: /path/to/otherscript.sh {{myvar}}  
when: groups.counter\_group\_{{inventory\_hostname}}|d(“”)|length == 4

- name: add host to counter group  
add\_host:  
group=counter\_group\_{{inventory\_hostname}}  
name=fakehost{{groups.counter\_group|d(“”)|length}}

Running this role multiple times lets you keep track of the number of runs so far and lets you use a makeshift counter for a conditional statement. If you’ve never used some of these things, here’s a little background:  
– The d(“”) in the when statement and the name parameter of the add\_host module is short for default(“”) and is for setting a default value for that variable so that it doesn’t error out when undefined.  
– In this case, using {{inventory\_hostname}} in the group name lets each host manage its own counter group so that multiple hosts are not adding to the same group on a single run of the role.

Good luck!  
\Chip

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 16, 2015, 5:21am UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/5 "2015-03-16T05:21:11Z")

</div>

That’s a very big workaround for a simple var++ stuff 🙂

It weird to say “Ansible’s human-readable IT automation language” and you can’t even do a basic value to variable assignment

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 16, 2015, 6:36am UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/6 "2015-03-16T06:36:14Z")

</div>

I found the solution:

You can use the `set_fact` module to increment your variable:

```auto
- set_fact: some_variable={{ some_variable | int + 1 }}

```

Your condition for running the extra task then should look like this:

```auto
  when: some_variable | int == 5

```

Make sure you always cast the value to and int with `| int` or it will be handled as a string.

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [March 16, 2015, 2:49pm UTC](https://forum.ansible.com/t/access-a-variable-defined-in-a-playbook-within-a-role/17739/7 "2015-03-16T14:49:45Z")

</div>

Awesome, I had forgotten that those variables survive between plays
