Answers: <p>Q: <em>&quot;<strong>How could I get it to fall back to Ansible's default for keys that don't exist?</strong>&quot;</em></p>
  <p>A: <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#making-variables-optional" rel="nofollow noreferrer">Omit</a> such parameters, e.g.</p>
  <pre class="lang-yaml prettyprint-override"><code>    create_home: &quot;{{ item.create_home|default(omit) }}&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-04T18:42:58.870Z
Answers: 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. 
Answers: user1960364
Answers: CC BY-SA 4.0
Answers: 2021-03-04T10:11:14.630Z
Answers: 2021-03-04T10:11:14.630Z
Answers: Vladimir Botka
Body: <p>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.</p>
  <p>For example, given the following data:</p>
  <pre class="lang-yml prettyprint-override"><code>---
  users:
   - { name: foo, group: bar, create_home: no }
     { name: goo, group: bar }
  
  </code></pre>
  <p>I'd normally loop over the list with something like:</p>
  <pre class="lang-yml prettyprint-override"><code>- name: Add users
    ansible.builtin.user:
      name: &quot;{{ item.name }}&quot;
      groups: &quot;{{ item.group }}&quot;
      create_home: &quot;{{ item.create_home }}&quot;
      state: present
    loop: &quot;{{ users }}&quot;
  </code></pre>
  <p>However, because <code>create_home</code> 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?</p>
  
Tags: <ansible>
Title: Looping an Array with Missing Keys in Ansible Tasks
UserDisplayName: user1960364
ThreadText: # Looping an Array with Missing Keys in Ansible Tasks
  
  (Asked by: username_0 on 2021-03-04)
  
  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:
  
  ```yml
  ---
  users:
   - { name: foo, group: bar, create_home: no }
     { name: goo, group: bar }
  
  ```
  
  I'd normally loop over the list with something like:
  
  ```yml
  - 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?
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-04. Score: 1
  
  Q: *"**How could I get it to fall back to Ansible's default for keys that don't exist?**"*
  
  A: [Omit](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#making-variables-optional) such parameters, e.g.
  
  ```yaml
      create_home: "{{ item.create_home|default(omit) }}"
  
  ```
  
  > **Comments:**
  > **username_0**: 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.   
  
  ---
  
  