geerlingguy / ansible-role-ntp

Ansible Role - NTP
https://galaxy.ansible.com/geerlingguy/ntp/
MIT License
319 stars 243 forks source link

Bare variable evaluation deprecation in ansible 2.8 #59

Closed gevalo1 closed 5 years ago

gevalo1 commented 5 years ago
[DEPRECATION WARNING]: evaluating ntp_enabled as a bare variable, this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

[DEPRECATION WARNING]: evaluating ntp_manage_config as a bare variable, this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

This warning is thrown by the Ensure NTP is running and enabled as configured. & Generate ntp.conf file tasks in ansible 2.8 (currently unreleased dev).

coledarr commented 5 years ago

Running ansible from git clone, noticed the above warning. So I ran a git pull to make sure I'm running the latest version. Now running commit 2128e7c14f from stable-2.8 branch.

I have this code at the end of a block in a role I have this:

 when:
      - plex_repo

Defined in defaults as

plex_repo:  yes

Also tried:

plex_repo: true

and

plex_repo: True

Block above this one has:

when:
  - not plex_repo

And does not give deprecated warning. These are the only two places I reference plex_repo.

Is this really intended behavior?

Here's the documentation that seems to say this is fine: https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html

wturrell commented 5 years ago

This goes away when I change:

when: foo to: when: foo == true

but like @coledarr, I'm confused whether we're really supposed to write it like this from now on.

I found this in the porting guide:

https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.8.html#module-option-conversion-to-string

wturrell commented 5 years ago

Ah - just discovered you can do also:

when: foo|bool

geerlingguy commented 5 years ago

This was fixed in #64 / https://github.com/geerlingguy/ansible-role-ntp/commit/99955a37a7856becf0a6eef249dba1bf60e3a09d

PKPublicCode commented 4 years ago

It seems that it's supposed to use when: foo|bool But in this case if foo is undefined, it throws error. so I need to do something like when: foo is defined and (foo | bool) Is there any more elegant solution to get true result if foo == True, and false in all other cases?

geerlingguy commented 4 years ago

@PKPublicCode - In the case of the NTP role, the variable should always be defined... right? Or are you asking in general / relating to something other than the NTP role?

PKPublicCode commented 4 years ago

My last question was "in general"... Probably I used wrong thread for it.

geerlingguy commented 4 years ago

@PKPublicCode - No problem, glad to see you here! There's not really a better way to do it currently (I know there's some shortcut in PHP like is_empty($var), but nothing like that afaict in Python or Jinja. I'd ask more though on Stack Exchange or Ansible's IRC channels.

iacgg commented 4 years ago

This can also happen when the variable has '' or "", it guess the auto bool typecasting will be disabled in ansible 2.12. Statement like when: foobar Need to have a variable like foobar: true and not foobar: 'true'

artm commented 3 years ago

An alternative for when: foo is defined and (foo | bool) is when: foo | default(false) | bool