include_role with passed variable defined by template fails

371 views
Skip to first unread message

Dmitriy Panteleyev

unread,
Oct 16, 2018, 12:06:00 AM10/16/18
to Ansible Project
I'm a bit confused how variables are defined when using `include_role`.  A basic example follows.

/play.yml
- hosts: localhost
  roles
:
   
- one



/roles/one/tasks/main.yml
---
- name: do a thing with another role
  include_role
:
    name
: two
  vars
:
   passed_variable
:
     - stuff: "{{ lookup('template', 'role_one_template.j2') }}"


/roles/one/templates/role_one_template_file.j2
bla bla bla bla
random bit of stuff
does
not matter what


/roles/two/tasks/main.yml
---
- debug:
   
var: passed_variable


My expectation is that role "one" will pull the template and pass the results of that lookup to role "two".  However, when running the play, I get first a warning, and then an error.  It seems as if the template lookup occurs in role "two".  So, what exactly is going on, and how do I fix it?


looking for "role_one_template.j2" at "/roles/two/templates/role_one_template.j2"
looking for "role_one_template.j2" at "/roles/two/role_one_template.j2"
looking for "role_one_template.j2" at "/templates/role_one_template.j2"
looking for "role_one_template.j2" at "/role_one_template.j2"
 [WARNING]: Unable to find 'role_one_template.j2' in expected paths (use -vvvvv to see paths)
...
...
ERROR! An unhandled exception occurred while templating '{u'stuff': [{{ lookup(\'template\', \'role_one_template.j2\') }}]}'.
Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'template'.
Error was a <class 'ansible.errors.AnsibleError'>, original message: the template file role_one_template.j2 could not be found for the lookup



Matt Martz

unread,
Oct 16, 2018, 10:48:22 AM10/16/18
to ansible...@googlegroups.com
The template does not exist.  You are using the wrong template file in your lookup call:

```
    - stuff: "{{ lookup('template', 'role_one_template.j2') }}"
```

However, you have indicated the file is actually named: /roles/one/templates/role_one_template_file.j2

--
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/16d95836-e93b-4e98-828d-9b4f176a0047%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Dmitriy Panteleyev

unread,
Oct 16, 2018, 12:44:27 PM10/16/18
to Ansible Project
Sorry, that was just a typo in my example.  The filenames all match.

Dmitriy Panteleyev

unread,
Oct 18, 2018, 3:21:24 PM10/18/18
to Ansible Project
Finally tracked the issue down to my misunderstanding of how lookup() works.

Reply all
Reply to author
Forward
0 new messages