Automating common tasks can save a lot of time, increase the reliability of a
system, and reduce maintenance cost. One such task to automate is to
make services reload its configuration files by sending the
So far so good. It gets more
difficult to auto this with Ansible when the service runs inside a container.
Here are two options.
Run kill inside container
If the image contains the
kill command, you can run it inside the container.
So the Ansible+Docker equivalent of
kill -s HUP $PID looks as follows
assuming you want to send the signal to the main process.
- name: Use kill inside the container community.docker.docker_container_exec: container: mycontainer command: kill -s HUP 1
Send signal from outside
If the image does not contain the
kill command, you can send a signal from outside.
So the the Ansible equivalent of
docker kill -s HUP $PID looks as follows.
- name: Use docker kill module community.docker.docker_container: name: mycontainer state: stopped # Send signal but don't remove container force_kill: true # Use kill signal instead of stop kill_signal: HUP # Send HUP instead of kill
There is no need to resort to
ansible.builtin.shell or similar.
This might also interest you