Understanding block and rescue

46 views
Skip to first unread message

SysAdmin EM

unread,
Aug 18, 2022, 11:51:50 AM8/18/22
to ansible...@googlegroups.com
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, 

Rowe, Walter P. (Fed)

unread,
Aug 18, 2022, 11:56:34 AM8/18/22
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtn%3DJzZ5B5Fh3QPP0rQ3pyQv1XchPzE_rLAcU0OksYezFBQ%40mail.gmail.com.

Daniel Barros

unread,
Aug 18, 2022, 2:05:55 PM8/18/22
to Ansible Project
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

---
- block:
    - name: "status systemd-resolved"
      command: systemctl status systemd-resolved
      register: resolved_status
  rescue:
    - name: "Execute this if task 1 fails"
      command: echo "Failed"

Reply all
Reply to author
Forward
0 new messages