I have a playbook with several roles. For example, I have an nginx role and a php role. Each has the typical directory structure. The nginx role has a template and a task for a virtualhost file that appears like this in the tasks
`
- name: “Move virtual host file into place”
template: “src={{item.src}} dest={{item.dest}} mode={{item.mode}} owner={{ nginx_site_user }} group={{ nginx_site_group }}”
with_items:
- dest: “{{ nginx_sites_available }}/{{ nginx_site_user }}”
src: “{{ nginx_vhost_file_template }}”
mode: 640
notify:
- restart nginx
`
When I run the nginx role, the variable nginx_vhost_file_template is set to “default” and the file “roles/nginx/templates/default” exists. This works fine.
For my PHP role, I simply want to override the nginx_vhost_file_template variable to point to a file “phpsitevhost” in the directory “roles/php/templates/phpsitevhost”. The php role includes the file “meta/main.yml” with the following contents.
`
Roles look in their own templates/ directory for templates. The nginx
role will be applied first due to the dependency, but it has its own
templates/ directory.
It does have its own templates directory, but that directory doesn’t have a file of that name. I’m wondering how I can get the documented functionality that would have ansible look in the other templates directories until it finds that file.
Otherwise, is there some way to accomplish what I am trying to do?
Daniel
I'm not seeing the documented feature you're describing at the link
you posted, and to my knowledge
Anisble has never searching other roles templates/ for templates.
If I was tackling this problem, I'd have the php role contain the
templates it needs.
You already know the nginx role has been applied, so you know where to put them.
Handlers and vars from the nginx dependency are visible when you apply
the php role,
so you can find the right place to put your configs and bounce nginx
if you need to easily
enough.
The documented feature is listed under behaviors:
“Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely”
Based on this I would expect a filename reference to look in “roles/x/templates” until it found the template file.
I can duplicate the file for lower roles, but I would rather parameterize it and have the stated functionality work to find it in the templates folder of the other role.
Ok I think you're misreading the docs.
"Any copy, script, template or include tasks (in the role) can
reference files in roles/x/{files,templates,tasks}/ (dir depends on
task) without having to path them relatively or absolutely"
(in the role) above refers to role x. roles can't see each others bits n pieces.
The php role needs nginx to be setup,it also needs a vhost.
Just have it set the nginx role as a dependency (like you already
are), but put the vhost template in the php role
e.g.
roles/
├── nginx
│ ...rest of role skipped...
│ ├── defaults
│ │ └── main.yml # <- php role can see these vars
│ └── handlers
│ └── main.yml # <- php role can run these handlers
└── php
│ ...rest of role skipped...
├── meta
│ └── main.yml # <- if there's a dependency set here
└── templates
└── foo.vhost.j2 # <- template lives here
I think you’re right that I read that wrong. However, I did find a way to reference the file from another role that I think should work for me.
{{ role_path }}/…/php/templates/phpsitevhost
Since I know the name of the role that will be installed, and that it will be adjacent to other roles, I should be able to always use “{{ role_path }}/…/” to access the files from another role.
This way I can prevent duplicating these files.
Thanks for your help.