community.docker
community.docker copied to clipboard
Support config rotate for docker_config
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
- Feature Idea
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
One option would be to enahance docker_config with an option that allows it to:
- create a new config using the name as prefix + file checksum ( first 6-10 characters of checksum should be enough IMO)
The new option for docker_config would be
generate_name
?! In the example case above this would bekibana_config_a3d12341
, This new name can be registered as an ansible variable to be used in the stack deployment.
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.
A somewhat related issue is #21.
I have found a workaround that seems to work ok for now:
- Use set_fact in ansible to get the contents and compute the file hash
- Use the hash to version the docker_config
- Use that in the service
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
@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.