Search code examples
ubuntuserviceansible

Ansible: service module does not stop a process sometimes


I originally had:

service:
  name: kannel
  state: restarted

but sometimes, for a unknown reason, it does not restart one of the processes in the service "smsbox", so I have to use shell module:

- name: restart kannel
  shell: |
    service kannel stop
    pkill -9 smsbox
    service kannel start

Is this the best that I can do? Is there a way to ensure all processes stopped and restarted with the service module of Ansible or the service command?


Solution

  • We have seen a similar behaviour when systemd reports the service Active and exited at the same time

    service <service name> status
      ...
      Active: active (exited)
      ...
    

    If the restart was done in the playbook or a role, you can set the actions of the restart in two steps

    - name: Stop the service
      ansible.builtin.service:
        name: kannel
        state: stopped
    
    - name: Start the service
      ansible.builtin.service:
        name: kannel
        state: started
    
    

    If this action was executed as a handler, the first task will need to notify the second task:

    handlers:
      - name: Restart service
        ansible.builtin.service:
          name: kannel
          state: stopped
        notify:
          - Start service
    
      - name: Start service
        ansible.builtin.service:
          name: kannel
          state: started