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

**URL:** <https://forum.ansible.com/t/trying-to-parse-a-dict-to-add-users-missing-something-easy/23598>\
**Category:** Ansible Project\
**Created:** [January 30, 2017, 5:04pm UTC](https://forum.ansible.com/t/trying-to-parse-a-dict-to-add-users-missing-something-easy/23598 "2017-01-30T17:04:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Geist](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Dan\_Geist](https://forum.ansible.com/u/Dan_Geist)\
**Post date:** [January 30, 2017, 5:04pm UTC](https://forum.ansible.com/t/trying-to-parse-a-dict-to-add-users-missing-something-easy/23598/1 "2017-01-30T17:04:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 31, 2017, 2:29pm UTC](https://forum.ansible.com/t/trying-to-parse-a-dict-to-add-users-missing-something-easy/23598/2 "2017-01-31T14:29:44Z")

</div>

Hi Dan,

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

&nbsp;&nbsp;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&gt](http://docs.ansible.com/ansible/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names&gt);

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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user\_accounts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"gecos": "Test User",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"grp": "wheel",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"pass": "123abc",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"status": "enabled",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"username": "testusr"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user\_accounts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"id": "testusr",

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

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

Cheers,  
Marko
