From a862606df2c0b3a158e6f7d39cdd994c517fb0d2 Mon Sep 17 00:00:00 2001 From: uumas Date: Sat, 5 Jul 2025 16:14:07 +0300 Subject: [PATCH] volume: Support device-based volumes Also support those options in container and service roles --- roles/container/meta/argument_specs.yaml | 24 +++++++++++- roles/container/tasks/main.yaml | 3 ++ roles/container/vars/main.yaml | 2 +- roles/service/meta/argument_specs.yaml | 48 +++++++++++++++++++++++- roles/volume/defaults/main.yaml | 3 ++ roles/volume/handlers/main.yaml | 7 ++++ roles/volume/meta/argument_specs.yaml | 16 ++++++++ roles/volume/tasks/main.yaml | 5 ++- roles/volume/vars/main.yaml | 24 ++++++++++-- 9 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 roles/volume/handlers/main.yaml diff --git a/roles/container/meta/argument_specs.yaml b/roles/container/meta/argument_specs.yaml index f72d61a..34f1bab 100644 --- a/roles/container/meta/argument_specs.yaml +++ b/roles/container/meta/argument_specs.yaml @@ -48,7 +48,7 @@ argument_specs: elements: dict options: type: - description: Type of volume + description: Type of mount type: str required: true choices: @@ -80,6 +80,28 @@ argument_specs: type: str required: false default: "" + volume_device: + description: >- + The path of a device which is mounted for the volume. + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_type: + description: >- + The filesystem type of device as used by the mount commands -t option + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_mount_options: + description: >- + The mount options to use for a filesystem as used by the mount command -o option + Only applicable if mount type is volume. + type: list + elements: str + required: false + default: [] container_publish_ports: description: "A list of published ports in docker format (::)" diff --git a/roles/container/tasks/main.yaml b/roles/container/tasks/main.yaml index d96bb52..9de6851 100644 --- a/roles/container/tasks/main.yaml +++ b/roles/container/tasks/main.yaml @@ -27,6 +27,9 @@ volume_name: "{{ volume.source }}" volume_uid: "{{ volume.user | default('') }}" volume_gid: "{{ volume.group | default('') }}" + volume_type: "{{ volume.volume_type | default('') }}" + volume_device: "{{ volume.volume_device | default('') }}" + volume_mount_options: "{{ volume.volume_mount_options | default([]) }}" loop: "{{ _container_volumes }}" loop_control: loop_var: volume diff --git a/roles/container/vars/main.yaml b/roles/container/vars/main.yaml index 063f645..f063eac 100644 --- a/roles/container/vars/main.yaml +++ b/roles/container/vars/main.yaml @@ -18,7 +18,7 @@ _container_mounts: >- {{ container_mounts | selectattr('type', '!=', 'volume') + container_mounts | selectattr('type', '==', 'volume') - | community.general.remove_keys(['user', 'group']) + | community.general.keep_keys(['type', 'source', 'destination', 'readonly']) | zip(_container_volume_mount_sources) | map('combine') }} diff --git a/roles/service/meta/argument_specs.yaml b/roles/service/meta/argument_specs.yaml index 1922c42..60eeab2 100644 --- a/roles/service/meta/argument_specs.yaml +++ b/roles/service/meta/argument_specs.yaml @@ -77,7 +77,7 @@ argument_specs: elements: dict options: type: - description: Type of volume + description: Type of mount type: str required: true choices: @@ -112,6 +112,28 @@ argument_specs: type: str required: false default: "" + volume_device: + description: >- + The path of a device which is mounted for the volume. + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_type: + description: >- + The filesystem type of device as used by the mount commands -t option + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_mount_options: + description: >- + The mount options to use for a filesystem as used by the mount command -o option + Only applicable if mount type is volume. + type: list + elements: str + required: false + default: [] service_container_secrets: description: - > @@ -249,7 +271,7 @@ argument_specs: elements: dict options: type: - description: Type of volume + description: Type of mount type: str required: true choices: @@ -274,6 +296,28 @@ argument_specs: - Defaults to false for volume and bind, true for template type: bool required: false + volume_device: + description: >- + The path of a device which is mounted for the volume. + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_type: + description: >- + The filesystem type of device as used by the mount commands -t option + Only applicable if mount type is volume. + type: str + required: false + default: "" + volume_mount_options: + description: >- + The mount options to use for a filesystem as used by the mount command -o option + Only applicable if mount type is volume. + type: list + elements: str + required: false + default: [] publish_ports: description: "A list of published ports in docker format (::)" type: list diff --git a/roles/volume/defaults/main.yaml b/roles/volume/defaults/main.yaml index 54af040..515c067 100644 --- a/roles/volume/defaults/main.yaml +++ b/roles/volume/defaults/main.yaml @@ -1,3 +1,6 @@ --- volume_uid: "" volume_gid: "" +volume_type: "" +volume_device: "" +volume_mount_options: [] diff --git a/roles/volume/handlers/main.yaml b/roles/volume/handlers/main.yaml new file mode 100644 index 0000000..3986973 --- /dev/null +++ b/roles/volume/handlers/main.yaml @@ -0,0 +1,7 @@ +--- +- name: "Restart volume service {{ volume_name }}" + ansible.builtin.systemd_service: + name: "{{ volume_name }}-volume.service" + state: restarted + daemon_reload: true + ignore_errors: '{{ ansible_check_mode }}' diff --git a/roles/volume/meta/argument_specs.yaml b/roles/volume/meta/argument_specs.yaml index c357a23..fd78218 100644 --- a/roles/volume/meta/argument_specs.yaml +++ b/roles/volume/meta/argument_specs.yaml @@ -17,3 +17,19 @@ argument_specs: type: str required: false default: "" + volume_device: + description: The path of a device which is mounted for the volume. + type: str + required: false + default: "" + volume_type: + description: The filesystem type of device as used by the mount commands -t option + type: str + required: false + default: "" + volume_mount_options: + description: The mount options to use for a filesystem as used by the mount command -o option + type: list + elements: str + required: false + default: [] diff --git a/roles/volume/tasks/main.yaml b/roles/volume/tasks/main.yaml index 767a472..3538e4c 100644 --- a/roles/volume/tasks/main.yaml +++ b/roles/volume/tasks/main.yaml @@ -5,7 +5,8 @@ - name: Create container volume service {{ volume_name }} containers.podman.podman_volume: name: "{{ volume_name }}" - options: "{{ volume_options }}" + options: "{{ _volume_options }}" state: quadlet quadlet_file_mode: "0644" - notify: Reload systemd daemon + quadlet_options: "{{ _volume_quadlet_options }}" + notify: Restart volume service {{ volume_name }} diff --git a/roles/volume/vars/main.yaml b/roles/volume/vars/main.yaml index 3535dfe..b1471e0 100644 --- a/roles/volume/vars/main.yaml +++ b/roles/volume/vars/main.yaml @@ -1,6 +1,24 @@ --- -volume_mount_options_incl_empty: +_volume_mount_options_incl_empty: - "{{ 'uid=' ~ volume_uid if volume_uid | length > 0 else '' }}" - "{{ 'gid=' ~ volume_gid if volume_gid | length > 0 else '' }}" -volume_mount_options: "{{ volume_mount_options_incl_empty | select('!=', '') | list }}" -volume_options: "{{ ['o=' ~ volume_mount_options | join(',')] if volume_mount_options | length > 0 else [] }}" +_volume_mount_options: >- + {{ + _volume_mount_options_incl_empty + | select('!=', '') + + volume_mount_options + }} +_volume_options: >- + {{ + (['o=' ~ _volume_mount_options | join(',')] if _volume_mount_options | length > 0 else []) + + (['type=' ~ volume_type] if volume_type | length > 0 else []) + + (['device=' ~ volume_device] if volume_device | length > 0 else []) + }} + +_volume_device_quadlet_options: | + [Service] + ExecStartPost=/usr/bin/podman volume mount {{ volume_name }} + ExecStop=/usr/bin/podman volume unmount {{ volume_name }} + ExecStop=/usr/bin/podman volume rm {{ volume_name }} +_volume_quadlet_options: >- + {{ [_volume_device_quadlet_options] if volume_device | length > 0 else [] }}