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: