What if I want to skip the whole loop in Ansible?
In accordance with the recommendations
When combining when with with_items (see "Loops"), the ... when operator is processed separately for each element.
Thus, during the work of such a play
--- - hosts: all vars: skip_the_loop: true tasks: - command: echo "{{ item }}" with_items: [1, 2, 3] when: not skip_the_loop
I get
skipping: [localhost] => (item=1) skipping: [localhost] => (item=2) skipping: [localhost] => (item=3)
While I do not want the condition to be checked every time.
Then I came up with the idea of ββusing inline conditions
- hosts: all vars: skip_the_loop: true tasks: - command: echo "{{ item }}" with_items: "{{ [1, 2, 3] if not skip_the_loop else [] }}"
I seem to have solved my problem, but then I get nothing. And I want only one line:
skipping: Loop has been skipped
source share