Understanding block and rescue

Hi, sorry for my english, i still learning.
I need to understand, how its works the blocks and rescue mode in ansible.

I create a playbook similar like this:

  • name: “status systemd-resolved”
    shell:
    cmd: |
    systemctl status systemd-resolved
    register: resolved_status

  • block:

  • name: “Task 1”

  • name: “Task 2”
    rescue:

  • name “Execute this if task 1 fails”

The first task fails because systemd-resolved is not installed:

fatal: [ema-test]: FAILED! => {“changed”: true, “cmd”: “systemctl status systemd-resolved\n”, “delta”: “0:00:00.039851”, “end”: “2022-08-18 15:34:04
.922583”, “msg”: “non-zero return code”, “rc”: 3, “start”: “2022-08-18 15:34:04.882732”, “stderr”: “”, “stderr_lines”: , “stdout”: “● systemd-reso
lved.service - Network Name Resolution\n Loaded: loaded (/lib/systemd/system/systemd-resolved.service; disabled; vendor preset: enabled)\n Drop-I
n: /lib/systemd/system/systemd-resolved.service.d\n └─resolvconf.conf\n Active: inactive (dead)\n Docs: man:systemd-resolved.service
(8)\n http://www.freedesktop.org/wiki/Software/systemd/resolved\n http://www.freedesktop.org/wiki/Software/systemd/writing-netwo
rk-configuration-managers\n http://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients”, “stdout_lines”: [“● systemd-resolv
ed.service - Network Name Resolution”, " Loaded: loaded (/lib/systemd/system/systemd-resolved.service; disabled; vendor preset: enabled)“, " Drop
-In: /lib/systemd/system/systemd-resolved.service.d”, " └─resolvconf.conf", " Active: inactive (dead)“, " Docs: man:systemd-resolved
.service(8)”, " http://www.freedesktop.org/wiki/Software/systemd/resolved", " http://www.freedesktop.org/wiki/Software/systemd/w
riting-network-configuration-managers", " http://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients"]}

but the rescue never run.

what is my mistake?

my idea is verify if systemd-resolved is installed, if not, i run others tasks.

Regards,

Hard to say without seeing the actual code. This page discusses blocks and rescue.

https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html

Walter

Hi, I believe I replied only to the author, so sorry if anyone is seeing duplicated messages from me.

But try moving the task that failed inside the block. Its currently outside