minimaximal
minimaximal

Reputation: 41

Ansible is ignoring until statement with import_tasks

I have the following two playbooks / tasks files. I want to re-run the children tasks until the result is neither changed nor failed, but max 6 times.

I have the impression that the until statement is simply ignored with the import statement.

The child file is executed only once, with no errors or failures. I inserted my test task directly in the until tasks in the parent file -> everything works fine.

But I need to use more than one child task (run the main task and then restart).

I know that you can't use until-loop with blocks and include_tasks don't work with until either. But I read the documentation as saying that import_tasks and until should work together (https://docs.ansible.com/ansible/latest/collections/ansible/builtin/import_tasks_module.html#attributes)

Is this behavior correct / intended or am I doing something wrong? If this behavior is intended, how could I solve my problem?

playbook.yaml

- name: "make this working"
  hosts: mygroup
  
  tasks:

    - name: rerun up to 6 times if not everything is ok
      until: (not result_update.changed) and (not result_update.failed)
      retries: 5
      ansible.builtin.import_tasks: "./children.yaml"

children.yaml


- name: shell1
  ansible.windows.win_shell: echo "test1"
  register: result_update
  # changed_when: false

- name: shell2
  ansible.windows.win_shell: echo "test2"

Upvotes: 1

Views: 726

Answers (1)

minimaximal
minimaximal

Reputation: 41

I couldn't solve this problem with the until statement. Instead I build my own loop system which is clearly not really ansible like and not an ideal solution but it works fine (at least for my needs).

playbook.yaml

- name: "make this working"
  hosts: mygroup
  
  tasks:

    - name: create variable for maximum tries
      ansible.builtin.set_fact:
        tries_max: 10

    - name: create variable for counting tries
      ansible.builtin.set_fact:
        tries_counter: 1


    - name: run this ({{ tries_counter }} / {{ tries_max }})
      ansible.builtin.include_tasks: "./children.yaml"

children.yaml

- name: shell1
  ansible.windows.win_shell: echo "test1"
  register: result_update
  # changed_when: false

- name: shell2
  ansible.windows.win_shell: echo "test2"

- name: increase try counter by 1
  ansible.builtin.set_fact:
    tries_counter: "{{ (tries_counter | int) + 1 }}"

- name: run tasks again if not everything is ok ({{ tries_counter }} / {{ tries_max }})
  when: ((result_update.changed) or (result_update.failed)) and ((tries_counter | int) <= (tries_max | int))
  ansible.builtin.include_tasks: "./children.yaml"

Upvotes: 2

Related Questions