template not looking in templates directory for included roles

389 views
Skip to first unread message

Daniel Watrous

unread,
Aug 21, 2016, 10:32:33 AM8/21/16
to Ansible Project
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.

---
dependencies
:
 
- { role: nginx }

Based on the documentation  http://docs.ansible.com/ansible/playbooks_roles.html#roles, I would expect the template call in the nginx role to look in both "roles/nginx/templates/" and "roles/php/templates/". It's not working this way and instead I get 

TASK [nginx : Move virtual host file into place] *******************************
failed
: [localhost] (item={u'dest': u'/etc/nginx/sites-available/examplewp', u'src': u'phpsitevhost', u'group': u'examplewp', u'mode': 640, u'owner': u'examplewp'}) => {"failed": true, "item": {"dest": "/etc/nginx/sites-available/examplewp", "group": "examplewp", "mode": 640, "owner": "examplewp", "src": "phpsitevhost"}, "msg": "IOError: [Errno 2] No such file or directory: u'/home/vagrant/src/phpsitevhost'"}


It appears to only be searching for the file in the directory where the playbook file is.

How can I get the documented behavior where it would look for the file in the templates directories for all included roles?



Dick Davies

unread,
Aug 22, 2016, 5:14:31 AM8/22/16
to ansible list
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.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b779599d-597b-4f6e-84fd-6f5918221777%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Daniel Watrous

unread,
Aug 22, 2016, 10:47:29 AM8/22/16
to ansible...@googlegroups.com

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


> email to ansible-project+unsubscribe@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAK5eLPRYZodnJbvd7z-icgM29zajJbY-Kd%3DgEUy2yt0dcEkYOQ%40mail.gmail.com.

Dick Davies

unread,
Aug 22, 2016, 1:55:25 PM8/22/16
to ansible list
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.
>> email to ansible-proje...@googlegroups.com.
>> To post to this group, send email to ansible...@googlegroups.com.
>> To view this discussion on the web visit
>>
>> https://groups.google.com/d/msgid/ansible-project/b779599d-597b-4f6e-84fd-6f5918221777%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAHSC4g2MSgo2-pnQ8_QTji1E3%3Dssr3ZyZjab0RHYzPv-AH0%2Bmw%40mail.gmail.com.

Daniel Watrous

unread,
Aug 22, 2016, 3:16:24 PM8/22/16
to ansible...@googlegroups.com
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.


>> email to ansible-project+unsubscribe@googlegroups.com.
>> To post to this group, send email to ansible-project@googlegroups.com.

>> To view this discussion on the web visit
>>
>> https://groups.google.com/d/msgid/ansible-project/b779599d-597b-4f6e-84fd-6f5918221777%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAK5eLPRYZodnJbvd7z-icgM29zajJbY-Kd%3DgEUy2yt0dcEkYOQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscribe@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.

> To view this discussion on the web visit
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAK5eLPSc3QFRTsWADu1FsBD1NLn31tNjgEaDNk6tHEuCLVcNHA%40mail.gmail.com.

Dick Davies

unread,
Aug 22, 2016, 3:29:42 PM8/22/16
to ansible list
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
>> >> email to ansible-proje...@googlegroups.com.
>> >> To post to this group, send email to ansible...@googlegroups.com.
>> >> To view this discussion on the web visit
>> >>
>> >>
>> >> https://groups.google.com/d/msgid/ansible-project/b779599d-597b-4f6e-84fd-6f5918221777%40googlegroups.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to a topic in the
>> > Google Groups "Ansible Project" group.
>> > To unsubscribe from this topic, visit
>> >
>> > https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
>> > To unsubscribe from this group and all its topics, send an email to
>> > ansible-proje...@googlegroups.com.
>> > To post to this group, send email to ansible...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/ansible-project/CAK5eLPRYZodnJbvd7z-icgM29zajJbY-Kd%3DgEUy2yt0dcEkYOQ%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Ansible Project" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to ansible-proje...@googlegroups.com.
>> > To post to this group, send email to ansible...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/ansible-project/CAHSC4g2MSgo2-pnQ8_QTji1E3%3Dssr3ZyZjab0RHYzPv-AH0%2Bmw%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ansible Project" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> ansible-proje...@googlegroups.com.
>> To post to this group, send email to ansible...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAHSC4g29SjSxS7%2BZJ%2BsRY%2B2WEx-wX_RtuEYkb7296QBtLWozgw%40mail.gmail.com.

Daniel Watrous

unread,
Aug 22, 2016, 10:20:15 PM8/22/16
to ansible...@googlegroups.com
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.

>> >> email to ansible-project+unsubscribe@googlegroups.com.
>> >> To post to this group, send email to ansible-project@googlegroups.com.

>> >> To view this discussion on the web visit
>> >>
>> >>
>> >> https://groups.google.com/d/msgid/ansible-project/b779599d-597b-4f6e-84fd-6f5918221777%40googlegroups.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to a topic in the
>> > Google Groups "Ansible Project" group.
>> > To unsubscribe from this topic, visit
>> >
>> > https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
>> > To unsubscribe from this group and all its topics, send an email to
>> > ansible-project+unsubscribe@googlegroups.com.
>> > To post to this group, send email to ansible-project@googlegroups.com.

>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/ansible-project/CAK5eLPRYZodnJbvd7z-icgM29zajJbY-Kd%3DgEUy2yt0dcEkYOQ%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Ansible Project" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to ansible-project+unsubscribe@googlegroups.com.
>> > To post to this group, send email to ansible-project@googlegroups.com.

>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/ansible-project/CAHSC4g2MSgo2-pnQ8_QTji1E3%3Dssr3ZyZjab0RHYzPv-AH0%2Bmw%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ansible Project" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to

>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/CAK5eLPSc3QFRTsWADu1FsBD1NLn31tNjgEaDNk6tHEuCLVcNHA%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscribe@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.

> To view this discussion on the web visit
>
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Mniko9GHPCI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAK5eLPSTaeQx3MHbR8qvV5YGaWdLFKf2yo9wf-Ve8hbL-EtqbQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages