-
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
Emit warnings when safe_eval() raises a SyntaxError or other Exception #14304
Conversation
This change is related to reported issue ansible#14291 and pull request ansible#14293. Without the fix from ansible#14293, this change will emit a warning as shown below, on the following playbook: ``yaml --- - hosts: localhost gather_facts: no vars: works: key1: 'string' key2: 1234 fails: key1: 'string' key2: 1234 key3: false tasks: - debug: msg={{ works | to_json }} - debug: msg={{ fails | to_json }} ``` On error, this results in a proper warning: ``` [dag@moria ansible.dag]$ ansible-playbook test49.yml PLAY *************************************************************************** TASK [debug] ******************************************************************* ok: [localhost] => { "msg": { "key1": "string", "key2": 1234 } } TASK [debug] ******************************************************************* [WARNING]: Error in expression "{"key3": false, "key2": 1234, "key1": "string"}". (name 'false' is not defined) ok: [localhost] => { "msg": "{\"key3\": false, \"key2\": 1234, \"key1\": \"string\"}" } PLAY RECAP ********************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 ```
This issue #14293 exists also on Ansible v1.9.4, so if accepted (in whatever form) I'll look at backporting the logic too. (I have something for 1.9 here working at $company as well) |
Can we get this into v2.2 please ? It simply ensures that important syntax errors (that should not happen, but once did) are not silently ignored. Should be a no-brainer to decide to merge it, or improve it even more. |
@@ -123,13 +129,14 @@ def generic_visit(self, node, inside_call=False): | |||
else: | |||
return result | |||
except SyntaxError as e: | |||
display.warning('SyntaxError in safe_eval() on expr: %s (%s)' % (expr, e)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure that this merits displaying error - as per the comment below this line, there is some special handling for this exception that results in passing the string back as-is for later processing. I appear to have been relying on this behavior, as I am now getting a ton of SyntaxError warnings on Jinja2 templates inlined in my group_vars YAML.
I have a group_vars YAML file that contains entries like this:
Since this change was committed, I now get a slew of errors on every playbook execution that look like this:
This is valid YAML as far as I can tell; I see no reason why it should be throwing an error. Perhaps the special behavior noted in the comment should silently succeed without raising a warning? |
@brandond I have made my own tests in order to reproduce your problem, but I cannot. group_vars/all test122_expression: 'Expr: {{ inventory_hostname }}'
test122_statement: >-
{%- if inventory_hostname == 'localhost' -%}
Local: {{ inventory_hostname }}
{%- else -%}
Other: {{ inventory_hostname }}
{%- endif -%} test122.yml - hosts: localhost
gather_facts: no
tasks:
- debug:
msg: '{{ test122_expression }}'
- debug:
msg: '{{ test122_statement }}' Output
PS Thanks for showing me that you can use Jinja2 statements inside YAML playbooks. It was my impression that only Jinja2 expressions were allowed in playbooks/vars files and Jinja2 statements were an exclusive to templates. (I wonder if we have such examples in the documentation) |
Aha, in case of late evaluation, this problem comes into play: group_vars/all test122_expression: 'Expr: {{ inventory_hostname }}'
test122_statement: >-
{%- if test122_variable == 'localhost' -%}
Local: {{ inventory_hostname }}
{%- else -%}
Other: {{ inventory_hostname }}
{%- endif -%} test122.yml - hosts: localhost
gather_facts: no
vars:
test122_variable: 'localhost'
tasks:
- debug:
msg: '{{ test122_expression }}'
- debug:
msg: '{{ test122_statement }}' Output
|
See ansible#14304 comments (ansible#14304 (comment)) for more information.
@brandond The warning is not related to YAML, but it is related to Jinja2 expression/statement evaluation. Since the statement is evaluated before some variables were known/evaluated/parsed, it causes a SyntaxError. In a subsequent (late) evaluation the syntax is considered fine because the variables are defined. Fixed by PR #17462, however it would have been better if you opened a new issue for this. |
@dagwieers - you're probably right, since the change was committed the issue merited a new issue. Thanks for the quick fix anyway! |
See #14304 comments (#14304 (comment)) for more information.
) See ansible#14304 comments (ansible#14304 (comment)) for more information.
This change is related to reported issue #14291 and pull request #14293.
Without the fix from #14293, this change will emit a warning as shown below, on the following playbook:
On error, this results in a proper warning: