diff --git a/.gitea/workflows/molecule.yml b/.gitea/workflows/molecule.yml index 44dd73f..8074d94 100644 --- a/.gitea/workflows/molecule.yml +++ b/.gitea/workflows/molecule.yml @@ -70,7 +70,7 @@ jobs: # Used by the scenarios as `image: ${MOLECULE_IMAGE}`; set it the same way to run # one locally. MOLECULE_IMAGE: >- - git.rkdr.net/mortis/molecule-ubuntu2404:0.1.8@sha256:e1ea7c6f953626c39d9f578d60a1ef668f09ed4e5d537d5c7601668b5c5e0eb3 + git.rkdr.net/mortis/molecule-ubuntu2404:0.1.9@sha256:2687d921fdcadf49c36bd7a3b01eaf5f2a53fa0e8f9ed91b2c7ab0631022193b # The same for Ubuntu 26.04, the release the hosts are moving to; used by # `image: ${MOLECULE_IMAGE_2604}` in extensions/molecule/image-smoke-2604. MOLECULE_IMAGE_2604: >- diff --git a/changelogs/fragments/docker-apt-arch-from-host.yml b/changelogs/fragments/docker-apt-arch-from-host.yml new file mode 100644 index 0000000..225fe8c --- /dev/null +++ b/changelogs/fragments/docker-apt-arch-from-host.yml @@ -0,0 +1,7 @@ +--- +minor_changes: + - >- + docker - ``docker_apt_arch`` now defaults to an empty value that derives the apt architecture from the host (``x86_64`` gives ``amd64``, + ``aarch64`` gives ``arm64``) instead of always writing ``amd64``, so an arm64 host no longer needs an override. A value set in the inventory + is used as before. A host with any other architecture fails the play with a message that names ``docker_apt_arch`` instead of getting an + ``amd64`` source line that its apt cannot use; set ``docker_apt_arch`` for such a host. diff --git a/roles/docker/README.md b/roles/docker/README.md index 447c5f5..f2be8a2 100644 --- a/roles/docker/README.md +++ b/roles/docker/README.md @@ -4,7 +4,7 @@ Installs Docker Engine from the upstream Docker APT repository, optionally updat ## 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. +Ubuntu. The key and repository URLs point at `download.docker.com/linux/ubuntu`, and the source line uses `ansible_distribution_release`. The apt architecture is derived from the host (`x86_64` gives `amd64`, `aarch64` gives `arm64`); any other architecture fails the play until `docker_apt_arch` is set. A limited molecule scenario covers the role's own logic (see [Tests](#tests)); the real Docker Engine is not tested. ## Requirements and run order @@ -25,7 +25,7 @@ Ubuntu. The key and repository URLs point at `download.docker.com/linux/ubuntu`, | `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_apt_arch` | derived from the host | no | Architecture in the APT source. Empty derives it (`x86_64` gives `amd64`, `aarch64` gives `arm64`); another architecture fails until this is set. A value set here is always used; lowercase letters and digits only. | | `docker_service` | `docker` | no | systemd unit name. | | `docker_group` | `docker` | no | Group for `docker_users`. | | `docker_users` | `[]` | no | Users added to the group. | diff --git a/roles/docker/defaults/main.yml b/roles/docker/defaults/main.yml index ef8edac..0c7acee 100644 --- a/roles/docker/defaults/main.yml +++ b/roles/docker/defaults/main.yml @@ -13,8 +13,9 @@ 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 +# Architecture written into the APT source. Empty (the default) derives it from the host: x86_64 gives amd64, aarch64 gives arm64, any +# other architecture fails the play, so set it here (for example armhf) when Docker publishes one for it. +docker_apt_arch: "" docker_service: docker docker_group: docker diff --git a/roles/docker/meta/argument_specs.yml b/roles/docker/meta/argument_specs.yml index ec2a6f1..d9b567c 100644 --- a/roles/docker/meta/argument_specs.yml +++ b/roles/docker/meta/argument_specs.yml @@ -56,9 +56,12 @@ argument_specs: - 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 + default: "" description: - - Architecture written into the APT source. Override per host if a host is not amd64. + - >- + Architecture written into the APT source. Empty (the default) derives it from the host: C(x86_64) gives C(amd64), C(aarch64) + gives C(arm64). Any other host architecture fails the play, so set this to the name Docker publishes for it. Lowercase letters + and digits only. A value set here is always used. docker_service: type: str default: docker diff --git a/roles/docker/molecule/default/converge.yml b/roles/docker/molecule/default/converge.yml index 48d0c6f..e022faa 100644 --- a/roles/docker/molecule/default/converge.yml +++ b/roles/docker/molecule/default/converge.yml @@ -59,3 +59,27 @@ - "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')" - "'docker_remove' in ansible_failed_result.msg | default('')" quiet: true + +# The architecture is written into the one-line apt source, so a value with a space or a bracket is refused before anything +# changes. The derived default is covered by the converge above and verify.yml (the container is x86_64, so the source says amd64). +- name: A docker_apt_arch value that would corrupt the apt source is refused + hosts: all + gather_facts: false + vars: + docker_apt_arch: "bad arch" + tasks: + - name: Apply the role with a bad architecture + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + - name: Fail when the bad architecture 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_apt_arch' in ansible_failed_result.msg | default('')" + quiet: true diff --git a/roles/docker/molecule/default/verify.yml b/roles/docker/molecule/default/verify.yml index 4298589..2fb7020 100644 --- a/roles/docker/molecule/default/verify.yml +++ b/roles/docker/molecule/default/verify.yml @@ -17,6 +17,7 @@ - /srv/docker-stub-repo/docker.asc register: docker_verify_files + # amd64 because the Molecule container is x86_64: the role derives the architecture from the host now. - name: The source line carries the arch, the keyring and the release ansible.builtin.assert: that: diff --git a/roles/docker/tasks/install.yml b/roles/docker/tasks/install.yml index e22bfb4..9671588 100644 --- a/roles/docker/tasks/install.yml +++ b/roles/docker/tasks/install.yml @@ -30,7 +30,7 @@ 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 + deb [arch={{ __docker_apt_arch }} signed-by={{ docker_keyring_path }}] {{ docker_repo_base_url }} {{ ansible_distribution_release }} stable mode: "0644" tags: [docker, install] diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml index be0652b..d5c2446 100644 --- a/roles/docker/tasks/main.yml +++ b/roles/docker/tasks/main.yml @@ -1,4 +1,9 @@ --- +- name: Validate inputs + ansible.builtin.import_tasks: validate.yml + when: not (docker_remove | default(false) | bool) + tags: always + - name: Remove Docker (explicit only) ansible.builtin.include_tasks: remove.yml when: docker_remove | default(false) | bool diff --git a/roles/docker/tasks/validate.yml b/roles/docker/tasks/validate.yml new file mode 100644 index 0000000..a4bf452 --- /dev/null +++ b/roles/docker/tasks/validate.yml @@ -0,0 +1,23 @@ +--- +# Fact-computing and assertion only, before any task changes the host. A removal does not write the apt source, so it does not need the architecture. +- name: Work out the apt architecture + ansible.builtin.set_fact: + __docker_apt_arch: >- + {{ docker_apt_arch | default('', true) or __docker_arch_by_fact[ansible_facts['architecture'] | default('')] | default('', true) }} + tags: always + +# The value is written into the one-line apt source (deb [arch=... signed-by=...]): a space or a bracket would corrupt it. +- name: Check the apt architecture + ansible.builtin.assert: + that: + - __docker_apt_arch is match('^[a-z0-9]+\Z') + fail_msg: >- + {% if docker_apt_arch | default('', true) | length > 0 %} + docker_apt_arch '{{ docker_apt_arch }}' is not a valid apt architecture name: use the name Docker publishes (for example amd64 or arm64), + lowercase letters and digits only. + {% else %} + The host architecture '{{ ansible_facts['architecture'] | default('unknown') }}' has no known Docker apt architecture (x86_64 and aarch64 do). + Set docker_apt_arch to the name Docker publishes for it, for example armhf. + {% endif %} + quiet: true + tags: always diff --git a/roles/docker/vars/main.yml b/roles/docker/vars/main.yml new file mode 100644 index 0000000..0b4a600 --- /dev/null +++ b/roles/docker/vars/main.yml @@ -0,0 +1,6 @@ +--- +# Constants, not inputs: the Docker apt architecture for each ansible_facts['architecture'] the role derives it for. docker_apt_arch +# overrides it for any other architecture. +__docker_arch_by_fact: + x86_64: amd64 + aarch64: arm64