title: How to handle empty list in Ansible loop
body: <p>Context is modifying many DNS zone records across different servers, using <code>community.windows.win_dns_record</code> in a loop.  Each DNS server has a var file in <code>host_vars</code>.  The variable for the list of record changes is <code>zone_records</code>.</p>
  <p>Sometimes there are no changes on a server, and I'd like to leave the <code>zone_records</code> variable undefined, but I am open to setting it to an empty list, or empty string.</p>
  <p>Here an attempt is made to short-circuit the loop by <code>when: zone_records is iterable</code></p>
  <pre class="lang-yaml prettyprint-override"><code>- name: Remove zone records
    community.windows.win_dns_record:
      computer_name: &quot;{{ remove.server_name }}&quot;
      zone: &quot;{{ remove.zone }}&quot;
      name: &quot;{{ remove.name }}&quot;
      state: &quot;{{ remove.state }}&quot;
      type: &quot;{{ remove.type }}&quot;
    loop:
      &quot;{{ query('dict', zone_records) }}&quot;
    when:
      - zone_records is iterable
      - remove.state == 'absent'
    loop_control:
      loop_var: remove
  </code></pre>
  <p>The above fails differently depending on whether <code>zone_records</code> is undefined, or a string instead of a list.<br />
  In the latter case, the error is:</p>
  <blockquote>
  <p>Invalid data passed to 'loop', it requires a list</p>
  </blockquote>
  <p>How should lists be handled in loops where sometimes there's no list or an empty list?</p>