feat(docker): manage /etc/docker/daemon.json (live-restore, log driver, extra options)
Adds docker_daemon_options, docker_live_restore, docker_log_driver, docker_log_max_size, docker_log_max_file and docker_daemon_restart. The file is written, before the packages are installed, only when one of the first five is set, so a host that sets none is unchanged; the role then owns the whole file. It restarts dockerd only when docker_daemon_restart is true, because a restart with live-restore off ends every container, and says so when the file applies later. A key set by both docker_daemon_options and a dedicated input, and a bad log value, stop the run before any change. Closes #375 Assisted-by: Claude Code:claude-sonnet-5-5
This commit is contained in:
1 parent
428b88a822
commit
887302b208
10 files changed
+302
-1
No files matched your search
@@ -0,0 +1,9 @@
|
||||
---
|
||||
minor_changes:
|
||||
- >-
|
||||
docker - Add ``docker_daemon_options``, ``docker_live_restore``, ``docker_log_driver``, ``docker_log_max_size``, ``docker_log_max_file``
|
||||
and ``docker_daemon_restart``, which manage ``/etc/docker/daemon.json``. The file is written, before the packages are installed, only when
|
||||
at least one of the first five is set, so a host that sets none is unchanged; the role then owns the whole file. It restarts dockerd only
|
||||
when ``docker_daemon_restart`` is true (default ``false``), because a restart with ``live-restore`` off ends every container, and says
|
||||
that the file applies at the next restart otherwise. A key that ``docker_daemon_options`` and a dedicated input both set, a bad
|
||||
``docker_log_driver``, ``docker_log_max_size`` or ``docker_log_max_file`` stops the run before any change.
|
||||
+18
-1
@@ -35,13 +35,19 @@ The role targets Ubuntu hosts only: it installs from Docker's Ubuntu repository,
|
||||
| `docker_start_missed_settle_seconds` | `30` | no | Seconds the boot unit waits after Docker answers, so dockerd's own restarts finish first. |
|
||||
| `docker_stop_at_shutdown` | `false` | no | Install a unit that ends container processes at shutdown while the network and NFS are still up. |
|
||||
| `docker_stop_at_shutdown_timeout` | `30` | no | Seconds the shutdown unit waits after SIGTERM before it sends SIGKILL (at least 1). |
|
||||
| `docker_daemon_options` | `{}` | no | Extra keys merged into `/etc/docker/daemon.json` (see [Daemon settings](#daemon-settings)). |
|
||||
| `docker_live_restore` | `false` | no | Write `"live-restore": true`. |
|
||||
| `docker_log_driver` | empty | no | Written as `log-driver`, for example `local`. |
|
||||
| `docker_log_max_size` | empty | no | Written as the `max-size` log option, for example `10m`. |
|
||||
| `docker_log_max_file` | empty | no | Written as the `max-file` log option, a string, for example `"3"`. |
|
||||
| `docker_daemon_restart` | `false` | no | Restart dockerd when `daemon.json` changed. |
|
||||
| `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 and the two units). The tasks in `remove.yml` carry `docker` and `remove`.
|
||||
`docker` (everything), `install`, `update`, `remove`, `configure` (group membership, the daemon settings and the two units). 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.
|
||||
|
||||
@@ -60,6 +66,16 @@ Things to know before you switch `docker_stop_at_shutdown` on:
|
||||
- The script reads the layout of the systemd cgroup driver on cgroup v2 (`/sys/fs/cgroup/system.slice/docker-<id>.scope`, and the cgroups below it). On a host that lays containers out differently it finds nothing, logs "no container scopes" and changes nothing, so check the journal (`journalctl -t docker-stop-at-shutdown -b -1`) after the first reboot.
|
||||
- `docker-start-missed --dry-run` lists what the boot unit would start; add `--settle 0` to skip the wait.
|
||||
|
||||
## Daemon settings
|
||||
|
||||
With any of `docker_daemon_options`, `docker_live_restore`, `docker_log_driver`, `docker_log_max_size` or `docker_log_max_file` set, the role writes `/etc/docker/daemon.json` (root, mode `0644`, valid JSON) before it installs the packages, so a first start of dockerd already reads it. With none set, it writes nothing and leaves an existing file alone.
|
||||
|
||||
- **The role owns the whole file.** A key that is not in the inputs is removed, including one a person added by hand. Put every key you need in `docker_daemon_options`.
|
||||
- **A key set twice is refused.** `live-restore`, `log-driver` and `log-opts` in `docker_daemon_options` stop the run when the dedicated input also sets them.
|
||||
- **No restart by default.** A change applies at the next dockerd restart, and the role says so. A restart with `live-restore` off ends every container, so `docker_daemon_restart: true` is for a host where that is acceptable, or one that already runs with `live-restore` and whose change needs no more than a restart. Not every key can be applied by a reload; see the Docker documentation for the ones that can.
|
||||
- **A bad value stops dockerd, not the role.** The role checks the shape of the log inputs, not whether the driver accepts the options: dockerd refuses to start with a rejected one (the `local` driver, for example, refuses some `max-file` and `compress` combinations). Try a new value on a host that can be down before it reaches the others.
|
||||
- Setting an input back to its default does not remove a key from a file already written; `docker_remove` leaves the file in place.
|
||||
|
||||
## Example play
|
||||
|
||||
```yaml
|
||||
@@ -81,6 +97,7 @@ What it shows, with the role's tasks running unchanged:
|
||||
- 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`.
|
||||
- Both units are installed, enabled, ordered against `docker.service` as stated, the shutdown unit is active, a unit is removed when its input is `false`, and `docker_remove` removes both.
|
||||
- With the daemon inputs set, `/etc/docker/daemon.json` is valid JSON with exactly the expected keys, mode `0644` and root-owned. A change with `docker_daemon_restart: false` leaves dockerd running (its start time is unchanged); with `true` it restarts it. A key set both in `docker_daemon_options` and by a dedicated input, and a bad log size, driver or file count, are refused before any change.
|
||||
- The argument check refuses a mapping for `docker_users`, a non-boolean `docker_remove` and a `docker_stop_at_shutdown_timeout` of 0.
|
||||
- A second run changes nothing (molecule's idempotence check).
|
||||
|
||||
|
||||
@@ -46,3 +46,17 @@ docker_start_missed_settle_seconds: 30
|
||||
# Off by default; set it on a host whose containers mount NFS shares.
|
||||
docker_stop_at_shutdown: false
|
||||
docker_stop_at_shutdown_timeout: 30
|
||||
|
||||
# /etc/docker/daemon.json. The role writes the file only when at least one of these is set, and then owns it whole: a key it does not
|
||||
# know is removed. Nothing is written by default, so a host that sets none of them keeps its file. The role restarts dockerd only when
|
||||
# docker_daemon_restart is true (a restart with live-restore off ends every container); otherwise the file applies at the next restart.
|
||||
# Extra daemon.json keys, merged as they are; a key that a dedicated input below also sets is refused.
|
||||
docker_daemon_options: {}
|
||||
# true writes "live-restore": true, so a docker.service restart leaves running containers alone.
|
||||
docker_live_restore: false
|
||||
# "log-driver" (for example local) and the "max-size" and "max-file" log options (for example 10m and "3", quoted). Empty writes nothing.
|
||||
docker_log_driver: ""
|
||||
docker_log_max_size: ""
|
||||
docker_log_max_file: ""
|
||||
# true restarts dockerd when the file changed.
|
||||
docker_daemon_restart: false
|
||||
@@ -102,6 +102,50 @@ argument_specs:
|
||||
default: 30
|
||||
description:
|
||||
- Seconds the unit waits for container processes to end after SIGTERM before it sends SIGKILL. At least 1.
|
||||
docker_daemon_options:
|
||||
type: dict
|
||||
default: {}
|
||||
description:
|
||||
- >-
|
||||
Keys merged as they are into C(/etc/docker/daemon.json), for any setting the dedicated inputs below do not cover. A key that a
|
||||
dedicated input also sets (C(live-restore), C(log-driver) or C(log-opts)) stops the run. When this or any other daemon input is
|
||||
set the role owns the whole file: a key it does not know is removed. Nothing is written when every daemon input is at its default.
|
||||
docker_live_restore:
|
||||
type: bool
|
||||
default: false
|
||||
description:
|
||||
- >-
|
||||
true writes C(live-restore) true to C(/etc/docker/daemon.json), so a C(docker.service) restart leaves running containers alone.
|
||||
It takes effect at the next dockerd restart. Setting it back to false does not remove the key from a file already written.
|
||||
docker_log_driver:
|
||||
type: str
|
||||
default: ""
|
||||
description:
|
||||
- >-
|
||||
Default logging driver written as C(log-driver) (for example C(local) or C(json-file)); lowercase letters, digits and C(-).
|
||||
Empty writes nothing. The role does not check that the driver accepts the log options below: dockerd refuses to start with one
|
||||
it rejects.
|
||||
docker_log_max_size:
|
||||
type: str
|
||||
default: ""
|
||||
description:
|
||||
- >-
|
||||
Written as the C(max-size) log option, a whole number with an optional C(k), C(m) or C(g) (for example C(10m)), or C(-1).
|
||||
Empty writes nothing.
|
||||
docker_log_max_file:
|
||||
type: str
|
||||
default: ""
|
||||
description:
|
||||
- >-
|
||||
Written as the C(max-file) log option, a whole number of at least 1. Quote it, because the input is a string. Empty writes
|
||||
nothing.
|
||||
docker_daemon_restart:
|
||||
type: bool
|
||||
default: false
|
||||
description:
|
||||
- >-
|
||||
true restarts dockerd when C(/etc/docker/daemon.json) changed. false (the default) leaves the running daemon alone, because a
|
||||
restart with C(live-restore) off ends every container; the file then applies at the next restart and the role says so.
|
||||
docker_service:
|
||||
type: str
|
||||
default: docker
|
||||
|
||||
@@ -12,6 +12,12 @@
|
||||
docker_start_missed: true
|
||||
docker_stop_at_shutdown: true
|
||||
docker_stop_at_shutdown_timeout: 20
|
||||
docker_live_restore: true
|
||||
docker_log_driver: local
|
||||
docker_log_max_size: 10m
|
||||
docker_log_max_file: "3"
|
||||
docker_daemon_options:
|
||||
max-concurrent-downloads: 3
|
||||
roles:
|
||||
- role: mortis.kamino.docker
|
||||
|
||||
@@ -109,3 +115,21 @@
|
||||
- "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')"
|
||||
- "'docker_stop_at_shutdown_timeout' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
# A key that docker_daemon_options and a dedicated input both set, and a log value dockerd would reject, stop the run before any change.
|
||||
- name: A bad daemon setting is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Apply the role with each bad setting
|
||||
ansible.builtin.include_tasks: tasks/refuse_daemon.yml
|
||||
vars:
|
||||
docker_refused: "{{ item }}"
|
||||
loop:
|
||||
- {docker_live_restore: true, docker_daemon_options: {live-restore: false}}
|
||||
- {docker_log_driver: local, docker_daemon_options: {log-driver: json-file}}
|
||||
- {docker_log_max_size: 10m, docker_daemon_options: {log-opts: {max-size: 1m}}}
|
||||
- {docker_log_driver: Local}
|
||||
- {docker_log_max_size: ten}
|
||||
- {docker_log_max_file: "0"}
|
||||
- {docker_log_max_file: "-1"}
|
||||
@@ -0,0 +1,23 @@
|
||||
---
|
||||
# The stand-in repository is not needed: validation stops the role before the first change.
|
||||
- name: Run the role with a bad daemon setting
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.docker
|
||||
vars:
|
||||
docker_live_restore: "{{ docker_refused.docker_live_restore | default(false) }}"
|
||||
docker_log_driver: "{{ docker_refused.docker_log_driver | default('') }}"
|
||||
docker_log_max_size: "{{ docker_refused.docker_log_max_size | default('') }}"
|
||||
docker_log_max_file: "{{ docker_refused.docker_log_max_file | default('') }}"
|
||||
docker_daemon_options: "{{ docker_refused.docker_daemon_options | default({}) }}"
|
||||
- name: Fail when the bad setting was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The validation stopped the play
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')"
|
||||
- "'docker_' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
@@ -203,6 +203,94 @@
|
||||
vars:
|
||||
docker_start_missed: true
|
||||
|
||||
- name: Verify the daemon settings
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
docker_verify_expected:
|
||||
live-restore: true
|
||||
log-driver: local
|
||||
log-opts:
|
||||
max-file: "3"
|
||||
max-size: 10m
|
||||
max-concurrent-downloads: 3
|
||||
tasks:
|
||||
- name: Read and stat daemon.json
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/docker/daemon.json
|
||||
register: docker_verify_daemon
|
||||
|
||||
- name: Stat daemon.json
|
||||
ansible.builtin.stat:
|
||||
path: /etc/docker/daemon.json
|
||||
register: docker_verify_daemon_stat
|
||||
|
||||
- name: The file holds exactly the keys the converge set, root-owned with mode 0644
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- docker_verify_daemon.content | b64decode | from_json == docker_verify_expected
|
||||
- docker_verify_daemon_stat.stat.mode == '0644'
|
||||
- docker_verify_daemon_stat.stat.pw_name == 'root'
|
||||
quiet: true
|
||||
|
||||
# The stand-in docker.service runs for real, so a restart shows in its start time: a changed file with docker_daemon_restart false leaves
|
||||
# it alone, and with true restarts it.
|
||||
- name: Verify that dockerd restarts only when asked
|
||||
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]
|
||||
docker_start_missed: true
|
||||
docker_stop_at_shutdown: true
|
||||
docker_stop_at_shutdown_timeout: 20
|
||||
docker_log_driver: local
|
||||
docker_log_max_size: 10m
|
||||
docker_log_max_file: "3"
|
||||
docker_live_restore: true
|
||||
tasks:
|
||||
- name: Read the start time of the service
|
||||
ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module
|
||||
register: docker_verify_before
|
||||
changed_when: false
|
||||
|
||||
- name: Apply a changed setting with docker_daemon_restart false
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.docker
|
||||
vars:
|
||||
docker_daemon_options: {max-concurrent-downloads: 5}
|
||||
docker_daemon_restart: false
|
||||
|
||||
- name: Read the start time again
|
||||
ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module
|
||||
register: docker_verify_kept
|
||||
changed_when: false
|
||||
|
||||
- name: Apply another change with docker_daemon_restart true
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.docker
|
||||
vars:
|
||||
docker_daemon_options: {max-concurrent-downloads: 6}
|
||||
docker_daemon_restart: true
|
||||
|
||||
- name: Read the start time a third time
|
||||
ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module
|
||||
register: docker_verify_restarted
|
||||
changed_when: false
|
||||
|
||||
- name: Read daemon.json
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/docker/daemon.json
|
||||
register: docker_verify_daemon_after
|
||||
|
||||
- name: The first change left dockerd alone and the second restarted it
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- docker_verify_kept.stdout == docker_verify_before.stdout
|
||||
- docker_verify_restarted.stdout != docker_verify_kept.stdout
|
||||
- (docker_verify_daemon_after.content | b64decode | from_json)['max-concurrent-downloads'] == 6
|
||||
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
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# Written before the packages are installed, so a first start of dockerd already reads it. On a host that runs Docker the file takes effect
|
||||
# at the next daemon restart: this role restarts dockerd only when docker_daemon_restart is true, because a restart with live-restore
|
||||
# off ends every container.
|
||||
- name: Collect the log options
|
||||
ansible.builtin.set_fact:
|
||||
__docker_log_opts: >-
|
||||
{{ ({'max-size': docker_log_max_size} if docker_log_max_size | length > 0 else {})
|
||||
| combine({'max-file': docker_log_max_file} if docker_log_max_file | length > 0 else {}) }}
|
||||
tags: [docker, configure]
|
||||
|
||||
- name: Work out the daemon settings
|
||||
ansible.builtin.set_fact:
|
||||
__docker_daemon_config: >-
|
||||
{{ docker_daemon_options
|
||||
| combine({'live-restore': true} if docker_live_restore | bool else {})
|
||||
| combine({'log-driver': docker_log_driver} if docker_log_driver | length > 0 else {})
|
||||
| combine({'log-opts': __docker_log_opts} if __docker_log_opts | length > 0 else {}) }}
|
||||
tags: [docker, configure]
|
||||
|
||||
- name: Create the Docker configuration directory
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: /etc/docker
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
when: __docker_daemon_config | length > 0
|
||||
tags: [docker, configure]
|
||||
|
||||
- name: Write the daemon settings
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/docker/daemon.json
|
||||
content: "{{ __docker_daemon_config | to_nice_json }}\n"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
register: __docker_daemon_file
|
||||
notify: "{{ ['Restart docker'] if docker_daemon_restart | bool else [] }}"
|
||||
when: __docker_daemon_config | length > 0
|
||||
tags: [docker, configure]
|
||||
|
||||
- name: Say when the daemon settings apply
|
||||
ansible.builtin.debug:
|
||||
msg: /etc/docker/daemon.json changed. dockerd reads it at its next restart; set docker_daemon_restart to restart it from this role.
|
||||
when:
|
||||
- __docker_daemon_file is changed
|
||||
- not (docker_daemon_restart | bool)
|
||||
tags: [docker, configure]
|
||||
@@ -9,6 +9,11 @@
|
||||
when: docker_remove | default(false) | bool
|
||||
tags: [docker, remove]
|
||||
|
||||
- name: Write the daemon settings
|
||||
ansible.builtin.include_tasks: daemon.yml
|
||||
when: not (docker_remove | default(false) | bool)
|
||||
tags: [docker, configure]
|
||||
|
||||
- name: Install Docker Engine
|
||||
ansible.builtin.include_tasks: install.yml
|
||||
when: not (docker_remove | default(false) | bool)
|
||||
|
||||
@@ -31,3 +31,29 @@
|
||||
docker_start_missed_settle_seconds must be a whole number of at least 0 and docker_stop_at_shutdown_timeout a whole number of at least 1.
|
||||
quiet: true
|
||||
tags: always
|
||||
|
||||
# The values go into daemon.json, which dockerd refuses to start with when it holds a bad one, so a shape check stops the run first.
|
||||
- name: Check the daemon log settings
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- docker_log_driver is match('^[a-z0-9-]*\Z')
|
||||
- docker_log_max_size is match('^(-1|[0-9]+[kKmMgG]?)?\Z')
|
||||
- docker_log_max_file is match('^[0-9]*\Z')
|
||||
- docker_log_max_file | length == 0 or docker_log_max_file | int >= 1
|
||||
fail_msg: >-
|
||||
docker_log_driver must be empty or lowercase letters, digits and -; docker_log_max_size empty, -1 or a whole number with an optional
|
||||
k, m or g (for example 10m); docker_log_max_file empty or a whole number of at least 1 (quote it).
|
||||
quiet: true
|
||||
tags: always
|
||||
|
||||
- name: Refuse a daemon option that a dedicated input also sets
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- not (docker_live_restore | bool and 'live-restore' in docker_daemon_options)
|
||||
- not (docker_log_driver | length > 0 and 'log-driver' in docker_daemon_options)
|
||||
- not ((docker_log_max_size | length > 0 or docker_log_max_file | length > 0) and 'log-opts' in docker_daemon_options)
|
||||
fail_msg: >-
|
||||
docker_daemon_options sets a key that a dedicated input also sets: live-restore (docker_live_restore), log-driver (docker_log_driver)
|
||||
or log-opts (docker_log_max_size and docker_log_max_file). Set it in one place.
|
||||
quiet: true
|
||||
tags: always
|
||||
Reference in new issue
Block a user