trying to parse a dict to add users: missing something easy...

19 views
Skip to first unread message

Dan Geist

unread,
Jan 30, 2017, 1:24:34 PM1/30/17
to Ansible Project
Hi, I'm trying to update user accounts on a number of linux hosts vi a dict containing the users. Here's a sample vars:

        "user_accounts": [
            {
                "testusr": {
                    "gecos": "Test User", 
                    "grp": "wheel", 
                    "pass": "123abc", 
                    "status": "enabled", 
                    "username": "testusr"
                }
            }
        ]


Here's the play:

- name: Sync User Accounts (create)
  user:
    name: "{{ item['username'] }}"
    comment: "{{ item['gecos'] }}"
    password: "{{ item['pass'] | default('notavalidpassword') }}"
    group: "{{ item['grp'] | default('') }}"
    shell: "{{ item['shell'] | default('/bin/bash') }}"
    update_password: always
    state: present
  when: "{{ item['status'] }}" == 'enabled'
  with_items:
    - "{{ user_accounts }}"
  tags: users

I'm getting a syntax error:

The offending line appears to be:

    state: present
  when: "{{ item['status'] }}" == 'enabled'
                               ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:


Any ideas?

Dan

Stankovic, Marko

unread,
Jan 31, 2017, 9:29:56 AM1/31/17
to ansible...@googlegroups.com
Hi Dan,

1. If you use a quote at the start, you should close it only at the end. So this should work:

when: "{{ item['status'] }} == 'enabled'"

2. In line above, you don't need {{ and thus quotes, when using when: 'always use {{ }} except when when:'<http://docs.ansible.com/ansible/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names>

3. You are missing "testusr" when accessing an item's elements, or better said, "testusr" part in var definition is an extra.
This would work better:

"user_accounts": [
{
"gecos": "Test User",
"grp": "wheel",
"pass": "123abc",
"status": "enabled",
"username": "testusr"
}
]

If you really need "testusr" data, you could put it as an additional element:

"user_accounts": [
{

"id": "testusr",

"gecos": "Test User",
"grp": "wheel",
"pass": "123abc",
"status": "enabled",
"username": "testusr"
}
]

So, with the variable declared as above, this would be a cleaner way to accomplish what you want:
when: item.status == 'enabled'

Cheers,
Marko


______________________________________________________________________________________________________

CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or
its subsidiaries and may contain proprietary, confidential or trade secret information.
This message is intended solely for the use of the addressee. If you are not the intended recipient
and have received this message in error, please delete this message from your system. Any unauthorized
reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

Reply all
Reply to author
Forward
0 new messages