Ansible – Blocks, Rescue and Error Handling

Ansible_Logo

Usually the Handlers work with a single task. However the “blocks” feature in Ansible helps us to group multiple tasks under one block section and attach handlers or rescue feature to it.

In the below example the Block and Rescue feature will be used to display a message if one of the task out of two fails. Please note that the httpd service is NOT installed. Therefore the ansible task to start the httpd service will fail.

[root@centos9vm ~]# cat block_n_rescus.yml

==== ====
– – –
– name: Copy a file to managed node if the httpd server that is stopped in present state is started by the play book
    hosts: 192.168.48.129
    tasks:
        – name: A Block of tasks
            block:
                – name: Restart httpd server
                    ansible.builtin.service:
                        name: httpd
                        state: started
                        enabled: false

                – name: Copy Index file
                    ansible.builtin.copy:
                        src: shiju.html
                        dest: /var/www/html/index.html
            rescue:
                – name: Error messahe when error identified
                    ansible.builtin.debug:
                        msg: “An error was seen on block”

==== ===

Let us execute the playbook and see if the message in rescue section is displayed

[root@centos9vm ~]# ansible-navigator run -m stdout block_n_rescus.yml

======= ====
PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***

TASK [Gathering Facts] *********************************************************

ok: [192.168.48.129]

TASK [Restart httpd server] ****************************************************
fatal: [192.168.48.129]: FAILED! => {“changed”: false, “msg”: “Could not find the requested service httpd: host”}

TASK [Error messahe when error identified] *************************************
ok: [192.168.48.129] => {
“msg”: “An error was seen on block”
}

PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
==== ====

Now let us install the httpd service and then re-execute the playbook. As the service is present, all tasks should get executed without any error, and therefore the rescue section should not get called.

[root@centos9vm ~]# ssh 192.168.48.129 ‘dnf install httpd -y’

[root@centos9vm ~]# ansible-navigator run -m stdout block_n_rescus.yml

======= ===

PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]

TASK [Restart httpd server] ****************************************************
changed: [192.168.48.129]

TASK [Copy Index file] *********************************************************
ok: [192.168.48.129]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
=== ==== ===