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

defined vars are empty string and not None/null #13877

Closed
oravirt opened this issue Jan 14, 2016 · 4 comments
Closed

defined vars are empty string and not None/null #13877

oravirt opened this issue Jan 14, 2016 · 4 comments
Labels
affects_2.2 This issue/PR affects Ansible v2.2 affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.
Milestone

Comments

@oravirt
Copy link

oravirt commented Jan 14, 2016

This doesn't seem to work on 2.0.0.1, but it does work on 1.9.4.
Tested on Fedora 23

- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
- name: debug
  debug: msg={{ansible_port|default(22)}}

v1.9.4 output

PLAY [localhost] *************************************************************** 
TASK: [debug] ***************************************************************** 
ok: [localhost] => {
    "msg": "22"
}

v2.0.0.1 output

PLAY *****************************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
   "msg": ""               <-- Default not picked up
}

First posted on mailing list: https://groups.google.com/forum/#!topic/ansible-project/4JLrQVGlxZ4

Any ideas? Am I missing something?
regards
/M

@bcoca bcoca added this to the stable-2.0 milestone Jan 18, 2016
@bcoca
Copy link
Member

bcoca commented Jan 18, 2016

no, the issue is that ansible_port is "" and not empty/None, this works:

- debug: msg="{{ansible_nonexistant|default(22)}}"

@bcoca bcoca changed the title Default value not picked up in v2.0.0.1? ansible_port is empty when it should be None Jan 18, 2016
@mtsonline
Copy link

Hi together,

let me add my 2ct to this report:
I do not think the title is correct, this seems to be a far more global problem in the 2.x branch.
Any defined variable / dictionary value that is not filled (thous should be None) is "" and so the default will not be set.

most of our playbooks are broken because of that bug.

example:

mydict:
mykey:

Here the value of mykey should be "None" and substituted by the given default.
But it is "" and will not be substituted.

Hope this helps to fix it.

cheers
Martin

@bcoca
Copy link
Member

bcoca commented Jan 19, 2016

hmm, you seem to be correct:

- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    testvar: !!null
  tasks:
    - name: debug
      debug: msg="{{testvar|default(22)}}"

@bcoca bcoca changed the title ansible_port is empty when it should be None defined vars are empty string and not None/null Jan 19, 2016
@bcoca bcoca modified the milestones: stable-2.0, stable-2.1 Sep 6, 2016
@ansibot ansibot added the affects_2.2 This issue/PR affects Ansible v2.2 label Sep 8, 2016
@ansibot ansibot added the affects_2.3 This issue/PR affects Ansible v2.3 label Dec 13, 2016
@bcoca
Copy link
Member

bcoca commented Mar 11, 2017

So looking at this again:

-debug: var=testvar

outputs:

"testvar": null

The issue with msg= is that jinja2 then translates nulls/nones into empty strings, for default to work it needs to 'booleanize'. The following works:

      debug: msg="{{testvar|default(22, True)}}"

outputs:

    "msg": "22"

Since it is just a question of the filter interacting with the jinja2 interpolation, I'm going to close this with the above as the solution.

@bcoca bcoca closed this as completed Mar 11, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.2 This issue/PR affects Ansible v2.2 affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

4 participants