How to reload config with ansible docker_container module?
Asked Answered
T

2

7

I am trying to accomplish docker kill -s HUP <container> in Ansible but it looks like the options I try always restart the container or attempt to instead of reloading the config.

Running the following command allows me to reload the configuration without restarting the container:

docker kill -s HUP <container>

The Ansible docker_container docs suggest the following options:

force_kill Use the kill command when stopping a running container.

kill_signal Override default signal used to kill a running container.

Using the kill_signal in isolation did nothing.

Below is an example of what I hoped would work:

- name: Reload haproxy config
  docker_container:
    name: '{{ haproxy_docker_name }}'
    state: stopped
    image: '{{ haproxy_docker_image }}'
    force_kill: True
    kill_signal: HUP

I assumed overriding force_kill and kill_signal would give me the desired behaviour. I have also tried setting state to 'started' and present.

What is the correct way to do this?

Tailback answered 27/10, 2017 at 4:53 Comment(4)
So you specified state: stopped, but Ansible "restarts the container or attempts to"? Or what is this question about?Unexceptionable
My configuration was just one example of what I have tried.Tailback
How does your comment explain anything?Unexceptionable
@Tailback your task works fine. Does the same as 'docker kill -s HUP <container>' command.Igenia
A
2

I needed to do the same with an haproxy docker instance to reload the configuration. The following worked in ansible 2.11.2:

handlers:
  - name: Restart HAProxy
    docker_container:
      name: haproxy
      state: stopped
      force_kill: True
      kill_signal: HUP
Arvid answered 1/2, 2022 at 15:55 Comment(0)
P
0

I went with a simple shell command, which runs whenever the docker-compose file has my service:

---
- hosts: pis
  remote_user: pi
  tasks:
    - name: Get latest docker images
      docker_compose:
        project_src: dc
        remove_orphans: true
        pull: true
      register: docker_compose_output
    - name: reload prometheus
      command: docker kill --signal=HUP dc_prometheus_1
      when: '"prometheus" in ansible_facts'
    - name: reload blackbox
      command: docker kill --signal=HUP dc_blackbox_1
      when: '"blackbox" in ansible_facts'

Appendix

I found some examples using GitHub advanced search, but they didn't work for me:

https://github.com/search?q=kill_signal%3A+HUP+docker+extension%3Ayml&type=Code

An example:

- name: Reload HAProxy
  docker_container:
    name: "haproxy"
    state: started
    force_kill: true
    kill_signal: HUP
Prairial answered 26/2, 2020 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.