# creating users

**URL:** <https://forum.ansible.com/t/creating-users/16245>\
**Category:** Ansible Project\
**Created:** [September 16, 2014, 1:52pm UTC](https://forum.ansible.com/t/creating-users/16245 "2014-09-16T13:52:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank3](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Frank3](https://forum.ansible.com/u/Frank3)\
**Post date:** [September 16, 2014, 1:52pm UTC](https://forum.ansible.com/t/creating-users/16245/1 "2014-09-16T13:52:49Z")

</div>

Hi,

I am trying to create users and groups and add users to groups and keys well, you know.

Does this layout looks correct?

vars/users.yml

groups:  
admins:  
state: present  
webmasters:  
state: present

admins:  
user1:  
state: present  
authorized:

- keys/ssh/user1.pub

webmasters:  
user2:  
state: present  
authorized:

- keys/ssh/user2.pub  
user3:  
state: present  
authorized:
- keys/ssh/user3.pub  
user4:  
state: present  
authorized:
- keys/ssh/user4.pub

tasks/main.yml

- name: Admins  
user:  
name={{ item.key }}  
shell=/bin/bash  
createhome=yes  
state={{ item.value.state }}  
with\_dict: admins

- name: Webmasters  
user:  
name={{ item.key }}  
shell=/home/bash  
createhome=yes  
state={{ item.value.state }}  
with\_dict: webmasters

- name: Groups  
group: name={{ item.key }} state={{ item.value.state }}  
with\_dict: groups

I get users admins and webmasters created but when creating groups things went wrong…

“…

TASK: […/roles/support | SUPPORT | add groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [support] =\> One or more undefined variables: ‘list object’ has no attribute ‘state’

FATAL: all hosts have already failed — aborting  
…”

The question is: Is this layout the best approach for managing users/groups and keys?

Cheers,  
Frank

---

<div class="post-metadata">

**Author:** ![Frank3](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Frank3](https://forum.ansible.com/u/Frank3)\
**Post date:** [September 16, 2014, 2:03pm UTC](https://forum.ansible.com/t/creating-users/16245/2 "2014-09-16T14:03:06Z")

</div>

Err, group is a reserved word…

I’ve changed to mygroups:

changed: [support] =\> (item={‘key’: ‘admins’, ‘value’: {‘state’: ‘present’}})  
changed: [support] =\> (item={‘key’: ‘webmasters’, ‘value’: {‘state’: ‘present’}})

Anyway, Is there any other (better) layout to define users?

Regards,  
Frank

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 16, 2014, 3:21pm UTC](https://forum.ansible.com/t/creating-users/16245/3 "2014-09-16T15:21:14Z")

</div>

I’d simplify by keeping a seperate list of users you want to be present and users to remove, that way you don’t have to repeat all the ‘state’ stuff.

Just make a task to add and another to remove.

---

<div class="post-metadata">

**Author:** ![Frank3](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Frank3](https://forum.ansible.com/u/Frank3)\
**Post date:** [September 16, 2014, 3:37pm UTC](https://forum.ansible.com/t/creating-users/16245/4 "2014-09-16T15:37:44Z")

</div>

The nice tip: I will assume that users are all present and removed\_users are absent.

users:

- name: user1  
keys:
- user1.pub  
groups:
- admins
- webmasters

removed\_users:

- foobar
