Using conditionals in loops
If you combine awhenstatement with aloop, Ansible processes the condition separately for each item. This is by design, so you can execute the task on some items in the loop and skip it on other items. For example:
If you need to skip the whole task when the loop variable is undefined, use the|defaultfilter to provide an empty iterator. For example, when looping over a list:
You can do the same thing when looping over a dict: