Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to control the execution of rescue module in ansible

Tags:

ansible

I have a playbook like this below ,i want to control execution of rescue when the block goes wrong。 I add when: is_debug is defined after rescue ,but when i run ansible-playbook dashboard.yml ,not give the is_debug value, the rescue is still excute when block has error .

i want know how to control the execution of rescue module in ansible ?

[root@ansiable-test update]# cat dashboard.yml
---
- hosts: controller
  vars_files:
        - "vars/vars.yml"
  tasks:
    - name: update horizon
    - block:
        - include: "roles/dashboard/tasks/update-horizon.yml"
      rescue:
      when: is_debug is defined
        - debug: msg='An error occurred during the upgrade,roll back to the old version'
        - include: "roles/dashboard/tasks/rollback.yml"
like image 767
qianwang Avatar asked Oct 27 '25 05:10

qianwang


1 Answers

There is no rescue "module". It's part of the block syntax, and you cannot apply when to just the rescue portion. You can apply the when condition to the task (or tasks) inside your rescue stanza:

- hosts: controller
  vars_files:
        - "vars/vars.yml"
  tasks:
    - name: update horizon
    - block:
        - include: "roles/dashboard/tasks/update-horizon.yml"
      rescue:
        - debug: msg='An error occurred during the upgrade,roll back to the old version'
          when: is_debug is defined

        - include: "roles/dashboard/tasks/rollback.yml"
          when: is_debug is defined
like image 177
larsks Avatar answered Oct 30 '25 10:10

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!