# All about Roles in 1.2 -- a great way to organize best-practices content

**URL:** <https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641>\
**Category:** Ansible Project\
**Created:** [April 6, 2013, 5:07pm UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641 "2013-04-06T17:07:06Z")\
**Posts on this page:** 6\
**Page:** 2

<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:** [April 11, 2013, 9:38pm UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/21 "2013-04-11T21:38:57Z")

</div>

> The last piece that still feels missing to me (at least for my use case)  
> is the templates and files. With the new roles structure the default  
> location for these is relative to the role directory. This works fine when  
> the template as is can be sufficiently represented with various jinja  
> interpolations. And for files its okay when the file is model/role specific  
> and not app+environment relevant. However I have use cases where the  
> app+environment can(/may need to) override the whole template, and/or  
> potentially use its own file/\* ref'd in the playbook. Would it make sense  
> to lookup templates and files first relative to the hosts file, then  
> fallback to role location?

You can still reference other paths in these cases by specifying a  
different path, it looks in two places, basically.

I do not think looking in a path relative to the host file is neccessarily  
a good idea, just because that I think crosses the streams too much between  
inventory data (this is something about my geography or hosts) and  
playbooks (this is a description about what I am deploying onto these  
hosts).

> This is something I do today which allows the app to own its own templates  
> & files (pulled from their git repo), if desired, via first\_available\_file  
> and some path hackery. But it seems to make sense to have this capability  
> 'built in' where the templates/files default to the roles subdir but can be  
> overriden relative to the 'inventory' if necessary.

Hmm, yeah I understand, but am worried if we include too much magic the  
playbook could not be read "at first glance" to know exactly what it would  
do, and that behavior is also important to me.

---

<div class="post-metadata">

**Author:** ![Darryl\_Stoflet](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darryl\_Stoflet](https://forum.ansible.com/u/Darryl_Stoflet)\
**Post date:** [April 12, 2013, 12:04am UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/22 "2013-04-12T00:04:47Z")

</div>

Agreed regarding too much magic. I found one way to do this simply using the 1.2 devel branch:

---

<div class="post-metadata">

**Author:** ![Darryl\_Stoflet](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darryl\_Stoflet](https://forum.ansible.com/u/Darryl_Stoflet)\
**Post date:** [April 12, 2013, 2:20am UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/23 "2013-04-12T02:20:34Z")

</div>

Disregard this. The roles implementation at this point is inconsistent as far as template and copy go when using first\_available\_file. W/o and directly using src is will search relative to the role dir (as in the roletest.yml example). W/ first\_available\_file it will go back to using the playbook source dir :-(.  
Should I file a bug or is this implementation still in the works?

One other note: I noticed that using {{ vars.inventory\_dir }} actually generated an error: recursive loop detected in template string: {{vars.inventory\_dir}}. Oddly though using {{vars.inventory\_dir}} as a debug msg value worked. However ${vars.inventory\_dir} worked in both cases.

So the below works, but as mentioned above first\_available\_file is relative to playbook dir which kinda defeats the purpose of the roles dir structure.

$ /ansible/examples/playbooks$ cat roles/bar/tasks/main.yml

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [April 12, 2013, 3:18am UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/24 "2013-04-12T03:18:14Z")

</div>

Hi,

What about the following (untested) approach of default values that is implemented as a hack, but could be part of the core:

# playbook.yml

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [April 12, 2013, 3:48am UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/25 "2013-04-12T03:48:02Z")

</div>

If a variable wants to be used throughout a role, then its default values and overrides could be stored back into a variable like this (although a better alternative to debug module would be nice):

- debug: msg=“{{ item }}”  
register: var1  
with\_first\_found:

- “{{ var1 }}”

- foo

- name: access the variable whose default value can be overriden  
shell: echo {{ var1.results[0].msg }}

Greetings,  
gw

---

<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:** [April 25, 2013, 9:13pm UTC](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641/26 "2013-04-25T21:13:30Z")

</div>

I’m sorry, this doesn’t really make sense, it is rather specific and would spam lots of data, and add lots of undue code.

Roles could contain variables that reference variables not yet defined – Ansible allows this, so it would not be perhaps as useful as you may think.

Some folks may have 5 simple scalar values in a role, others may have a four page nested hash table.

[Previous page](https://forum.ansible.com/t/all-about-roles-in-1-2-a-great-way-to-organize-best-practices-content/12641.md?page=1)
