-
Notifications
You must be signed in to change notification settings - Fork 23.7k
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
Interpolation of undefined variables different between with_items and direct reference from inside templates. #9008
Comments
I just got bitten by this: - hosts: examplehost
tasks:
- name: "a task"
shell: "echo this is just an example it applies to any action"
sudo: "yes"
sudo_user: "{{ userr }}" # oops, typo'd variable name
vars:
user: "myuser" Which produces:
I can submit another ticket, or I can just leave this as a note on this ticket. |
And again with the odd
- name: "config | Create the basic deploy target directories"
file:
name: "{{ item }}"
state: "directory"
owner: "{{ app_user_username }}"
group: "{{ app_user_username }}"
with_items:
- "{{ app_path }}"
- "{{ app_path_releases }}"
# ... |
This looks like a bug down the lazy evaluation path to me. The "{{ foo }}" becoming "{# foo #}" is a result of the "anti-new parameter injection" security checking stuff coming in which should not come into play here. I would expect it to remain "{{ foo }}" and result in an undefined variable error. |
@damncabbage is this only happening with variables that are undefined, or are you seeing this with all variables? |
Only typo'd / undefined variables. (In the last example, |
Great, just wanted to make sure, I'm looking into this now. Thanks! |
@damncabbage I've created the above patch, if you'd like to test it out. Using your first example, the following output is the result: PLAY [localhost] ************************************************************** TASK: [things with_items] ***************************************************** fatal: [127.0.0.1] => undefined variable in items: 'should_explode' is undefined FATAL: all hosts have already failed -- aborting |
Tested the patch; it explodes as expected. Thanks. 😃 My test case (simpler than the initial setup): - hosts: localhost
tasks:
- debug:
var: "{{ item }}"
with_items: a_list
vars:
a_list:
- "{{ foo }}"
- "{{ foob }}"
foo: "Hello" I do agree with @redbaron; please instead consider: import subprocess
import contextlib
+ import jinja2.exceptions
from vault import VaultLib # if not already a list, get ready to evaluate with Jinja2
# not sure why the "/" is in above code :)
try:
- new_terms = template.template(basedir, "{{ %s }}" % terms, inject)
+ new_terms = template.template(basedir, terms, inject, convert_bare=True, fail_on_undefined=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR)
if isinstance(new_terms, basestring) and "{{" in new_terms:
pass
else:
terms = new_terms
+ except jinja2.exceptions.UndefinedError, e:
+ raise errors.AnsibleUndefinedVariable('undefined variable in items: %s' % e)
except:
pass (Is it standard practice with this project to also add a case to the automated test suite?) |
Feel free to send a PR for that so you get credit, the way I did it was admittedly not pythonic. |
(Fixes ansible#9008.) With credit to jimi-c for the initial pass in this commit: jimi-c@b18bd6b
Done; thank you. |
Closing This TicketHi! 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! |
It was reverted in 97408fe Can you explain - why? Ansible ignores undefined variables again. |
Issue Type:
Bug Report
Ansible Version:
ansible 1.7.1 (identical behaviour in devel, 267b3fb)
Environments:
Ubuntu 14.04 managing Ubuntu 14.04 machines.
Summary:
The following
group_vars/all
... correctly interpolates
should_explode
when that variable is provided, but:{{ should_explode }}
when referenced from within awith_items
loop, and(
error_on_undefined_vars
is set to True. And I say{{ should_explode }}
, but when used with the copy or template modules I actually get{# should_explode #}
instead; I'm considering the {{ vs {# confusion a very secondary issue for the sake of this ticket, though.)Steps To Reproduce:
Have the following in a
group_vars/all
file:Have two templates:
item_name.j2
:... and
things_names.j2
:Have the following tasks in a playbook:
Add an ansible.cfg with the following if you want to be sure about the error-on-undefined:
Finally, run the hash integration tests with
cd test/integration; make test_hash
.Expected Results:
Actual Results:
And on the second run (no modifications):
Notes:
I'm not sure if this is known behaviour. The second run is definitely weird at least, even if the inconsistency in variable dereferencing is expected.
The text was updated successfully, but these errors were encountered: