1

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?

KrNeki
  • 135
  • 8
  • 2
    This is the exact purpose of the omit paremeter in the default filter => `uid: "{{ item.uid | default(omit) }}". https://docs.ansible.com/ansible/2.3/playbooks_filters.html#omitting-parameters – Zeitounator Dec 01 '20 at 12:54
  • Huh.. so it does. Thanks. Want to make it an answer? – KrNeki Dec 01 '20 at 13:26
  • Nope. I was just too lazy^wbusy to look for a duplicate answer. This is an FAQ. – Zeitounator Dec 01 '20 at 13:34
  • 1
    Does this answer your question? [How to omit if variable is empty in Ansibe?](https://stackoverflow.com/questions/62140639/how-to-omit-if-variable-is-empty-in-ansibe) – Zeitounator Dec 01 '20 at 13:39
  • Not really. The answer is the same, but I would never have landed on that question. I am asking what is the default value for the ansible key in a module - the answer you gave me (correct) is that there is no such thing, but that the key itself can be omitted with the *omit* as the defined key value.. – KrNeki Dec 01 '20 at 13:50

1 Answers1

2

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

Angus
  • 96
  • 7