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

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.

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.

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