docker-compose/tasks/main.yml
Knut Ahlers ba38259775
Fix: replace deprecated include builtin
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-03-12 00:33:50 +01:00

201 lines
5.6 KiB
YAML

---
# Install docker-compose
- name: "Loading distribution specific instructions for {{ ansible_distribution }}"
include_tasks: "{{ ansible_distribution }}.yml"
when: not docker_compose_use_docker_image
- name: Install shell wrapper for docker-compose image
include_tasks: "docker-wrapper.yml"
when: docker_compose_use_docker_image
# Configure docker-compose
- name: Create configuration directory
file:
dest: /etc/docker-compose
state: directory
mode: 0700
- name: Write docker-compose.yml
template:
dest: /etc/docker-compose/docker-compose.yml
src: templates/docker-compose.yml
mode: 0600
register: compose_file
- name: Validate configuration
command: docker-compose config -q
args:
chdir: /etc/docker-compose
when: compose_file.changed and docker_compose_validate and packer_build_name is not defined
# Install and start services
- name: Install docker-compose.service
copy:
dest: /etc/systemd/system/docker-compose.service
content: |
[Unit]
Description=Docker Compose container starter
After=docker.service network-online.target
Requires=docker.service network-online.target
[Service]
WorkingDirectory=/etc/docker-compose
Type=oneshot
RemainAfterExit=yes
ExecStartPre=-{{ docker_compose_binary_path }} pull --quiet --ignore-pull-failures
ExecStart={{ docker_compose_binary_path }} up -d --remove-orphans
ExecStop={{ docker_compose_binary_path }} {{ docker_compose_stop_action }}
ExecReload={{ docker_compose_binary_path }} pull --quiet --ignore-pull-failures
ExecReload={{ docker_compose_binary_path }} up -d --remove-orphans
[Install]
WantedBy=docker.service
register: docker_compose_service
- name: Enable docker-compose service
systemd:
name: docker-compose.service
daemon_reload: true
enabled: true
- name: Restart service if service configuration changed
systemd:
name: docker-compose.service
daemon_reload: true
state: restarted
when: docker_compose_service.changed and packer_build_name is not defined
- name: Reload and apply configuration
systemd:
name: docker-compose.service
state: reloaded
when: (docker_compose.changed or compose_file.changed) and packer_build_name is not defined
- name: Install docker-compose-reload.service
copy:
dest: /etc/systemd/system/docker-compose-reload.service
content: |
[Unit]
Description=Refresh images and update containers
[Service]
Type=oneshot
ExecStart=/bin/systemctl reload-or-restart docker-compose.service
- name: Install docker-compose-reload.timer
copy:
dest: /etc/systemd/system/docker-compose-reload.timer
content: |
[Unit]
Description=Refresh images and update containers
After=docker-compose.service
[Timer]
OnCalendar=*:0/{{ docker_compose_reload_minutes }}
RandomizedDelaySec={{ docker_compose_reload_max_delay }}
[Install]
WantedBy=timers.target
register: docker_compose_reload_timer
- name: Enable docker-compose-reload.timer
systemd:
name: docker-compose-reload.timer
daemon_reload: true
enabled: true
when: docker_compose_reload_enabled
- name: Restart docker-compose-reload.timer if it changed
systemd:
name: docker-compose-reload.timer
daemon_reload: true
state: restarted
when: docker_compose_reload_enabled and docker_compose_reload_timer.changed and packer_build_name is not defined
- name: Install docker-clean script
copy:
content: |
#!/bin/bash
set -euo pipefail
function step() { echo $@; }
step "Removing containers created / exited >= ~1h ago..."
CONTAINERS=$(docker ps -a | gawk '/(hours?|days?|weeks?|months?) ago\s+(Created|Exited)/{ print $1 }' | xargs)
[ -n "${CONTAINERS}" ] && docker rm ${CONTAINERS}
step "Removing unused and dangling images..."
docker image prune -a -f
dest: /usr/local/bin/docker-clean
mode: '0755'
owner: 'root'
group: 'root'
when: docker_compose_add_cleanjob
- name: Install docker-clean.service
copy:
dest: /etc/systemd/system/docker-clean.service
content: |
[Unit]
Description=Clean out old containers and unused images
[Service]
Type=oneshot
ExecStart=/usr/local/bin/docker-clean
when: docker_compose_add_cleanjob
- name: Install docker-clean.timer
copy:
dest: /etc/systemd/system/docker-clean.timer
content: |
[Unit]
Description=Clean out old containers and unused images
[Timer]
OnCalendar=*:0
[Install]
WantedBy=timers.target
register: docker_compose_clean_timer
when: docker_compose_add_cleanjob
- name: Enable docker-clean timer
systemd:
name: docker-clean.timer
daemon_reload: true
enabled: true
when: docker_compose_add_cleanjob
- name: Restart docker-clean.timer if it changed
systemd:
name: docker-clean.timer
daemon_reload: true
state: restarted
when: docker_compose_clean_timer is defined and docker_compose_clean_timer.changed and packer_build_name is not defined and docker_compose_add_cleanjob
- name: Deactivate docker-clean timer
systemd:
name: docker-clean.timer
daemon_reload: true
enabled: false
state: stopped
ignore_errors: true # Might fail when the job is not present
when: not docker_compose_add_cleanjob
- name: Uninstall docker-clean components
file:
dest: '{{ item }}'
state: absent
with_items:
- /usr/local/bin/docker-clean
- /etc/systemd/system/docker-clean.service
- /etc/systemd/system/docker-clean.timer
when: not docker_compose_add_cleanjob