# How can I run a role with different variables in a loop? Or am I looking at things the wrong way

**URL:** <https://forum.ansible.com/t/how-can-i-run-a-role-with-different-variables-in-a-loop-or-am-i-looking-at-things-the-wrong-way/17740>\
**Category:** Ansible Project\
**Created:** [March 13, 2015, 3:33pm UTC](https://forum.ansible.com/t/how-can-i-run-a-role-with-different-variables-in-a-loop-or-am-i-looking-at-things-the-wrong-way/17740 "2015-03-13T15:33:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitted](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@gitted](https://forum.ansible.com/u/gitted)\
**Post date:** [March 13, 2015, 3:33pm UTC](https://forum.ansible.com/t/how-can-i-run-a-role-with-different-variables-in-a-loop-or-am-i-looking-at-things-the-wrong-way/17740/1 "2015-03-13T15:33:57Z")

</div>

Hi,

I noticed I am repeating myself in my role’s tasks, and I am probably doing this wrong 🙂

I have a role that deploys my application to server(s).

The server will have 2 applications installed on it, app1 and app2.

So my directory structure on my server looks like:

/var/www/app1/…  
/var/www/app2/…

The tasks for both app1 and app2 are VERY similiar, and I am basically copying each task 2 times for example:

- name: app1 directory exists  
file: path={{app1\_path}} state=directory

- name: app2 directory exists  
file: path={{app2\_path}} state=directory

I then do the same thing for directory permissions, 2 times.

- name: push app1 to server  
copy: src={{app1\_build}}/app1.zip dest={{app1\_path}}

- name: push app2 to server  
copy: src={{app2\_build}}/app2.zip dest={{app2\_path}}

etc.  
etc.

What I am looking for is creating a role, and then looping through a set of variables, is this possible?

**psuedo code of what I want**  
e.g.

loop  
role “role\_name” app\_path=$app\_path, app\_name=$app\_name, app\_filename=$app\_filename  
end loop

This way I can re-use my role, instead of having to repeat things so many times.

I’m sure this is possible but how can I do this with Ansible?

Thanks!

---

<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:08am UTC](https://forum.ansible.com/t/how-can-i-run-a-role-with-different-variables-in-a-loop-or-am-i-looking-at-things-the-wrong-way/17740/2 "2015-03-16T00:08:30Z")

</div>

gitted,

Your approach depends on how you’re organizing your variables. Ansible (as of now) does not let you loop through roles or includes. The best way to do this is to loop in each task.

Here’s a sample:

vars:  
apps:  
app1:  
path: /var/www/app1  
build: app1.tar  
app2:  
path: /var/www/app2  
build: app2.tar  
tasks:

- name: Ensure directories exist  
file:  
path={{item.value.path}}  
state=directory  
with\_dict: apps

- name: Push apps to server  
copy:  
src={{item.value.build}}/{{item.key}}.zip  
dest={{item.value.path}}

with\_dict: apps

There are other ways to approach this as well, but this should do what you want

Chip

---

<div class="post-metadata">

**Author:** ![gitted](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@gitted](https://forum.ansible.com/u/gitted)\
**Post date:** [March 18, 2015, 8:17pm UTC](https://forum.ansible.com/t/how-can-i-run-a-role-with-different-variables-in-a-loop-or-am-i-looking-at-things-the-wrong-way/17740/3 "2015-03-18T20:17:38Z")

</div>

That makes sense, thanks.
