# Creating users & groups with vars - not working

**URL:** <https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918>\
**Category:** Ansible Project\
**Created:** [July 7, 2017, 2:01am UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918 "2017-07-07T02:01:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 7, 2017, 2:01am UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/1 "2017-07-07T02:01:29Z")

</div>

Trying to create users and groups with vars inline…this is not working…what am I missing here?

— # Users and Groups

- hosts: localhost  
become: yes  
vars:  
groups:
- devops
- dbadbmins
- serveradmins

users:

- frank

- joe

- dave  
tasks:

- name: Create groups  
group:  
name: “{{ item.groups }}”  
state: present  
with\_items: “{{groups}}”  
ignore\_errors: yes

- name: Create users  
user:  
name: “{{ item.user }}”  
state: present  
with\_items: “{{users}}”

Output -

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [Create groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘ansible.vars.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘groups’\n\nThe error appears to have been in ‘/etc/ansible/playbooks/usersgroups.yml’: line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Create groups\n ^ here\n”}  
…ignoring

TASK [Create users] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘ansible.vars.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘user’\n\nThe error appears to have been in ‘/etc/ansible/playbooks/usersgroups.yml’: line 22, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create users\n ^ here\n”}  
to retry, use: --limit @/etc/ansible/playbooks/usersgroups.retry

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 7, 2017, 8:35am UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/2 "2017-07-07T08:35:50Z")

</div>

You are trying to access item.groups, but there is not item.groups. The groupname is item itself. The same thing for item.user!

Just change item.groups to item and item.user to item

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 7, 2017, 1:19pm UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/3 "2017-07-07T13:19:46Z")

</div>

To put Daniel's response into clear example:

&nbsp;&nbsp;- name: Create groups  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{groups}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes

&nbsp;&nbsp;&nbsp;&nbsp;- name: Create users  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{users}}"

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 7, 2017, 6:39pm UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/4 "2017-07-07T18:39:53Z")

</div>

Thanks Brian!

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 7, 2017, 7:24pm UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/5 "2017-07-07T19:24:48Z")

</div>

Thanks. It works but only if I swap with\_items: “{{groups}}” for with\_items: “{{grouplist}}”

and call my vars grouplist also. Something it didnt like about calling those 2 groups

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 7, 2017, 10:22pm UTC](https://forum.ansible.com/t/creating-users-groups-with-vars-not-working/24918/6 "2017-07-07T22:22:44Z")

</div>

i should have noticed, 'groups' is a reserved var, which has a  
dictionary with the inventory groups and the hosts that belong to  
them.

No matter what you set, the 'groups' from Ansible will overwrite it.
