Ansible – Error Handling

Ansible_Logo

Do not misunderstand the concept error handling in this document with error handling in usual software programming terminology.

Usually when one writes a program, a syntax check will check for syntax including indentation, spacing, etc. But it will not check if content such as package name to be installed using the playbook, parameters of a function in modules mentioned in the task , etc are correct. In this case the syntax check may be successful, but when running the playbook the execution may fail at the line incorrect parameters are mentioned and thus halt further execution of the playbook.

The below example is of a playbook that tries installation of two dnf packages. However due to the typo related httpd the remaining execution of the playbook to install mariadb server too stops.

[root@centos9vm ~]# cat errorHandler.yml

===== ====
– – –
– name: Task Failure Exercise
    hosts: 192.168.48.129
    vars:
        web_package: http
        db_pacakge: mariadb-server
        myPackage:
            – http
            – mariadb-server
        db_service: mariadb
    tasks:
        – name: Install httpd
            ansible.builtin.dnf:
                name: “{{ web_package }}”
                state: present

        – name: Install mariadb
            ansible.builtin.dnf:
                name: “{{ db_pacakge }}”
                state: present

==== ===

[root@centos9vm ~]# ansible-navigator run -m stdout errorHandler.yml –syntax-check

===== ===
playbook: /root/errorHandler.yml

===== ===

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

===== ====

PLAY [Task Failure Exercise] ***************************************************

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

TASK [Install httpd] ***********************************************************
fatal: [192.168.48.129]: FAILED! => {“changed”: false, “failures”: [“No package http available.”], “msg”: “Failed to install some of the specified packages”, “rc”: 1, “results”: []}

PLAY RECAP *********************************************************************
192.168.48.129 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Please review the log for errors.

===== ===

Now let us add the error handling parameter and check the result.

[root@centos9vm ~]# cat errorHandler.yml

==== ====
– – –
– name: Task Failure Exercise
    hosts: 192.168.48.129
    vars:
        web_package: http
        db_pacakge: mariadb-server
        myPackage:
            – http
            – mariadb-server
        db_service: mariadb
    tasks:
        – name: Install httpd
            ansible.builtin.dnf:
                name: “{{ web_package }}”
                state: present
            ignore_errors: yes

        – name: Install mariadb
            ansible.builtin.dnf:
                name: “{{ db_pacakge }}”
                state: present

==== ===

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

===== ===

PLAY [Task Failure Exercise] ***************************************************

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

TASK [Install httpd] ***********************************************************
fatal: [192.168.48.129]: FAILED! => {“changed”: false, “failures”: [“No package http available.”], “msg”: “Failed to install some of the specified packages”, “rc”: 1, “results”: []}
…ignoring

TASK [Install mariadb] *********************************************************
changed: [192.168.48.129]

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