1

I'm attempting to create an Ansible Role to create users... I want it to be able to accept a list of dicts and if a key is missing from the dict, it will fall back to Ansible's default.

For example, given the following data:

---
users:
 - { name: foo, group: bar, create_home: no }
   { name: goo, group: bar }

I'd normally loop over the list with something like:

- name: Add users
  ansible.builtin.user:
    name: "{{ item.name }}"
    groups: "{{ item.group }}"
    create_home: "{{ item.create_home }}"
    state: present
  loop: "{{ users }}"

However, because create_home doesn't exist for the second user, it'd throw an error. How could I get it to fall back to Ansible's default for keys that don't exist?

1 Answer 1

1

Q: "How could I get it to fall back to Ansible's default for keys that don't exist?"

A: Omit such parameters, e.g.

    create_home: "{{ item.create_home|default(omit) }}"
1
  • Oh, that looks to be exactly what I needed... thanks! ๐Ÿ˜ I'm really not a fan of Ansible's documentation; I don't find it to be very newcomer friendly. ๐Ÿ˜• I'll give it a try later when I have some time and I'll accept this as the solution if it works, which I'm sure it will. ๐Ÿ‘๐Ÿผ Commented Mar 4, 2021 at 18:42

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.