Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Created February 5, 2023 20:38
Show Gist options
  • Save pwalkr/bf3d96de629337afbb333a0e1fd0b800 to your computer and use it in GitHub Desktop.
Save pwalkr/bf3d96de629337afbb333a0e1fd0b800 to your computer and use it in GitHub Desktop.
Automatic rotation of a docker swarm config using an ansible template
# This set of tasks rotates a swarm config by generating a config name.
# If input contents change, the hash changes, triggering a name change and restart of consuming service(s)
# Tasks should all `run_once` if multiple swarm managers are in play.
- hosts: swarm_manager
tasks:
- set_fact:
config_data: '{{ lookup("template", "templates/my-template.cfg") }}'
- name: create config
community.docker.docker_config:
name: 'my-service-{{ config_data | hash("md5") }}'
data: '{{ config_data }}'
register: myconfig
notify: 'prune swarm configs'
- community.docker.docker_stack:
name: mystack
compose:
- version: '3.3'
services:
myservice:
image: 'myimage'
configs:
- '{{ myconfig.config_name }}'
configs: '{{ { myconfig.config_name:{"external":true} } }}'
handlers:
- name: prune swarm configs
ansible.builtin.shell: |
{% raw %}
for config in $(docker config ls --format="{{ .ID }}"); do
docker config rm $config || true
done
{% endraw %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment