# Using defaults for Ansible user module for for "home", "group" and "groups"

**URL:** <https://forum.ansible.com/t/using-defaults-for-ansible-user-module-for-for-home-group-and-groups/29937>\
**Category:** Ansible Project\
**Created:** [March 21, 2019, 3:09pm UTC](https://forum.ansible.com/t/using-defaults-for-ansible-user-module-for-for-home-group-and-groups/29937 "2019-03-21T15:09:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suhail\_Choudhury](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@Suhail\_Choudhury](https://forum.ansible.com/u/Suhail_Choudhury)\
**Post date:** [March 21, 2019, 3:09pm UTC](https://forum.ansible.com/t/using-defaults-for-ansible-user-module-for-for-home-group-and-groups/29937/1 "2019-03-21T15:09:25Z")

</div>

Hi,

I’m trying to set optional values only if they are specified for the “user” module.

So with:

- name: Add users  
user:  
name: “{{ item.username }}”  
comment: “{{ item.comment }}”  
state: present  
shell: “{{ (item.shell) | default(‘/bin/bash’) }}”

password: “{{ item.password }}”  
update\_password: always  
with\_items: “{{ merged\_users }}”  
tags: users

I only have to add an optional shell variable with anotehr value such as ‘/bin/ksh’ to override the default bash value.

How can I do the same for:

home: “{{ item.home }}”  
group: “{{ item.group }}”  
groups: “{{ item.groups }}”

As the home directory defaults to ‘/home/username’, group defaults to ‘username’ and groups also defaults to ‘username’.

I’ve tried something like:

group: “{{ (item.group) | default(‘/bin/bash’) }}”

but no joy.

So please help with these 3 values - how do I define defaults for “home”, “group” and “groups”?

Regards,  
Suhail.

---

<div class="post-metadata">

**Author:** ![Suhail\_Choudhury](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@Suhail\_Choudhury](https://forum.ansible.com/u/Suhail_Choudhury)\
**Post date:** [March 22, 2019, 11:26am UTC](https://forum.ansible.com/t/using-defaults-for-ansible-user-module-for-for-home-group-and-groups/29937/2 "2019-03-22T11:26:41Z")

</div>

Sorry I meant I’ve tried something like:

group: “{{ (item.group) | default(‘’) }}”

but no joy as Ansible throws the error that “Group “” does not exist”.

If I try

group: “{{item.group if item.group is defined else item.username }}”

then Ansible fails with the group does not exist.

Whereas the default without specifying is that it creates it as the username.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [March 22, 2019, 12:34pm UTC](https://forum.ansible.com/t/using-defaults-for-ansible-user-module-for-for-home-group-and-groups/29937/3 "2019-03-22T12:34:03Z")

</div>

> Sorry I meant I've tried something like:
> 
> group: "{{ (item.group) | default('') }}"

Try default(omit), that'd just not set the parameter if item.group isn't  
defined.

- Sebastian
