So one solution would be to put both tasks in a block, and put the `when: backuppc_client_mysql_dump is defined` condition on the block itself.
On Thursday, March 31, 2022 at 1:46:47 PM UTC-4 Matt Martz wrote: > `when` statements with a loop are evaluated for every iteration, and not > before the loop happens. > > This is documented at: > https://docs.ansible.com/ansible-core/devel/user_guide/playbooks_conditionals.html#using-conditionals-in-loops > > > If you combine a when statement with a loop, 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. > > On Thu, Mar 31, 2022 at 12:43 PM [email protected] <[email protected]> > wrote: > >> I do not know if this is a bug or I do not know how to use when: with the >> loop: >> >> The tasks >> - name: debug backuppc_client_mysql_dump >> ansible.builtin.debug: >> msg: "{{ backuppc_client_mysql_dump }}" >> when: backuppc_client_mysql_dump is defined >> tags: backuppc >> >> - name: set up backuppc user MySQL dump authorized_keys >> ansible.posix.authorized_key: >> user: "{{ backuppc_client_item.0.username }}" >> state: "present" >> key_options: "{{ backuppc_client_item.0.key_options }}" >> key: "{{ backuppc_client_item.1 }}" >> manage_dir: true >> loop: "{{{backuppc_client_mysql_dump|subelements('authorized_keys') }}" >> loop_control: >> loop_var: backuppc_client_item >> when: backuppc_client_mysql_dump is defined >> tags: backuppc >> >> Ansible run >> TASK [backuppc-client : debug backuppc_client_mysql_dump] >> ********************** >> skipping: [docker.*.com] >> >> TASK [backuppc-client : set up backuppc user MySQL dump authorized_keys] >> ******* >> fatal: [docker.*.com]: FAILED! => {"msg": "obj must be a list of dicts or >> a nested dict"} >> >> The debug task is skipped because backuppc_client_mysql_dump is not >> defined. I expect the MySQL dump authorized_keys task to be skipped too but >> it's not. >> >> Any help? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ansible Project" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/2126286d-150d-4646-8684-753ee437d67dn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/ansible-project/2126286d-150d-4646-8684-753ee437d67dn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Matt Martz > @sivel > sivel.net > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/78610b23-45b1-43e0-8c56-8bb681be1abdn%40googlegroups.com.
