Ansible ERROR! 'retries' is not a valid attribute for a PlaybookInclude

37 Views Asked by At

I want to run 2 times the playbook1 that may fail using import_playbook.

- name: Run playbook twice with potential failure
  ansible.builtin.import_playbook: playbook1.yml
  retries: 2
  delay: 10
  register: result
  until: result.rc == 0

However, there is no retries/dely/register/until attributes for import_playbook https://docs.ansible.com/ansible/latest/collections/ansible/builtin/import_playbook_module.html

ERROR! 'retries' is not a valid attribute for a PlaybookInclude

Any alternatives or workaround ?

1

There are 1 best solutions below

1
trkmh19 On

Unfortunately, the import_playbook module itself doesn't support the retries, delay, register, and until attributes as I know.

However, you can achieve a similar effect you want by incorporating a loop and error handling directly at your playbook1.yml level . Here's an example I do quickly of how you might structure it:

- name: Run playbook1 with potential failure
  hosts: <your_target_hosts>
  tasks:
    - name: Execute the playbook1
      include_tasks: playbook1.yml
      loop: "{{ range(2) }}"
      loop_control:
        loop_var: retry_count
      register: result
      until: result.rc == 0
      retries: 2
      delay: 10
      when: result is failed

Here, the include_tasks module is used to include playbook1.yml, and a loop is added to run it twice (range(2)).

The register attribute captures the result, and the until attribute is used to retry if the result has a non-zero return code.

The retries and delay attributes control the retry behavior.

I hope this helps you to achieve the desired behavior.