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

improve conditional warnings #57190

Merged
merged 5 commits into from
Jun 3, 2019
Merged

Conversation

s-hertel
Copy link
Contributor

SUMMARY

Fixes #53428 - Now only non-boolean when statements will trigger this warning
Fixes #56830 - Fix the order for warning on templating delimiters

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/playbook/conditional.py

Fix bare variable warnings when the variable is a boolean
# make sure the templar is using the variables specified with this method
templar.available_variables = all_vars

try:
# if the conditional is "unsafe", disable lookups
disable_lookups = hasattr(conditional, '__UNSAFE__')
conditional = templar.template(conditional, disable_lookups=disable_lookups)
if not isinstance(conditional, bool) and bare_vars_warning:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debate whether issuing the warning here is catching some of the edge cases.

Take the following example:

- debug:
  when: thing
  vars:
    thing: other
    other: false

This is a little contrived for brevity, but is the users intent to evaluate the string "other" or have it double interpolated to the value of False?

Copy link
Contributor Author

@s-hertel s-hertel May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivel Good food for thought. I haven't altered what happens for the example task above on this branch versus devel (the var is double interpolated to the value of False and a warning is given for the bare var). But if brackets are used instead the bare variable warning doesn't occur.

- debug:
  when: thing
  vars:
    thing: "{{ other }}"
    other: false

In your example without brackets I'm unsure if the non-bracketed edge case has the wrong behavior. Since there is a bare var in the mix getting a warning for it seems like it could be appropriate. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel pretty happy with this as it is. My reasoning is that if the behavior is going to stay for the user when CONDITIONAL_BARE_VARS changes then we don't need to warn them about anything.

@sivel
Copy link
Member

sivel commented May 30, 2019

I left a question on the commit. But looks like I did so in between commits, so it's marked as "outdated" and not shown on the "Files Changed" tab.

@s-hertel
Copy link
Contributor Author

@sivel thanks, found it :-)

@bcoca
Copy link
Member

bcoca commented May 30, 2019

can you add a couple of test cases? one that triggered warning before and does not anymore and one that always triggers warning?

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels May 30, 2019
@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label May 30, 2019
If the behavior may change when the default behavior for CONDITIONAL_BARE_VARS becomes False there should be a warning. Boolean type conditionals will not change in behavior so don't warn.
@mkrizek mkrizek merged commit 21cd24a into ansible:devel Jun 3, 2019
@jamescassell
Copy link
Contributor

can we backport this one?

jamescassell pushed a commit to jamescassell/ansible that referenced this pull request Jun 3, 2019
* Fix order for warning on templated conditionals

Fix bare variable warnings when the variable is a boolean

* changelog

* Add tests for cases that should and should not give warnings

If the behavior may change when the default behavior for CONDITIONAL_BARE_VARS becomes False there should be a warning. Boolean type conditionals will not change in behavior so don't warn.

* oops, forgot to add files

* typo

(cherry picked from commit 21cd24a)
@s-hertel
Copy link
Contributor Author

Backport: #57329

abadger pushed a commit that referenced this pull request Jun 12, 2019
* Fix order for warning on templated conditionals

Fix bare variable warnings when the variable is a boolean

* changelog

* Add tests for cases that should and should not give warnings

If the behavior may change when the default behavior for CONDITIONAL_BARE_VARS becomes False there should be a warning. Boolean type conditionals will not change in behavior so don't warn.

* oops, forgot to add files

* typo

(cherry picked from commit 21cd24a)
@ansible ansible locked and limited conversation to collaborators Aug 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ansible 2.8.0 warning on when: variable when variable is dynamic spurious CONDITIONAL_BARE_VARS warnings
6 participants