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