79211228

Date: 2024-11-21 12:54:42
Score: 2
Natty:
Report link

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:

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: akire0ne