I'm trying to create an ansible playbook to start a service if it's only in stopped state. Created the below playbook.
tasks:
- name: Check the Service Status
ansible.builtin.command: systemctl status mariadb.service
register: service_status
notify: Mariadb restart
changed_when: service_status.rc != 0
ignore_errors: true
handlers:
- name: Mariadb restart
ansible.builtin.service:
name: httpd
state: restarted
When I run this playbook it's failing even I mentioned ignore_errors statement in task.
TASK [Check the Service Status] ************************************************************************************************************************************************************
fatal: [192.168.1.102]: FAILED! => {"changed": true, "cmd": ["systemctl", "status", "mariadb.service"], "delta": "0:00:00.016680", "end": "2024-11-21 00:20:53.396153", "msg": "non-zero return code", "rc": 3, "start": "2024-11-21 00:20:53.379473", "stderr": "", "stderr_lines": [], "stdout": "○ mariadb.service - MariaDB 10.5 database server\n Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)\n Active: inactive (dead) since Wed 2024-11-20 23:30:01 EST; 50min ago\n Duration: 3min 52.893s\n Docs: man:mariadbd(8)\n https://mariadb.com/kb/en/library/systemd/\n Process: 12935 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)\n Process: 12957 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)\n Process: 12992 ExecStart=/usr/libexec/mariadbd --basedir=/usr $MYSQLD_OPTS $_WSREP_NEW_CLUSTER (code=exited, status=0/SUCCESS)\n Process: 13004 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)\n Main PID: 12992 (code=exited, status=0/SUCCESS)\n Status: \"MariaDB server is down\"\n CPU: 526ms\n\nNov 20 23:26:08 RHEL-Node-2 systemd[1]: Starting MariaDB 10.5 database server...\nNov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.\nNov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.\nNov 20 23:26:08 RHEL-Node-2 systemd[1]: Started MariaDB 10.5 database server.\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopping MariaDB 10.5 database server...\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: mariadb.service: Deactivated successfully.\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopped MariaDB 10.5 database server.", "stdout_lines": ["○ mariadb.service - MariaDB 10.5 database server", " Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)", " Active: inactive (dead) since Wed 2024-11-20 23:30:01 EST; 50min ago", " Duration: 3min 52.893s", " Docs: man:mariadbd(8)", " https://mariadb.com/kb/en/library/systemd/", " Process: 12935 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)", " Process: 12957 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)", " Process: 12992 ExecStart=/usr/libexec/mariadbd --basedir=/usr $MYSQLD_OPTS $_WSREP_NEW_CLUSTER (code=exited, status=0/SUCCESS)", " Process: 13004 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)", " Main PID: 12992 (code=exited, status=0/SUCCESS)", " Status: \"MariaDB server is down\"", " CPU: 526ms", "", "Nov 20 23:26:08 RHEL-Node-2 systemd[1]: Starting MariaDB 10.5 database server...", "Nov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.", "Nov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.", "Nov 20 23:26:08 RHEL-Node-2 systemd[1]: Started MariaDB 10.5 database server.", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopping MariaDB 10.5 database server...", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: mariadb.service: Deactivated successfully.", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopped MariaDB 10.5 database server."]}
...ignoring
PLAY RECAP *********************************************************************************************************************************************************************************
192.168.1.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
Can someone help me with this?
I want to start the service only if it's in stopped status.
fatal: [192.168.1.102]: FAILED! => {"changed": true, "cmd": ["systemctl", "status", "mariadb.service"], "delta": "0:00:00.016680", "end": "2024-11-21 00:20:53.396153", "msg": "non-zero return code", "rc": 3, "start": "2024-11-21 00:20:53.379473", "stderr": "", "stderr_lines": [], "stdout": "○ mariadb.service - MariaDB 10.5 database server\n Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)\n Active: inactive (dead) since Wed 2024-11-20 23:30:01 EST; 50min ago\n Duration: 3min 52.893s\n Docs: man:mariadbd(8)\n https://mariadb.com/kb/en/library/systemd/\n Process: 12935 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)\n Process: 12957 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)\n Process: 12992 ExecStart=/usr/libexec/mariadbd --basedir=/usr $MYSQLD_OPTS $_WSREP_NEW_CLUSTER (code=exited, status=0/SUCCESS)\n Process: 13004 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)\n Main PID: 12992 (code=exited, status=0/SUCCESS)\n Status: "MariaDB server is down"\n CPU: 526ms\n\nNov 20 23:26:08 RHEL-Node-2 systemd[1]: Starting MariaDB 10.5 database server...\nNov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.\nNov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.\nNov 20 23:26:08 RHEL-Node-2 systemd[1]: Started MariaDB 10.5 database server.\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopping MariaDB 10.5 database server...\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: mariadb.service: Deactivated successfully.\nNov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopped MariaDB 10.5 database server.", "stdout_lines": ["○ mariadb.service - MariaDB 10.5 database server", " Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)", " Active: inactive (dead) since Wed 2024-11-20 23:30:01 EST; 50min ago", " Duration: 3min 52.893s", " Docs: man:mariadbd(8)", " https://mariadb.com/kb/en/library/systemd/", " Process: 12935 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)", " Process: 12957 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)", " Process: 12992 ExecStart=/usr/libexec/mariadbd --basedir=/usr $MYSQLD_OPTS $_WSREP_NEW_CLUSTER (code=exited, status=0/SUCCESS)", " Process: 13004 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)", " Main PID: 12992 (code=exited, status=0/SUCCESS)", " Status: "MariaDB server is down"", " CPU: 526ms", "", "Nov 20 23:26:08 RHEL-Node-2 systemd[1]: Starting MariaDB 10.5 database server...", "Nov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.", "Nov 20 23:26:08 RHEL-Node-2 mariadb-prepare-db-dir[12957]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.", "Nov 20 23:26:08 RHEL-Node-2 systemd[1]: Started MariaDB 10.5 database server.", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopping MariaDB 10.5 database server...", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: mariadb.service: Deactivated successfully.", "Nov 20 23:30:01 RHEL-Node-2 systemd[1]: Stopped MariaDB 10.5 database server."]} ...ignoring
If the task result is fatal the handler is not notified. Instead of
ignore_errors: true
set
failed_when: service_status.rc > 3
This solves the problem of notifying a handler by failed command (rc=3 in your case). However, it doesn't necessarily solve the problem "to start the service only if it's in stopped status."
Example of a complete playbook to test the handler
- hosts: localhost
tasks:
- command: 'false'
register: status
notify: Mariadb restart
changed_when: status.rc != 0
failed_when: status.rc > 3
handlers:
- name: Mariadb restart
debug:
msg: Restart Mariadb