Ansible user module: how to have the UID defined to a default setting?

979 Views Asked by At

I have a list of dictionaries containing user details. Some of them need specific UIDs, but most of them don't.

Example:

  # list_of_dictionaries__users
- { name: user1, uid: 654, group: user1 }
- { name: user2, group: user2 }
- { name: user3, group: user3 }
- { name: user4, group: user4 }
- { name: user5, uid: 657, group: user5 }

When I use the Ansible user module in a loop like this,

- name: Ensure user existence
  user:
    name: {{  item.name  }}
    uid: {{  item.uid | default('ANY_UID')}}
    group: {{  item.group }}
  loop: {{ list_of_dictionaries__users }}

I don't know how to tell ansible to treat the uid parameter as if it wasn't set at all, that is, to assign whatever UID linux gives it.

Any ideas?

1

There are 1 best solutions below

0
On

You should be able to use the "omit" keyword to make the value optional.

- name: Ensure user existence
  user:
    name: {{  item.name  }}
    uid: {{  item.uid | default(omit)}}
    group: {{  item.group }}
  loop: {{ list_of_dictionaries__users }}

This is covered in the Ansible documentation here