Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper pathing for jinja2 includes #11931

Closed
bretmartin opened this issue Aug 11, 2015 · 4 comments
Closed

Improper pathing for jinja2 includes #11931

bretmartin opened this issue Aug 11, 2015 · 4 comments
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release
Milestone

Comments

@bretmartin
Copy link
Contributor

Issue Type:
  • Bug Report
Ansible Version:
ansible 2.0.0 (devel 6338c3b28c) last updated 2015/08/11 13:51:04 (GMT -400)
  lib/ansible/modules/core: (detached HEAD dea5f5d15e) last updated 2015/08/11 14:52:59 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 210ee3febc) last updated 2015/08/11 15:11:00 (GMT -400)
  v1/ansible/modules/core: (detached HEAD f8d8af17cd) last updated 2015/08/10 11:28:57 (GMT -400)
  v1/ansible/modules/extras: (detached HEAD 495ad450e5) last updated 2015/08/10 11:29:00 (GMT -400)
  config file =
  configured module search path = None
Ansible Configuration:

No config file found; using defaults

Environment:

N/A, running on Mac OS X

Summary:

An include in a jinja2 template should accept a path relative to the directory where the template file resides. This doesn't work, but a full path or a path relative to the playbook directory both do.

Steps To Reproduce:

main.yml:


---

- template: src=test.j2 dest=/tmp/foo

test.j2:

{% include "test2.j2" %}
Expected Results:

Expanded template at /tmp/foo

Actual Results:
TASK [test : template dest=/tmp/foo src=test.j2] *******************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "TemplateNotFound: test2.j2"}

However, if I change the src= setting:

test.yml:

{% include "roles/test/templates/test2.j2" %}

New result:

TASK [test : template dest=/tmp/foo src=test.j2] *******************************
changed: [localhost]

and then /tmp/foo has the expected contents. A full, absolute path also works.

@bretmartin
Copy link
Contributor Author

I tried digging into this a bit and ran out of time, so I thought it was best to open an issue even though I didn't find the fix. I see that a search path is set for jinja2, but I can't figure out why it is seemingly not being searched...

@drybjed
Copy link
Contributor

drybjed commented Aug 28, 2015

I can confirm the bug being present in latest Ansible 2.0 beta release. Here's an example test case which errors on Ansible 2.0 but works fine on Ansible 1.9.2.

@jimi-c jimi-c added bug_report P2 Priority 2 - Issue Blocks Release labels Aug 28, 2015
@jimi-c jimi-c added this to the v2 milestone Aug 28, 2015
@jimi-c jimi-c closed this as completed in f162990 Sep 1, 2015
@jimi-c
Copy link
Member

jimi-c commented Sep 1, 2015

Ahh, tracked this down to the fix merged in in #9933, which incorrectly set the value on the self._templar.environment.searchpath instead of self._templar.environment.loader.searchpath. After correctly assigning the value there, the example provided by @drybjed works without error:

# ansible-playbook -vv site.yml 
Using /etc/ansible/ansible.cfg as config file
1 plays in site.yml
PLAY ***************************************************************************
TASK [ansible-template-test : Generate the file from templates] ****************
changed: [localhost] => {"changed": true, "checksum": "94dec40f15539fd4777b885cbe4423a4b78fb5a7", "dest": "/tmp/output-file", "gid": 0, "group": "root", "md5sum": "0bdd04bb72f25fab6582d6079a0ff9a7", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 91, "src": "/root/.ansible/tmp/ansible-tmp-1441143378.51-9017626803498/source", "state": "file", "uid": 0}
PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0   
# cat /tmp/output-file 
# This file is managed by Ansible, all changes will be lost
First example
Second example

We believe the above commit should resolve this problem for you. This will also be included in the next major release.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

@bretmartin
Copy link
Contributor Author

Fix confirmed over here as well. Thanks @jimi-c!

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release
Projects
None yet
Development

No branches or pull requests

4 participants