I want to include tasks based on the condition set inside the task.
tasks:
- name: SET INIT STATUS
set_facts:
passed: true
- name: INCLUDE TASKS WITH LOOP
include_tasks: update_status.yml
loop: [1, 2, 3]
when: passed
update_status.yml:
- name: OUTPUT THE STATUS
debug: msg="ITEM: {{item}} has status {{passed}}"
- name: UPDATE the Status
set_fact:
passed: false
What I expected that it will only run once for item 1 because after it runs, it will change the status to false and then it failed the condition check. But it actually run 3 time so looks like it does not check the status during the loop again.
Here is the output
TASK: [SET INIT STATUS]
ok: [localhost]
TASK: [INCLUDE TASKS WITH LOOP]
included: simple.yml for localhost
included: simple.yml for localhost
included: simple.yml for localhost
TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 1 has status True
}
TASK: [UPDATE the Status]
ok: [localhost]
TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 2 has status False
}
TASK: [UPDATE the Status]
ok: [localhost]
TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 3 has status False
}
TASK: [UPDATE the Status]
ok: [localhost]
After trying different approach, i was able to achieve what i want by putting the loop inside a block.
- name: LOOP WITH INCLUDE_TASK INSIDE BLOCK
block:
- name: LOOP WITH INCLUDE_TASK
include_tasks: simple.yml
loop: [1, 2, 3]
when: passed
And here is the result
TASK [SET passed] ******************************************************************************************
ok: [localhost]
TASK [LOOP WITH INCLUDE_TASK] ******************************************************************************************
included: /home/richard/samples/simple.yml for localhost
included: /home/richard/samples/simple.yml for localhost
included: /home/richard/samples/simple.yml for localhost
TASK [SHOW THE STATUS] ******************************************************************************************
ok: [localhost] => {
"msg": "Item 1 has status True"
}
TASK [CHANGE THE STATUS] ******************************************************************************************
ok: [localhost]
TASK [SHOW THE STATUS] ******************************************************************************************
skipping: [localhost]
TASK [CHANGE THE STATUS] ******************************************************************************************
skipping: [localhost]
TASK [SHOW THE STATUS] ******************************************************************************************
skipping: [localhost]
TASK [CHANGE THE STATUS] ******************************************************************************************
skipping: [localhost]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With