admin管理员组文章数量:1122832
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.builtinmand: 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 /\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)", " /", " 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 /\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)", " /", " 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
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.
Share Improve this question edited Nov 22, 2024 at 1:12 Vladimir Botka 67.6k7 gold badges44 silver badges76 bronze badges asked Nov 21, 2024 at 8:50 Linux4uLinux4u 131 silver badge3 bronze badges 1 |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
2 Answers
Reset to default 1If 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
Do not use ansible.builtin.command
for this purpose. This is expected to fail as your command is returning rc != 0
so the module perceive this as a failure.
There is other modules that can help you with this such as ansible.builtin.systemd
See example here
- name: Get Service Status
ansible.builtin.systemd:
name: mariadb
register: result
- debug:
var: result.status.ActiveState
- name: Mariadb start if in inactive state
ansible.builtin.service:
name: mariadb
state: started
when: result.status.ActiveState == 'inactive'
Also one note on your usage of changed_when
and handlers
. These should be used when your task is making actual changes on the system. Here you're just looking for the state of something, so you're not making changes. As you can see above you just need to useregister
to save the result of your task and use an appropriate when
statement to apply the intended logic.
See links for more info:
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_service_module.html
- Ansible: How to get service status by Ansible?
本文标签: Started a service in stopped state using AnsibleStack Overflow
版权声明:本文标题:Started a service in stopped state using Ansible - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312325a1935060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ignore_errors: true
. – Vladimir Botka Commented Nov 22, 2024 at 1:16