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

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. 👍🏼 Mar 4 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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