# some guidance with roles

**URL:** <https://forum.ansible.com/t/some-guidance-with-roles/12829>\
**Category:** Ansible Project\
**Created:** [May 19, 2013, 10:42pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829 "2013-05-19T22:42:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 19, 2013, 10:42pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/1 "2013-05-19T22:42:11Z")

</div>

Hi,

Im using Ansible 1.2 latest dev release and trying the new feature of roles in order to migrate all of our infrastructure code from a specific vendor cloud manager to Ansible. In our existing infrastructure we use “templates” that are a set of scripts and specific variable values in order to deploy a server (web/ app/ proxy etc…) .

After looking at the documentation and Ansible best practice the current directory tree we have at the moment is :

roles/  
└── db

├── tsaks

│ ├── mysql.yml

└── proxy

└── web

└── common  
├── handlers  
│ ├── all.yml

├── templates

├── vars

├── tasks  
│ ├── ssh\_users.yml

│ ├── monitoring.yml

If my understanding is correct, the template concept we were using before in Ansbile are achieved with the “roles” feature. The common role should be applied to ALL servers and consist of configuration regarding installing ssh keys & the monitoring agent. For the DB tier we want all common tasks to be applied + all tasks for the db role right ? if that’s the case I’m having problems executing my tasks using the role feature :

test.yml

---

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [May 19, 2013, 10:52pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/2 "2013-05-19T22:52:13Z")

</div>

You will want to create a common/tasks/main.yml file which will be run when include the common role from your top-level playbook. From there you can include other playbooks if needed.

See [https://github.com/ansible/ansible-examples/blob/master/language\_features/roletest2.yml](https://github.com/ansible/ansible-examples/blob/master/language_features/roletest2.yml)

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 19, 2013, 10:59pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/3 "2013-05-19T22:59:51Z")

</div>

Hi John, running ansible-playbook test.yml with the setup as bellow doesn’t work :

test.yml

---

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [May 19, 2013, 11:06pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/4 "2013-05-19T23:06:50Z")

</div>

try this:

path/to/roles/common/tasks/main.yml:—

- include monitoring.yml

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 19, 2013, 11:24pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/5 "2013-05-19T23:24:29Z")

</div>

Thanks John, that seems to work. I was trying before with “include:” .

Regarding the vars and handlers files I have available in roles/common/vars/ roles/common/handlers/ needed by my tasks in roles/common/tasks/ , where is it best to define them? inside the master playbook (test.yml) or in the main.yml ? what’s the correct syntax ? do I need to use relative or absolute path ?

Regards,  
N.

---

<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:** [May 20, 2013, 12:21am UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/6 "2013-05-20T00:21:29Z")

</div>

Please take a look here for some examples:

[https://github.com/ansible/ansible-examples/blob/master/lamp\_haproxy/site.yml](https://github.com/ansible/ansible-examples/blob/master/lamp_haproxy/site.yml)

Note not all examples use roles yet, some just have a directory called “roles” – we need to fix this 🙂

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 20, 2013, 10:38am UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/7 "2013-05-20T10:38:16Z")

</div>

Thank you Michael, the examples you paste are very helpful but I can’t find where are you using the vars ? Why do you have a group\_vars and don’t have defined a vars directory for every role ?

---

<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:** [May 20, 2013, 1:03pm UTC](https://forum.ansible.com/t/some-guidance-with-roles/12829/8 "2013-05-20T13:03:00Z")

</div>

Group vars are inventory variables, as described in [http://ansible.cc/docs/patterns.html#table-of-contents](http://ansible.cc/docs/patterns.html#table-of-contents)

Variable directories for roles are optional, so it’s just an organizational choice.
