feat: enable customization of unit options in systemd file
All checks were successful
development / Check commit compliance (push) Successful in 5s
test / Retrieve Credentials (pull_request) Successful in 1s
pull-requests-open / Check commit compliance (pull_request) Successful in 5s
test / end_to_end_role (default, debian11) (pull_request) Successful in 39s
test / end_to_end_role (default, debian12) (pull_request) Successful in 38s
test / end_to_end_role (default, ubuntu2004) (pull_request) Successful in 40s
test / end_to_end_role (default, ubuntu2204) (pull_request) Successful in 39s
test / end_to_end_role (default, ubuntu2404) (pull_request) Successful in 39s
test / end_to_end_role (with_custom_flags, debian11) (pull_request) Successful in 38s
test / end_to_end_role (with_custom_flags, debian12) (pull_request) Successful in 39s
test / end_to_end_role (with_custom_flags, ubuntu2004) (pull_request) Successful in 39s
test / end_to_end_role (with_custom_flags, ubuntu2204) (pull_request) Successful in 39s
test / end_to_end_role (with_custom_flags, ubuntu2404) (pull_request) Successful in 40s

This also adjusts the unit.j2 template accordingly, as well as update documentation for the role.

It also updates tests to cover more scenarios
This commit is contained in:
Bertrand Lanson 2024-11-09 17:03:46 +01:00
parent 56306d1e71
commit 8c9bdfbea5
Signed by: lanson
SSH Key Fingerprint: SHA256:/nqc6HGqld/PS208F6FUOvZlUzTS0rGpNNwR5O2bQBw
10 changed files with 81 additions and 76 deletions

101
README.md
View File

@ -1,20 +1,20 @@
docker_systemd_service **Docker systemd service**
========= =========================
> This repository is only a mirror. Development and testing is done on a private gitea server.
This role lets you configure a docker container and run it as a systemd service on **debian-based** distributions. This role is heavily sourced from [mhutter.docker-systemd-service](https://github.com/mhutter/ansible-docker-systemd-service), but aims at providing some of the missing features of said role. This role lets you configure a docker container and run it as a systemd service on **debian-based** distributions. This role is heavily sourced from [mhutter.docker-systemd-service](https://github.com/mhutter/ansible-docker-systemd-service), but aims at providing some of the missing features of said role.
Requirements **Requirements**
------------ ---------------
This roles assumes you have docker installed on the target host. You can use [ednz_cloud.install_docker](https://github.com/ednz_cloud/install_docker) to do so. This roles assumes you have **docker** installed on the target host. You can use [ednz_cloud.install_docker](https://github.com/ednz_cloud/install_docker) to do so.
Role Variables **Role Variables**
-------------- -----------------
Available variables are listed below, along with default values. A sample file for the default values is available in `default/docker_systemd_service.yml.sample` in case you need it for any `group_vars` or `host_vars` configuration.
### Service configuration
```yaml ```yaml
docker_systemd_service_container_name: "My-Service" # by default, set to "My-Service" docker_systemd_service_container_name: "My-Service"
``` ```
The name that will be assigned to the container. The name that will be assigned to the container.
@ -24,24 +24,47 @@ docker_systemd_service_image: # by default, not defined
The image (and optionally tag) to use for the service. The image (and optionally tag) to use for the service.
```yaml ```yaml
docker_systemd_service_container_env: {} # by default, set to {} docker_systemd_service_start: true
``` ```
A list of key/value pairs, that will be written to the environment file for the container. the key NEEDS TO BE CAPTIALIZED, it will not be done automatically. Example: `MY_ENV_VAR: foobar`. Indicates whether the service should start after installation. Defaults to `true`.
```yaml ```yaml
docker_systemd_service_container_pull_image: true # by default, set to true docker_systemd_service_systemd_unit_options: {}
``` ```
Whether or not the role should pull the image during its run. Extra options to add to the `[Unit]` section of the systemd unit file. Map of strings.
```yaml ```yaml
docker_systemd_service_container_pull_force_source: true # by default, set to true docker_systemd_service_systemd_service_options: {}
```
Extra options to add to the `[Service]` section of the systemd unit file. Map of strings.
```yaml
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
```
The name of the systemd service to register.
### Container configuration
```yaml
docker_systemd_service_container_env: {}
```
A list of key/value pairs, that will be written to the environment file for the container.
```yaml
docker_systemd_service_container_pull_image: true
```
Whether or not the role should pull the image during its run, prior to starting the service.
```yaml
docker_systemd_service_container_pull_force_source: true
``` ```
If `docker_systemd_service_container_pull_image: true`, whether the pull you be executed at every run. See [`docker_image.force_source`](https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#parameter-force_source) If `docker_systemd_service_container_pull_image: true`, whether the pull you be executed at every run. See [`docker_image.force_source`](https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#parameter-force_source)
```yaml ```yaml
docker_systemd_service_flags: [] # by default, set to [] docker_systemd_service_flags: []
``` ```
This variable lets you pass whatever flags you need to the docker run command. It is a list, to which you can add multiple types of flags: This variable lets you pass whatever flags you need to the docker run command. It is a list, to which you can add multiple types of flags:
- ```yaml - ```yaml
- key: value - key: value
# will pass the flag --key "value" to the container. # will pass the flag --key "value" to the container.
@ -62,43 +85,13 @@ This variable lets you pass whatever flags you need to the docker run command. I
- /path/on/host:/path/on/container - /path/on/host:/path/on/container
- /var/run/docker.sock:/var/run/docker.sock:ro - /var/run/docker.sock:/var/run/docker.sock:ro
```yaml **Dependencies**
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" # by default, set to "{{ docker_systemd_service_container_name }}_container" ---------------
```
The name of the systemd service to register.
```yaml
docker_systemd_service_systemd_options: [] # by default, set to []
```
Extra options to include in systemd service file.
```yaml
docker_systemd_service_enabled: true # by default, set to true
```
Whether the service should be enabled during the role's run.
```yaml
docker_systemd_service_masked: false # by default, set to false
```
Whether the service should be marked as masked.
```yaml
docker_systemd_service_state: started # by default, set to started
```
The state the service should be put in. Valid options are: `reloaded`, `restarted`, `started`, `stopped`, and `absent`. Realistically, you probably want to use `started` or `stopped`. `absent` can be used to remove the service and all associated files from the host.
```yaml
docker_systemd_service_restart: true # by default, set to true
```
Whether the role should restart the service if changes are made to any of the files (when service is already runing).
Dependencies
------------
None. None.
Example Playbook **Example Playbook**
---------------- -------------------
```yaml ```yaml
# calling the role inside a playbook with either the default or group_vars/host_vars # calling the role inside a playbook with either the default or group_vars/host_vars
@ -107,12 +100,12 @@ Example Playbook
- ednz_cloud.docker_systemd_service - ednz_cloud.docker_systemd_service
``` ```
License **License**
------- ----------
MIT / BSD MIT / BSD
Author Information **Author Information**
------------------ ---------------------
This role was created by Bertrand Lanson in 2023. This role was created by Bertrand Lanson in 2023.

View File

@ -8,6 +8,6 @@ docker_systemd_service_container_pull_force_source: true
docker_systemd_service_flags: [] docker_systemd_service_flags: []
docker_systemd_service_container_cmd: [] docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: [] docker_systemd_service_systemd_unit_options: {}
docker_systemd_service_systemd_service_options: {}
docker_systemd_service_start: true docker_systemd_service_start: true

View File

@ -7,5 +7,6 @@ docker_systemd_service_container_pull_force_source: false
docker_systemd_service_flags: [] docker_systemd_service_flags: []
docker_systemd_service_container_cmd: [] docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: [] docker_systemd_service_systemd_unit_options: {}
docker_systemd_service_systemd_service_options: {}
docker_systemd_service_start: false docker_systemd_service_start: false

View File

@ -7,5 +7,6 @@ docker_systemd_service_container_pull_force_source: false
docker_systemd_service_flags: [] docker_systemd_service_flags: []
docker_systemd_service_container_cmd: [] docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: [] docker_systemd_service_systemd_unit_options: {}
docker_systemd_service_systemd_service_options: {}
docker_systemd_service_start: true docker_systemd_service_start: true

View File

@ -12,5 +12,7 @@ docker_systemd_service_flags:
- NET_ADMIN - NET_ADMIN
docker_systemd_service_container_cmd: [] docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: [] docker_systemd_service_systemd_unit_options:
Requires: multi-user.target
docker_systemd_service_systemd_service_options: {}
docker_systemd_service_start: false docker_systemd_service_start: false

View File

@ -49,9 +49,9 @@
nginx_expected_service_file: | nginx_expected_service_file: |
# Ansible managed: Do NOT edit this file manually! # Ansible managed: Do NOT edit this file manually!
[Unit] [Unit]
Requires=multi-user.target
After=docker.service After=docker.service
PartOf=docker.service PartOf=docker.service
Requires=docker.service
[Service] [Service]
EnvironmentFile=/etc/default/nginx EnvironmentFile=/etc/default/nginx

View File

@ -12,5 +12,7 @@ docker_systemd_service_flags:
- NET_ADMIN - NET_ADMIN
docker_systemd_service_container_cmd: [] docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container" docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: [] docker_systemd_service_systemd_unit_options:
Requires: multi-user.target
docker_systemd_service_systemd_service_options: {}
docker_systemd_service_start: true docker_systemd_service_start: true

View File

@ -49,9 +49,9 @@
nginx_expected_service_file: | nginx_expected_service_file: |
# Ansible managed: Do NOT edit this file manually! # Ansible managed: Do NOT edit this file manually!
[Unit] [Unit]
Requires=multi-user.target
After=docker.service After=docker.service
PartOf=docker.service PartOf=docker.service
Requires=docker.service
[Service] [Service]
EnvironmentFile=/etc/default/nginx EnvironmentFile=/etc/default/nginx

View File

@ -1,21 +1,31 @@
# {{ ansible_managed }} # {{ ansible_managed }}
{% set service_systemd_options_keys = docker_systemd_service_systemd_options | selectattr("key") | map(attribute="key") | list %} {% set service_systemd_service_options_keys = docker_systemd_service_systemd_service_options.keys() | list %}
{% set service_systemd_unit_options_keys = docker_systemd_service_systemd_unit_options.keys() | list %}
[Unit] [Unit]
{% for key, value in docker_systemd_service_systemd_unit_options | dictsort %} {% for key, value in docker_systemd_service_systemd_unit_options | dictsort %}
{{ key }}={{ value }} {{ key }}={{ value }}
{% endfor %} {% endfor %}
{% if not 'After' in service_systemd_unit_options_keys %}
After=docker.service
{% endif %}
{% if not 'PartOf' in service_systemd_unit_options_keys %}
PartOf=docker.service
{% endif %}
{% if not 'Requires' in service_systemd_unit_options_keys %}
Requires=docker.service
{% endif %}
[Service] [Service]
{% for item in docker_systemd_service_systemd_options %} {% for key, value in docker_systemd_service_systemd_service_options | dictsort %}
{{ item['key'] }}={{ item['value'] }} {{ key }}={{ value }}
{% endfor %} {% endfor %}
{% if not 'EnvironmentFile' in service_systemd_options_keys %} {% if not 'EnvironmentFile' in service_systemd_service_options_keys %}
EnvironmentFile={{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }} EnvironmentFile={{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% if not 'ExecStartPre' in service_systemd_options_keys %} {% if not 'ExecStartPre' in service_systemd_service_options_keys %}
ExecStartPre=-{{ docker_systemd_service_docker_path }} rm -f {{ docker_systemd_service_container_name }} ExecStartPre=-{{ docker_systemd_service_docker_path }} rm -f {{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% if not 'ExecStart' in service_systemd_options_keys %} {% if not 'ExecStart' in service_systemd_service_options_keys %}
{% set docker_flags = docker_systemd_service_flags | create_docker_flags %} {% set docker_flags = docker_systemd_service_flags | create_docker_flags %}
ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_service_container_name }} \ ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_service_container_name }} \
--rm \ --rm \
@ -23,19 +33,19 @@ ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_
{{ docker_flags -}}{% if docker_flags +%} {{ docker_flags -}}{% if docker_flags +%}
{% endif %}{{ docker_systemd_service_image -}}{{ ' ' if docker_systemd_service_container_cmd else '' }}{% if docker_systemd_service_container_cmd is string %}{{ docker_systemd_service_container_cmd | trim }}{% else %}{{ docker_systemd_service_container_cmd | join(' ') | trim }}{% endif %} {% endif %}{{ docker_systemd_service_image -}}{{ ' ' if docker_systemd_service_container_cmd else '' }}{% if docker_systemd_service_container_cmd is string %}{{ docker_systemd_service_container_cmd | trim }}{% else %}{{ docker_systemd_service_container_cmd | join(' ') | trim }}{% endif %}
{% endif +%} {% endif +%}
{% if not 'ExecStop' in service_systemd_options_keys %} {% if not 'ExecStop' in service_systemd_service_options_keys %}
ExecStop={{ docker_systemd_service_docker_path }} stop {{ docker_systemd_service_container_name }} ExecStop={{ docker_systemd_service_docker_path }} stop {{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% if container_start_post is defined %} {% if container_start_post is defined %}
ExecStartPost=-{{ container_start_post }} ExecStartPost=-{{ container_start_post }}
{% endif %} {% endif %}
{% if not 'SyslogIdentifier' in service_systemd_options_keys %} {% if not 'SyslogIdentifier' in service_systemd_service_options_keys %}
SyslogIdentifier={{ docker_systemd_service_container_name }} SyslogIdentifier={{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% if not 'Restart' in service_systemd_options_keys %} {% if not 'Restart' in service_systemd_service_options_keys %}
Restart=always Restart=always
{% endif %} {% endif %}
{% if not 'RestartSec' in service_systemd_options_keys %} {% if not 'RestartSec' in service_systemd_service_options_keys %}
RestartSec=10s RestartSec=10s
{% endif %} {% endif %}

View File

@ -2,7 +2,3 @@
# vars file for docker_systemd_service # vars file for docker_systemd_service
docker_systemd_service_sysconf_dir: /etc/default docker_systemd_service_sysconf_dir: /etc/default
docker_systemd_service_docker_path: "/usr/bin/docker" docker_systemd_service_docker_path: "/usr/bin/docker"
docker_systemd_service_systemd_unit_options:
After: docker.service
PartOf: docker.service
Requires: docker.service