Convert with_X to loop: but when: condition being ignored?

29 views
Skip to first unread message

b...@tanners.org

unread,
Mar 31, 2022, 1:43:04 PM3/31/22
to Ansible Project
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?

Matt Martz

unread,
Mar 31, 2022, 1:46:47 PM3/31/22
to ansible...@googlegroups.com
`when` statements with a loop are evaluated for every iteration, and not before the loop happens.


> 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.

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2126286d-150d-4646-8684-753ee437d67dn%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Todd Lewis

unread,
Mar 31, 2022, 4:32:27 PM3/31/22
to Ansible Project
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.

Brian Coca

unread,
Apr 1, 2022, 10:03:02 AM4/1/22
to Ansible Project
blocks are not a way to skip tasks, a 'when' on a block is just for
tasks inside the block to inherit.

> 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.


--
----------
Brian Coca

Todd Lewis

unread,
Apr 1, 2022, 10:13:33 AM4/1/22
to ansible...@googlegroups.com, uto...@gmail.com

Ouch! Then you'd have to put the tasks into their own tasks file, and use include_tasks ...

Or give backuppc_client_mysql_dump a reasonable |default(...) so that the looped task(s) would do no harm.

blocks are not a way to skip tasks, a 'when' on a block is just fortasks inside the block to inherit.

Reply all
Reply to author
Forward
0 new messages