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
Lookup plugin 'template' doesn't search the same paths as 'file' #9551
Comments
I believe there may be a misunderstanding there: "File lookup plugin searches for file in the playbook directory and in the role's template directory." File lookup plugins look in "files/", the template one I believe looks in "templates/". This is by design, because of the way templates should operate on templates. |
You're right, this was an error in description, of course I meant the "files/" dir. The issue still stands though. The template plugin should behave the same way as the file plugin and search for the file in playbook directory (in addition to "templates/" of course). I think that when you use |
Not all lookups behave that way, some of the file based lookups have special logic to look into the files/ directory. |
Ok, sure, I agree that behaviour should not be identical. In my humble opinion all the lookups which read some kind of file should search similar paths and the playbook directory might be one of them. When I use the file lookup and then decide to exchange it with template then I expect that both will allow me to store the files somewhere outside the role. AFAIK the template action searches the root project directory too. All I advocate that both Also there is a second issue here: template lookup fails silently if file is not found. |
Just had a colleague of mine run into this as well, while trying to use the virt (extras) module which has something like In my opinion people who didn't run into this behavior before do not expect it. The lack of any error message makes it worse. Edit: This was semi-fixed in 1.8.2 and fully fixed in 1.8.3 (error message now reports the path, not just the basename). Current devel reports the absolute path. So in the template lookup still checks the role's |
I have the same problem with ansible version 1.9.1, so what is the current status, and is there a workaround?
with the directory structure
I get the error:
So it searches in the roles path instead of the playbook path for It would make much more sense if the template is searched in the playbook folder and not in the role folder, as "{{ lookup('template', 'templates/application.cfg.j2') }}" is written in the playbook and not in the role, so let's say it is evaluated too late: not in the playbook but in the role. Can I force ansible to evaluate this variable already in the playbook and only pass the lookuped string to the role? Because that is what I really want: produce template and pass the result to the role... How could I at least get the absolute path to the playbook template folder |
@sebastianneubauer you coudl use {{ lookup('template', playbook_dir + '/templates/application.cfg.j2') }} |
@bennojoy : thanks, "playbook_dir" seems to work! Do you know by chance, if this is somewhere documented or is it an implementation detail which might change in future versions without notice? |
check current devel, the template lookup should now look at both role and playbook level dirs |
Hi, this situation is no longer present in the devel branch (which will be ansible 2.0) 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! |
Ansible Version
The problem
File lookup plugin searches for file in the playbook directory and in the role's files directory.
When using the template plugin the playbook directory isn't searched and lookup returns empty string if file is not found.
{{ lookup('file', 'config/test.j2') }}
- returns the file contents{{ lookup('template', 'config/test.j2') }}
- return empty string{{ lookup('template', '/home/somebody/project/config/test.j2') }}
- absolute path works as expectedThe dir structure is
The text was updated successfully, but these errors were encountered: