0

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?

U880D
  • 8,601
  • 6
  • 24
  • 40
puravidaso
  • 1,013
  • 1
  • 5
  • 22
  • I looks more to me like an issue with the unit of service of this application. And the systemd unit configuration is not really on topic here. It might be better suited on other Q&A, like server fault, but, not please, read their on topic help page first. – β.εηοιτ.βε Dec 17 '22 at 18:19
  • I agree it is a problem with the service ("kennel" in this case). It, however, presents an issue how to handle it in ansible with imperfect service. It (process does not stop) happens occasionally, but the result is catastrophic when this happens. – puravidaso Dec 17 '22 at 18:47
  • in the title, was it intended to say `ansible` instead of `sensible` ? – Carlos Monroy Nieblas Dec 18 '22 at 02:54
  • Yes, I think the app made an auto correction, but I so nor think I can edit it now. – puravidaso Dec 18 '22 at 04:55

1 Answers1

2

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
Carlos Monroy Nieblas
  • 2,225
  • 2
  • 16
  • 27
  • Does the "state stop" ensure all the process being stopped, and if not, it will kill? I feel most of time shell is most directly, and sometimes it is even easier as in this example. – puravidaso Dec 18 '22 at 04:59
  • It could be caused by [What does status `active (exited)` mean for a `systemd` service?](https://unix.stackexchange.com/questions/241970/) and can be observed also under other circumstances [`nfs-server` state in `service_facts` differ from `ansible.builtin.systemd`](https://stackoverflow.com/a/73958274/6771046). – U880D Dec 18 '22 at 10:10
  • > Does the "state stop" ensure all the process being stopped, and if not, it will kill? Yes, Ansible will do its best to stop the service and kill the process; in addition, if the service is already stopped, it will acknowledge it and go to the next task, for this the task will be reported as `OK` – Carlos Monroy Nieblas Dec 18 '22 at 16:32