ansible-collections / community.docker

Community Docker Collection for Ansible: modules and plugins for working with Docker
https://galaxy.ansible.com/ui/repo/published/community/docker/
GNU General Public License v3.0
203 stars 121 forks source link

Support config rotate for docker_config #109

Open ieugen opened 3 years ago

ieugen commented 3 years ago
SUMMARY

I would like to be able to change a configuration of a docker service/stack that I deploy via ansible.

The functionality is described here https://docs.docker.com/engine/swarm/configs/#example-rotate-a-config .

Right now the docker_config fails to update since a service is using it. Docker supports create + rm, not update. Hence,

ISSUE TYPE
COMPONENT NAME

docker_config

ADDITIONAL INFORMATION

Using a playbook like bellow, if I update the config content and run the playbook, it will fail because it tries to update the config (by removing and adding it again). Removing fails because the configuration is used by the service.


- name: Configuration for kibana
  community.docker.docker_config:
    name: kibana_config
    state: present
    data: "{{ lookup('file', 'files/monitoring_elk/kibana.yml') }}"

- name: monitoring_elk
  community.docker.docker_stack:
    state: present
    name: monitoring_elk
    prune: yes
    compose:
      - version: "3.8"
        services:
          elasticsearch:
          kibana:
            image: "docker.elastic.co/kibana/kibana:7.12.0"
            #REDACTED
            configs:
              - source: kibana_config
                target: /usr/share/kibana/config/kibana.yml
ieugen commented 3 years ago

One option would be to enahance docker_config with an option that allows it to:

docker_config could also have a prune option to remove older configs. The prune option could be an integer to limit the number of configs kept in history? If prune is 5 it should keep max 5 versions. If prune is 0 it should keep only current version.

This would make stack deployments idempotent IMO.

felixfontein commented 3 years ago

A somewhat related issue is #21.

ieugen commented 3 years ago

I have found a workaround that seems to work ok for now:

Does not work with docker_swarm_stack since it can't accept jinja interpolation in config name.

- name: Configuration contents for kibana config
  set_fact:
    kibana_config_contents: "{{ lookup('file', 'files/monitoring_elk/kibana.yml') }}"

- name: Configuration version for kibana
  set_fact:
    kibana_config_version: "{{ kibana_config_contents | checksum | truncate(6,True,'') }}"

- community.docker.docker_config:
    name: "kibana_config_{{ kibana_config_version }}"
    state: present
    data: "{{ kibana_config_contents }}"

- name: ELK kibana
  community.docker.docker_swarm_service:
   # REDACTED
    configs:
      - config_name: "kibana_config_{{ kibana_config_version }}"
        filename: /usr/share/kibana/config/kibana.yml
imartinezortiz commented 2 years ago

@ieugen just checked that after #295 it is possible to rotate a config in a docker swarm stack. This playbook it is a basic playbook (nginx + php-fpm) that I've used to test the approach. Just change the PHP DEBUG constant in the app_config and run the playbook.