# How do I create a variable shared by a number of tasks in a role in Ansible?

**URL:** <https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638>\
**Category:** Ansible Project\
**Created:** [February 23, 2014, 6:25pm UTC](https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638 "2014-02-23T18:25:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Butler](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Butler](https://forum.ansible.com/u/Mark_Butler)\
**Post date:** [February 23, 2014, 6:25pm UTC](https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638/1 "2014-02-23T18:25:46Z")

</div>

(I created a SO question about this but maybe it would be better to ask here -

[http://stackoverflow.com/questions/21972208/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible](http://stackoverflow.com/questions/21972208/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible))

I am creating a task file in an Ansible role using the [best practice directory layout](http://docs.ansible.com/playbooks_best_practices.html#directory-layout) e.g.

```auto
myscripts/roles
myscripts/roles/the_role
myscripts/roles/the_role/tasks
myscripts/roles/the_role/tasks/main.yml

```

In main.yml, I need to call a number of Java tasks so I have to specify the classpath each time e.g.

```auto
- name: java | Do something with Bar Java class
  action: command java -cp A.jar:B.jar:C.jar com.example.Bar myargs

- name: java | Do something with Foo Java class
  action: command java -cp A.jar:B.jar:C.jar com.example.Foo myOtherargs

```

so I would to use a variable to replace A.jar:B.jar:C.jar

I have been looking at the [documentation](http://docs.ansible.com/playbooks_variables.html) and this [blog post](http://jpmens.net/2012/08/30/ansible-variables-variables-and-more-variables/). It seems possible to make a variable that is associated with a host or a group of hosts. However this variable should be associated with a task - how do I do that? You can [do it in a Playbook](https://github.com/ansible/ansible-examples/blob/master/language_features/get_url.yml) but I can’t get it to work because I have split the [Playbook into roles](http://docs.ansible.com/playbooks_best_practices.html#directory-layout)?

---

<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:** [February 24, 2014, 7:31pm UTC](https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638/2 "2014-02-24T19:31:24Z")

</div>

Hi Mark,

Small introductory request – Please don’t post questions to StackOverflow if you are going to ask questions here. It’s kind of like texting us to let us know we have a voicemail, plus, there’s a really strong community here that shouldn’t be hassled with having to respond both here and on Stack Overflow… You’re going to get a faster answer here most of the time anyway and it saves us from having to surf to answer questions in both places 🙂

“It seems possible to make a variable that is associated with a host or a group of hosts. However this variable should be associated with a task - how do I do that?”

There’s no such thing, though I’m unclear why you can’t define this at inventory or group level. If it’s different jars every time, perhaps this should be a parameter that you would like to pass into a role?

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 26, 2014, 1:01am UTC](https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638/3 "2014-02-26T01:01:31Z")

</div>

> (I created a SO question about this but maybe it would be better to ask here -

Probably, I don’t know your Significant Other… 😃

> In main.yml, I need to call a number of Java tasks so I have to specify the classpath each time e.g.
> 
> ```auto
> - name: java | Do something with Bar Java class
> action: command java -cp A.jar:B.jar:C.jar com.example.Bar myargs
> 
> - name: java | Do something with Foo Java class
> action: command java -cp A.jar:B.jar:C.jar com.example.Foo myOtherargs
> 
> ```
> 
> so I would to use a variable to replace A.jar:B.jar:C.jar

Well, it sounds like you need to set a variable and then use it in your task. group\_vars are associated with groups, but there are a whole host of places that you can create that particular variable.

I would advise reading most of the documentation page on Variables…

[http://docs.ansible.com/playbooks\_variables.html](http://docs.ansible.com/playbooks_variables.html)

Where you should put that variable in your case is pretty much up to you. The variable precedence section will help you to decide where you should put it to ensure that you get the results that you want.

Adam

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 26, 2014, 1:08am UTC](https://forum.ansible.com/t/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible/14638/4 "2014-02-26T01:08:56Z")

</div>

To be clear the answer is there in the precedence section… Either you want to define it at the playbook level, at the role level, as a role default, or parameterise it and pass it into the roles…

I really don’t know which works best for you but that variable precedence section covers it. I found the blog post less than helpful but that might be that the age of it is significantly older than the latest version of Ansible.

I hope that this helps,  
Adam
