0

I am trying to iterate multiple tasks based on the result from one of the tasks in the child yml file.

Since, looping on blocks is not possible, I have included all the tasks in task file and including that child file from main playbook.

main.yml:

- hosts: host01
  vars:
    state: "running"
  tasks:
    - name: include tasks file
      include_tasks: ./abc.yml
      when: state == "running"
      with_sequence: start=1 end=3


abc.yml:

- name: get the value from the system
  shell: echo something
  register: out

- name: override the variable state as completed
  set_fact: 
    state: "completed"
  when: out.rc == 0

Failure case: so here, i should iterate the include tasks file until I get state as "completed"(try max three times) . If not fail the Playbook.

success case: If out.rc results zero, the variable state is overriden with "completed" in first iteration itself but still its executing two more times instead of exiting.

What am I missing here? or anyother way we can iterate multiple tasks based on output of one of the tasks?

prasanna kumar
  • 257
  • 3
  • 4
  • 17

1 Answers1

0

"If out.rc results zero, the variable state is overriden with "completed" in first iteration itself but still its executing two more times instead of exiting."

A: when applies to each iteration and not to the with_sequence loop as a whole. It seems to be an issue that the when condition is not updated on iterations when include_tasks module is used.

"To iterate multiple tasks based on the output of one of the tasks (shell)"

A: You'll be better off to implement as much logic as possible in shell (for example an internal counter of the loops with 3 states: running-interrupted-completed in this case).

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63