feat(docker): add the docker role #72

Merged
Defame1297 merged 3 commits from feat/522-docker-role into main 2026-11-08 19:20:10 +00:00
18 changed files with 775 additions and 0 deletions

No files matched your search

+6
View File
@@ -8,3 +8,9 @@ exclude_paths:
- .venv
- changelogs
- images
# The Collection is checked out outside an ansible_collections/mortis/kamino path (CI clones it
# into the workspace), so ansible-lint cannot resolve a role of this Collection by its FQCN in
# a scenario's converge play. List the roles the scenarios call by FQCN.
mock_roles:
- mortis.kamino.docker
+2
View File
@@ -0,0 +1,2 @@
minor_changes:
- docker - New role. Installs, updates or removes Docker Engine from the upstream APT repository and adds users to the docker group.
View File
Whitespace-only changes.
+72
View File
@@ -0,0 +1,72 @@
# docker
Installs Docker Engine from the upstream Docker APT repository, optionally updates it, or removes it, and adds listed users to the `docker` group. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
## Supported platforms
Ubuntu. The key and repository URLs point at `download.docker.com/linux/ubuntu`, and the source line uses `ansible_distribution_release`. The default architecture is `amd64`. A limited molecule scenario covers the role's own logic (see [Tests](#tests)); the real Docker Engine is not tested.
## Requirements and run order
- Needs gathered facts and privilege escalation.
- Use it by its fully qualified name, `mortis.kamino.docker`.
- Run it before any role that needs the Docker group. Roles that install later and create their own user must add that user to the group themselves (see `docker_users`).
- The `ufw` role is not a dependency and is not called: Docker publishes ports through iptables and owns no UFW rule.
## Role variables
| Variable | Default | Required | Description |
|---|---|---|---|
| `docker_remove` | `false` | no | Remove Docker instead of installing it. |
| `docker_remove_data` | `false` | no | With `docker_remove`, purge packages and delete `/var/lib/docker`. |
| `docker_update` | `false` | no | Upgrade packages to their current candidate version. |
| `docker_packages` | `docker-ce`, `docker-ce-cli`, `containerd.io` | no | Packages managed. |
| `docker_keyring_path` | `/etc/apt/keyrings/docker.asc` | no | Repository key location. |
| `docker_apt_list_path` | `/etc/apt/sources.list.d/docker.list` | no | APT source file. |
| `docker_gpg_url` | `https://download.docker.com/linux/ubuntu/gpg` | no | Key URL. |
| `docker_repo_base_url` | `https://download.docker.com/linux/ubuntu` | no | Repository base URL. |
| `docker_apt_arch` | `amd64` | no | Architecture in the APT source. |
| `docker_service` | `docker` | no | systemd unit name. |
| `docker_group` | `docker` | no | Group for `docker_users`. |
| `docker_users` | `[]` | no | Users added to the group. |
## Tags
`docker` (everything), `install`, `update`, `remove`, `configure` (group membership). The tasks in `remove.yml` carry `docker` and `remove`.
Update (`docker_update: true`) and removal (`docker_remove: true`) are opt-in: run them on a host that is safe to change.
## Example play
```yaml
- hosts: standalone_vms
roles:
- role: mortis.kamino.docker
vars:
docker_users: [deploy]
```
## Tests
`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), in the `molecule-ubuntu2404` test container. A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url` and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does.
What it shows, with the role's tasks running unchanged:
- The key is fetched, stored at `docker_keyring_path` with mode `0644`, and apt really verifies the repository against it (an unsigned or wrongly signed repository would leave no install candidate).
- The source line has the architecture, the `signed-by` keyring and `ansible_distribution_release`.
- The packages are installed from that repository, the unit is enabled and active, and `docker_users` join `docker_group`.
- `docker_remove` with `docker_remove_data` removes the source line, the keyring, the packages, the unit and `/var/lib/docker`.
- The argument check refuses a mapping for `docker_users` and a non-boolean `docker_remove`.
- A second run changes nothing (molecule's idempotence check).
What it does not show: that the upstream repository and key at `download.docker.com` are reachable and valid, that the real packages install, that `dockerd` starts, anything about container networking or iptables, the `docker_update` path (`tasks/update.yml`), and removal without `docker_remove_data` (the removal stops the unit first, so it can run only once per install). It needs the Ubuntu mirrors, for `gnupg` in `prepare.yml` and the `curl` the role installs. These gaps are a known residual risk of the role.
## Notes
- `docker_remove` is checked first: when true, only `remove.yml` runs.
- Removal with `docker_remove_data` is destructive. It deletes `/var/lib/docker`.
- The update task resolves each package's candidate with `apt-cache policy` and fails if a package has none. It installs `package=version`, never `latest`.
- Installing or updating the packages notifies `Restart docker`. The `meta: flush_handlers` at the end of `tasks/main.yml` is untagged, so it runs only without a `--tags` selection; under `--tags update` (or `install`) it is skipped and the handler runs at the end of the play instead.
- Do not combine with anything that installs the distro `docker.io` package on the same host: this role installs `docker-ce` from Docker's repository, and the two packages conflict.
- Docker bypasses UFW for published ports (`-p`), so UFW rules do not restrict container ports.
- `no_log` is not used: the role handles no secrets.
+31
View File
@@ -0,0 +1,31 @@
---
docker_update: false
docker_remove_data: false
docker_remove: false
docker_packages:
- docker-ce
- docker-ce-cli
- containerd.io
docker_keyring_path: /etc/apt/keyrings/docker.asc
docker_apt_list_path: /etc/apt/sources.list.d/docker.list
docker_gpg_url: https://download.docker.com/linux/ubuntu/gpg
docker_repo_base_url: https://download.docker.com/linux/ubuntu
# Override per host if a host is not amd64.
docker_apt_arch: amd64
docker_service: docker
docker_group: docker
# Usernames to add to the `docker` group at install time. Left empty by
# default: a role that needs Docker access but installs *after* this one in
# a host's `services:` list won't have created its run-as user yet when this
# role runs, so it must add itself to the group in its own tasks instead.
# This list only helps a role/user that already exists before `docker` runs.
docker_users: []
# Docker manipulates iptables directly for any published container port
# (`-p`), bypassing UFW rules entirely - so the role owns no UFW rule and does
# not call the ufw role.
+6
View File
@@ -0,0 +1,6 @@
---
- name: Restart docker
become: true
ansible.builtin.systemd:
name: "{{ docker_service }}"
state: restarted
+79
View File
@@ -0,0 +1,79 @@
---
argument_specs:
main:
short_description: Install, update or remove Docker Engine
description:
- >-
Installs Docker Engine from the upstream APT repository, or removes it when C(docker_remove) is true. Optionally updates the packages to
the current candidate versions, then adds C(docker_users) to C(docker_group).
- Needs gathered facts, because the APT source uses C(ansible_distribution_release).
options:
docker_remove:
type: bool
default: false
description:
- >-
Removes Docker instead of installing it. Stops the service, removes the packages, the APT source and the keyring,
and skips the install, update and group tasks.
docker_remove_data:
type: bool
default: false
description:
- >-
Only used with C(docker_remove). Purges the packages and deletes C(/var/lib/docker), which destroys images, containers and volumes.
docker_update:
type: bool
default: false
description:
- >-
Upgrades each package in C(docker_packages) to its current APT candidate version, pinned as package=version.
The C(Restart docker) handler is notified only when a package actually changes.
docker_packages:
type: list
elements: str
default: [docker-ce, docker-ce-cli, containerd.io]
description:
- Packages installed, updated or removed.
docker_keyring_path:
type: path
default: /etc/apt/keyrings/docker.asc
description:
- Where the Docker repository key is stored.
docker_apt_list_path:
type: path
default: /etc/apt/sources.list.d/docker.list
description:
- APT source file written for the Docker repository.
docker_gpg_url:
type: str
default: https://download.docker.com/linux/ubuntu/gpg
description:
- URL of the repository signing key.
docker_repo_base_url:
type: str
default: https://download.docker.com/linux/ubuntu
description:
- Base URL of the repository. The source line uses C(ansible_distribution_release) and the C(stable) component.
docker_apt_arch:
type: str
default: amd64
description:
- Architecture written into the APT source. Override per host if a host is not amd64.
docker_service:
type: str
default: docker
description:
- Name of the systemd unit that is enabled, started, restarted and stopped.
docker_group:
type: str
default: docker
description:
- Group that C(docker_users) are added to.
docker_users:
type: list
elements: str
default: []
description:
- >-
Existing users added to C(docker_group) with append. Empty by default, because a role that installs after docker creates its
run-as user too late and must join the group itself.
+17
View File
@@ -0,0 +1,17 @@
---
galaxy_info:
role_name: docker
namespace: mortis
author: Defame1297
license: GPL-3.0-or-later
description: Install, update or remove Docker Engine from the upstream Docker APT repository and add users to the docker group.
min_ansible_version: "2.19"
platforms:
- name: Ubuntu
versions:
- all
galaxy_tags:
- docker
- containers
- apt
dependencies: []
@@ -0,0 +1,61 @@
---
# The role runs unchanged against a signed local repository of stand-in packages (see
# prepare.yml), so the key fetch, the source line, the package install, the unit, the group
# membership and the handler are the real tasks. Only the packages are stand-ins: no Docker
# Engine is installed, and nothing here shows that dockerd works.
- name: Converge the role against the stand-in repository
hosts: all
vars:
docker_gpg_url: file:///srv/docker-stub-repo/docker.asc
docker_repo_base_url: file:///srv/docker-stub-repo
docker_users: [moleculeuser]
roles:
- role: mortis.kamino.docker
# The plays below are refusals: argument_specs rejects the wrong types before any task runs,
# so the idempotence run stays clean. A sentinel in the failure message tells "the role
# refused" from "the fail task after the role ran", which a bare rescue cannot.
- name: A docker_users value that is not a list is refused
hosts: all
gather_facts: false
vars:
docker_users:
not: a-list
tasks:
- name: Apply the role with a mapping for docker_users
block:
- name: Include the role
ansible.builtin.include_role:
name: mortis.kamino.docker
- name: Fail when the wrong type was accepted
ansible.builtin.fail:
msg: MOLECULE-ACCEPTED
rescue:
- name: The argument check stopped the play
ansible.builtin.assert:
that:
- "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')"
- "'docker_users' in ansible_failed_result.msg | default('')"
quiet: true
- name: A docker_remove value that is not a boolean is refused
hosts: all
gather_facts: false
vars:
docker_remove: maybe
tasks:
- name: Apply the role with a non-boolean docker_remove
block:
- name: Include the role
ansible.builtin.include_role:
name: mortis.kamino.docker
- name: Fail when the wrong type was accepted
ansible.builtin.fail:
msg: MOLECULE-ACCEPTED
rescue:
- name: The argument check stopped the play
ansible.builtin.assert:
that:
- "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')"
- "'docker_remove' in ansible_failed_result.msg | default('')"
quiet: true
+101
View File
@@ -0,0 +1,101 @@
#!/bin/bash
# Builds the signed APT repository the docker scenario installs from, so the role's own key,
# source-line and package tasks run for real without the real Docker Engine.
#
# build-stub-repo.sh <repo-root> <codename> <work-dir>
#
# The repository holds empty stand-ins for docker-ce, docker-ce-cli and containerd.io (the
# default docker_packages). docker-ce ships a stub docker.service and a postinst that creates
# the docker group, as the real package does. The Release file is signed with a throwaway key
# generated here; the matching public key is exported to <repo-root>/docker.asc, which is what
# the role fetches as its keyring. The secret key stays in <work-dir> and is never installed.
set -euo pipefail
repo=${1:?usage: build-stub-repo.sh <repo-root> <codename> <work-dir>}
codename=${2:?usage: build-stub-repo.sh <repo-root> <codename> <work-dir>}
work=${3:?usage: build-stub-repo.sh <repo-root> <codename> <work-dir>}
version=0.0.1~molecule
rm -rf "$repo" "$work"
mkdir -p "$repo/pool" "$repo/dists/$codename/stable/binary-amd64" "$work/gnupg" "$work/pkg"
chmod 0700 "$work/gnupg"
build_deb() {
local name=$1 tree="$work/pkg/$1"
mkdir -p "$tree/DEBIAN"
cat > "$tree/DEBIAN/control" <<CONTROL
Package: $name
Version: $version
Architecture: all
Maintainer: Molecule <molecule@invalid>
Description: stand-in for $name in the docker role's molecule scenario
Empty package. It installs nothing from Docker.
CONTROL
if [ "$name" = docker-ce ]; then
mkdir -p "$tree/usr/lib/systemd/system"
cat > "$tree/usr/lib/systemd/system/docker.service" <<'UNIT'
[Unit]
Description=Stand-in for the Docker Engine (molecule)
[Service]
ExecStart=/bin/sleep infinity
[Install]
WantedBy=multi-user.target
UNIT
cat > "$tree/DEBIAN/postinst" <<'POSTINST'
#!/bin/sh
set -e
if [ "$1" = configure ]; then
getent group docker > /dev/null || groupadd --system docker
systemctl daemon-reload || true
fi
POSTINST
chmod 0755 "$tree/DEBIAN/postinst"
fi
dpkg-deb --root-owner-group --build "$tree" "$repo/pool/${name}_${version}_all.deb" > /dev/null
}
for name in docker-ce docker-ce-cli containerd.io; do
build_deb "$name"
done
index="$repo/dists/$codename/stable/binary-amd64/Packages"
: > "$index"
for deb in "$repo"/pool/*.deb; do
{
dpkg-deb --field "$deb"
echo "Filename: pool/$(basename "$deb")"
echo "Size: $(stat --format %s "$deb")"
echo "SHA256: $(sha256sum "$deb" | cut -d' ' -f1)"
echo
} >> "$index"
done
release="$repo/dists/$codename/Release"
{
echo "Origin: molecule-stub"
echo "Label: molecule-stub"
echo "Suite: $codename"
echo "Codename: $codename"
echo "Date: $(LC_ALL=C date -u '+%a, %d %b %Y %H:%M:%S UTC')"
echo "Architectures: amd64"
echo "Components: stable"
echo "SHA256:"
echo " $(sha256sum "$index" | cut -d' ' -f1) $(stat --format %s "$index") stable/binary-amd64/Packages"
} > "$release"
export GNUPGHOME="$work/gnupg"
gpg --batch --quiet --pinentry-mode loopback --passphrase '' \
--quick-generate-key 'molecule stub repository <molecule@invalid>' rsa2048 sign never
gpg --batch --quiet --yes --armor --export > "$repo/docker.asc"
gpg --batch --quiet --yes --pinentry-mode loopback --passphrase '' --clearsign \
--output "$repo/dists/$codename/InRelease" "$release"
gpg --batch --quiet --yes --pinentry-mode loopback --passphrase '' --armor --detach-sign \
--output "$repo/dists/$codename/Release.gpg" "$release"
# The _apt user reads the repository during apt update.
chmod -R a+rX "$repo"
# Stop the gpg-agent that gpg started, so it does not outlive the script.
gpgconf --kill gpg-agent || true
@@ -0,0 +1,28 @@
---
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
# writable cgroup, nothing else. NET_ADMIN is kept so the platform matches the other scenarios;
# this role does not need it, because the Docker Engine is a stand-in package and no daemon,
# bridge or iptables rule is created.
driver:
name: docker
platforms:
- name: docker-instance
image: ${MOLECULE_IMAGE}
pre_build_image: true
pull: true
command: /sbin/init
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
capabilities:
- NET_ADMIN
security_opts:
- apparmor=unconfined
provisioner:
name: ansible
verifier:
name: ansible
+55
View File
@@ -0,0 +1,55 @@
---
# The real Docker Engine cannot run in an unprivileged test container (dockerd needs the
# bridge, iptables and a delegated cgroup), so the role installs stand-in packages from a
# local APT repository. The repository is built and signed here, with a throwaway key, so the
# role's own tasks run unchanged: fetch the key, write the source line, install, enable and
# start the unit, join the group. Like the pihole and system_baseline scenarios, this needs
# the Ubuntu mirrors for gnupg and for the curl the role installs.
- name: Prepare
hosts: all
vars:
docker_molecule_repo: /srv/docker-stub-repo
docker_molecule_work: /var/tmp/docker-stub-work
tasks:
- name: Install gnupg, which signs the stand-in repository
ansible.builtin.apt:
name: gnupg
state: present
update_cache: true
- name: Copy the repository builder
ansible.builtin.copy:
src: build-stub-repo.sh
dest: /var/tmp/build-stub-repo.sh
owner: root
group: root
mode: "0755"
- name: Build and sign the stand-in repository
ansible.builtin.command:
argv:
- /var/tmp/build-stub-repo.sh
- "{{ docker_molecule_repo }}"
- "{{ ansible_facts['distribution_release'] }}"
- "{{ docker_molecule_work }}"
creates: "{{ docker_molecule_repo }}/docker.asc"
- name: Remove the signing key and the builder
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- "{{ docker_molecule_work }}"
- /var/tmp/build-stub-repo.sh
- name: Create a user for the docker group
ansible.builtin.user:
name: moleculeuser
shell: /usr/sbin/nologin
create_home: false
- name: Seed a Docker data directory for the removal test
ansible.builtin.file:
path: /var/lib/docker/molecule-marker
state: directory
mode: "0755"
+134
View File
@@ -0,0 +1,134 @@
---
# What the role is for, as far as stand-in packages can show it: the repository key and the
# source line are in place and apt really used them, the packages are installed from that
# repository, the unit is enabled and running, and the user joined the group. The second play
# runs the removal, which would break the checks of the first, so it comes last.
- name: Verify the install
hosts: all
vars:
docker_verify_packages: [docker-ce, docker-ce-cli, containerd.io]
tasks:
- name: Read the source list and the two keys
ansible.builtin.slurp:
src: "{{ item }}"
loop:
- /etc/apt/sources.list.d/docker.list
- /etc/apt/keyrings/docker.asc
- /srv/docker-stub-repo/docker.asc
register: docker_verify_files
- name: The source line carries the arch, the keyring and the release
ansible.builtin.assert:
that:
- >-
(docker_verify_files.results[0].content | b64decode) ==
'deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] file:///srv/docker-stub-repo '
~ ansible_facts['distribution_release'] ~ ' stable\n'
quiet: true
- name: The keyring is the key the repository published
ansible.builtin.assert:
that:
- docker_verify_files.results[1].content == docker_verify_files.results[2].content
quiet: true
- name: Stat the keyring
ansible.builtin.stat:
path: /etc/apt/keyrings/docker.asc
register: docker_verify_keyring
- name: The keyring is world-readable, as apt needs
ansible.builtin.assert:
that: docker_verify_keyring.stat.mode == "0644"
quiet: true
- name: Ask apt where docker-ce comes from
ansible.builtin.command: apt-cache policy docker-ce
register: docker_verify_policy
changed_when: false
- name: Apt installed docker-ce from the signed stand-in repository
ansible.builtin.assert:
that:
- "'Installed: 0.0.1~molecule' in docker_verify_policy.stdout"
- "'file:/srv/docker-stub-repo' in docker_verify_policy.stdout"
quiet: true
- name: Ask dpkg for the package states
ansible.builtin.command:
argv: [dpkg-query, --show, --showformat, "${db:Status-Status}\\n", "{{ item }}"]
loop: "{{ docker_verify_packages }}"
register: docker_verify_dpkg
changed_when: false
- name: Every package is installed
ansible.builtin.assert:
that: docker_verify_dpkg.results | map(attribute='stdout') | map('trim') | unique | list == ['installed']
quiet: true
- name: Ask systemd about the unit
ansible.builtin.systemd:
name: docker.service
register: docker_verify_unit
check_mode: true
- name: The unit is enabled and running
ansible.builtin.assert:
that:
- docker_verify_unit.status.UnitFileState == "enabled"
- docker_verify_unit.status.ActiveState == "active"
quiet: true
- name: Ask which groups the user is in
ansible.builtin.command: id -nG moleculeuser
register: docker_verify_groups
changed_when: false
- name: The user joined the docker group
ansible.builtin.assert:
that: "'docker' in docker_verify_groups.stdout.split()"
quiet: true
# docker_remove_data is set, so this also covers the destructive path: the packages are
# purged and /var/lib/docker, seeded in prepare.yml, is deleted. The removal stops the unit
# before the package takes it away, so it can only run once; a second run would fail on the
# missing unit, which is why it is not in the converge.
- name: Verify the removal
hosts: all
gather_facts: false
vars:
docker_remove: true
docker_remove_data: true
docker_verify_packages: [docker-ce, docker-ce-cli, containerd.io]
tasks:
- name: Remove Docker with its data
ansible.builtin.include_role:
name: mortis.kamino.docker
- name: Stat what the removal must delete
ansible.builtin.stat:
path: "{{ item }}"
loop:
- /etc/apt/sources.list.d/docker.list
- /etc/apt/keyrings/docker.asc
- /var/lib/docker
- /usr/lib/systemd/system/docker.service
register: docker_verify_gone
- name: The source list, the keyring, the data and the unit are gone
ansible.builtin.assert:
that: docker_verify_gone.results | map(attribute='stat.exists') | unique | list == [false]
quiet: true
- name: Ask dpkg for the package states
ansible.builtin.command:
argv: [dpkg-query, --show, --showformat, "${db:Status-Status}\\n", "{{ item }}"]
loop: "{{ docker_verify_packages }}"
register: docker_verify_dpkg
changed_when: false
failed_when: false
- name: No package is installed any more
ansible.builtin.assert:
that: "'installed' not in docker_verify_dpkg.results | map(attribute='stdout') | map('trim') | list"
quiet: true
+52
View File
@@ -0,0 +1,52 @@
---
- name: Install prerequisite packages
become: true
ansible.builtin.apt:
name:
- ca-certificates
- curl
state: present
update_cache: true
tags: [docker, install]
- name: Ensure APT keyrings directory exists
become: true
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: "0755"
tags: [docker, install]
- name: Install Docker APT GPG key
become: true
ansible.builtin.get_url:
url: "{{ docker_gpg_url }}"
dest: "{{ docker_keyring_path }}"
mode: "0644"
tags: [docker, install]
- name: Install Docker APT source list
become: true
ansible.builtin.copy:
dest: "{{ docker_apt_list_path }}"
content: |
deb [arch={{ docker_apt_arch }} signed-by={{ docker_keyring_path }}] {{ docker_repo_base_url }} {{ ansible_distribution_release }} stable
mode: "0644"
tags: [docker, install]
- name: Install Docker Engine packages
become: true
ansible.builtin.apt:
name: "{{ docker_packages }}"
state: present
update_cache: true
notify: Restart docker
tags: [docker, install]
- name: Enable and start Docker service
become: true
ansible.builtin.systemd:
name: "{{ docker_service }}"
enabled: true
state: started
tags: [docker, install]
+30
View File
@@ -0,0 +1,30 @@
---
- name: Remove Docker (explicit only)
ansible.builtin.include_tasks: remove.yml
when: docker_remove | default(false) | bool
tags: [docker, remove]
- name: Install Docker Engine
ansible.builtin.include_tasks: install.yml
when: not (docker_remove | default(false) | bool)
tags: [docker, install]
- name: Ensure Docker Engine is up-to-date
ansible.builtin.include_tasks: update.yml
when:
- not (docker_remove | default(false) | bool)
- docker_update | bool
tags: [docker, update]
- name: Add configured users to the docker group
become: true
ansible.builtin.user:
name: "{{ item }}"
groups: "{{ docker_group }}"
append: true
loop: "{{ docker_users }}"
when: not (docker_remove | default(false) | bool)
tags: [docker, configure]
- name: Flush handlers
ansible.builtin.meta: flush_handlers
+39
View File
@@ -0,0 +1,39 @@
---
# Run with docker_remove=true (and docker_remove_data=true to purge) on a host that is safe to change.
- name: Stop and disable Docker service
become: true
ansible.builtin.systemd:
name: "{{ docker_service }}"
state: stopped
enabled: false
tags: [docker, remove]
- name: Remove Docker Engine packages
become: true
ansible.builtin.apt:
name: "{{ docker_packages }}"
state: absent
purge: "{{ docker_remove_data | default(false) | bool }}"
tags: [docker, remove]
- name: Remove Docker APT source list
become: true
ansible.builtin.file:
path: "{{ docker_apt_list_path }}"
state: absent
tags: [docker, remove]
- name: Remove Docker APT keyring
become: true
ansible.builtin.file:
path: "{{ docker_keyring_path }}"
state: absent
tags: [docker, remove]
- name: Remove Docker data directory (images, containers, volumes)
become: true
ansible.builtin.file:
path: /var/lib/docker
state: absent
when: docker_remove_data | default(false) | bool
tags: [docker, remove]
+61
View File
@@ -0,0 +1,61 @@
---
# Run with docker_update=true (tag: update).
- name: Refresh APT cache before checking for Docker updates
become: true
ansible.builtin.apt:
update_cache: true
tags: [docker, update]
- name: Determine latest available candidate version for each Docker package
ansible.builtin.command: "apt-cache policy {{ item }}"
register: docker_apt_policy
changed_when: false
# Read-only, so it also runs in the --check preview. Skipped there, there is no stdout to read the
# candidate from.
check_mode: false
loop: "{{ docker_packages }}"
loop_control:
label: "{{ item }}"
tags: [docker, update]
- name: Extract candidate version for each Docker package
ansible.builtin.set_fact:
docker_candidates: >-
{{ docker_candidates | default({}) | combine({item.item:
item.stdout | regex_search('Candidate:\s+(\S+)', '\1')
| default([''], true) | first}) }}
loop: "{{ docker_apt_policy.results }}"
loop_control:
label: "{{ item.item }}"
tags: [docker, update]
- name: Fail if any Docker package has no installable candidate
ansible.builtin.fail:
msg: >-
No installable candidate version found for Docker package '{{ item.key }}'
(apt-cache policy reported candidate '{{ item.value }}'). Check that the
Docker APT repo is reachable and the package name in docker_packages is
correct.
loop: "{{ docker_candidates | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: not item.value or item.value == '(none)'
tags: [docker, update]
# Pinned to each package's resolved candidate version (never state=latest)
# so this task stays idempotent and passes ansible-lint's package-latest rule.
- name: Build pinned package=version list from candidates
ansible.builtin.set_fact:
docker_pinned_packages: "{{ docker_pinned_packages | default([]) + [item.key ~ '=' ~ item.value] }}"
loop: "{{ docker_candidates | dict2items }}"
loop_control:
label: "{{ item.key }}"
tags: [docker, update]
- name: Update Docker Engine packages to latest candidate versions
become: true
ansible.builtin.apt:
name: "{{ docker_pinned_packages }}"
state: present
notify: Restart docker
tags: [docker, update]
+1
View File
@@ -2,3 +2,4 @@ COPYING no-smart-quotes
LICENSES/EUPL-1.2.txt no-smart-quotes
LICENSES/GPL-3.0-only.txt no-smart-quotes
LICENSES/GPL-3.0-or-later.txt no-smart-quotes
roles/docker/molecule/default/files/build-stub-repo.sh shebang # run by the docker scenario, not by Ansible