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

boolean interpreted as dict, 2.0 #12206

Closed
mattwillsher opened this issue Sep 2, 2015 · 9 comments
Closed

boolean interpreted as dict, 2.0 #12206

mattwillsher opened this issue Sep 2, 2015 · 9 comments

Comments

@mattwillsher
Copy link
Contributor

I have a dict:

users:
  - name: matt
    comment: 'Matt Willsher'
    is_admin: True
    ssh_keys:
      - ecdsa-sha2-nistp256 AAAAE2

1.9.2 debug:

ok: [sam.wilsys.net] => {
    "var": {
        "users": [
            {
                "comment": "Matt Willsher",
                "is_admin": true,
                "name": "matt",
                "ssh_keys": [
                    "ecdsa-sha2-nistp256 AAAAE2"
                ]
            }
        ]
    }
}

2.0 output:

ok: [sam.wilsys.net] => {
    "changed": false,
    "users": [
        {
            "comment": "Matt Willsher",
            "is_admin": {},
            "name": "matt",
            "ssh_keys": [
                "ecdsa-sha2-nistp256 AAAAE2"
            ]
        }
    ]
}

Then:

failed: [sam.wilsys.net] => (item={u'comment': u'Matt Willsher', u'is_admin': {}, u'name': u'matt', u'ssh_keys': [u'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOgLXY1vHW5weJpl5XUm7yZ261OJrv3Pq1R743zS2Ssw', u'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGFdUSXkWRPKbHZtPMexORfQxuav9zVpHwFmifY92sTIE3uy5CbmvaAch3+sf/Qg3oBUCR2mhp729uEyGuaZAZc=']}) => {"failed": true, "item": {"comment": "Matt Willsher", "is_admin": {}, "name": "matt", "ssh_keys": ["ecdsa-sha2-nistp256 AAAAE2"]}, "msg": "argument createhome is of type <type 'dict'> and we were unable to convert to bool"}

The code

- name: Create users
  user:
    name: '{{ item.name }}'
    state: '{{ item.state | default("present") }}'
    uid: '{{ item.uid | default(omit) }}'
    group: '{{ item.group | default(item.name) }}'
    groups: '{{ item.groups | default("") | join(",") }}'
    shell: '{{ item.shell | default(users_default_shell) }}'
    comment: '{{ item.comment | default("Unknown User") }}'
    password: '{{ item.password | default(users_default_password) }}'
    home: '{{ item.home | default("/home/"+item.name) }}'
    createhome: '{{ item.createhome | default(users_create_homedirs) }}'
    system: '{{ item.system | default(omit) }}'
    append: yes
  with_items: users
  when: >
    not ( item.name == ansible_ssh_user and
    item.state|default('present') == 'absent' )

users_create_homedirs is set as yes is the defaults/main.yml

The code works as expected in 1.9

$ ansible-playbook --version
ansible-playbook 2.0.0 (devel e8d7fafc52) last updated 2015/09/02 09:05:09 (GMT +100)
  lib/ansible/modules/core: (detached HEAD ef1f31c652) last updated 2015/08/31 21:19:36 (GMT +100)
  lib/ansible/modules/extras: (detached HEAD 5b39e19387) last updated 2015/08/31 21:19:36 (GMT +100)
  config file = /Users/matt/Development/ansible/ansible.cfg
  configured module search path = None
@bretmartin
Copy link
Contributor

+1 this is happening to me too

@bretmartin
Copy link
Contributor

Poking at this a bit more, I think the problem is actually something with the jinja2 filtering or default().

test.yml:

---

- hosts: localhost
  tasks:
  - set_fact:
      test: '{{ bogus_variable | default(True) }}'
  - debug: var=test

Run:

% ansible-playbook -i inventory test.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [set_fact test={{ bogus_variable | default(True) }}] **********************
ok: [localhost]

TASK [debug var=test] **********************************************************
ok: [localhost] => {
    "changed": false,
    "test": {}
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

@bretmartin
Copy link
Contributor

Using git bisect I have isolated this to 54dbfba

@mgedmin
Copy link
Contributor

mgedmin commented Sep 2, 2015

Confirming: I can reproduce this with 54dbfba, but not with 5adcd70.

Weird. Investigating.

@mgedmin
Copy link
Contributor

mgedmin commented Sep 2, 2015

Out of the two changes in 54dbfba, it's the change to lib/ansible/vars/init.py that breaks this.

@mgedmin
Copy link
Contributor

mgedmin commented Sep 2, 2015

Ok, the bug is that the dicts passed to _combine_vars() are sometimes not dicts but collections.defaultdict() instances. The previous code would return a regular dict, my new code now returns a collections.defaultdict() in that case.

Fix coming soon.

@jimi-c
Copy link
Member

jimi-c commented Sep 2, 2015

Closing This Ticket

Hi!

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!

@bretmartin
Copy link
Contributor

Fix confirmed, thanks @jimi-c and @mgedmin!

@mattwillsher
Copy link
Contributor Author

Likewise, now works for me. Thanks!

@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
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants