feat(template): add support for passing all docker run arguments
All checks were successful
test / Linting (push) Successful in 9s

This commit is contained in:
Bertrand Lanson 2023-12-09 16:46:13 +01:00
parent b96cebd586
commit 4725483303
4 changed files with 37 additions and 21 deletions

View File

@ -5,6 +5,8 @@ docker_systemd_service_image:
docker_systemd_service_container_env: {}
docker_systemd_service_container_pull_image: true
docker_systemd_service_container_pull_force_source: true
docker_systemd_service_flags: []
#! TO REPLACE
docker_systemd_service_container_labels: []
docker_systemd_service_container_cmd: []
docker_systemd_service_container_host_network: false
@ -20,6 +22,8 @@ docker_systemd_service_container_cap_drop: []
docker_systemd_service_container_devices: []
docker_systemd_service_container_privileged: false
docker_systemd_service_container_args: ""
#! END TO REPLACE
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: []
docker_systemd_service_enabled: true

View File

@ -1,11 +1,17 @@
---
docker_systemd_service_container_name: "nginx"
docker_systemd_service_container_name: "ninx"
docker_systemd_service_image: nginx
docker_systemd_service_container_env: {}
docker_systemd_service_container_pull_image: true
docker_systemd_service_container_pull_force_source: false
docker_systemd_service_container_labels: []
docker_systemd_service_flags:
- network: host
- volume:
- /root:/mnt/volume
- /var:/test:ro
docker_systemd_service_container_cmd: []
#! TO REPLACE
docker_systemd_service_container_labels: []
docker_systemd_service_container_host_network: false
docker_systemd_service_container_network: ""
docker_systemd_service_container_user: ""
@ -19,6 +25,7 @@ docker_systemd_service_container_cap_drop: []
docker_systemd_service_container_devices: []
docker_systemd_service_container_privileged: false
docker_systemd_service_container_args: ""
#! END TO REPLACE
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: []
docker_systemd_service_enabled: true

View File

@ -6,3 +6,5 @@
- name: "Install docker"
ansible.builtin.include_role:
name: ednxzu.install_docker
vars:
install_docker_python_packages: true

View File

@ -1,7 +1,22 @@
# {{ ansible_managed }}
{% macro params(name, vals) %}
{% for v in vals %}{{ name }} {{ v }} {% endfor %}
{%- macro create_docker_flags(flags) %}
{% for item in flags %}
{{ create_docker_flag(item) }}
{% endfor %}
{% endmacro -%}
{%- macro create_docker_flag(item) %}
{% set key = item.keys() | first %}
{% set value = item[key] %}
{%- if value is string %}
--{{ key }} {{ value }} \
{%- elif value is iterable and value is not string %}
{% for val in value %}
--{{ key }} {{ val }} \{% if not loop.last +%}
{% endif %}{% endfor %}
{% endif -%}
{% endmacro %}
{% set service_systemd_options_keys = docker_systemd_service_systemd_options | selectattr("key") | map(attribute="key") | list %}
[Unit]
{% for key, value in docker_systemd_service_systemd_unit_options | dictsort %}
@ -12,30 +27,18 @@
{% for item in docker_systemd_service_systemd_options %}
{{ item['key'] }}={{ item['value'] }}
{% endfor %}
{% if docker_systemd_service_container_env is defined %}
{% if not 'EnvironmentFile' in service_systemd_options_keys %}
EnvironmentFile={{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }}
{% endif %}
{% endif %}
{% if not 'ExecStartPre' in service_systemd_options_keys %}
ExecStartPre=-{{ docker_systemd_service_docker_path }} rm -f {{ docker_systemd_service_container_name }}
{% endif %}
{% if not 'ExecStart' in service_systemd_options_keys %}
{% if docker_systemd_service_container_env is defined %}ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_service_container_name }} --rm --env-file {{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }} {% endif %}\
{{ params('--volume', docker_systemd_service_container_volumes)~' \\'| trim }}
{% if docker_systemd_service_container_host_network == true %}--network host \{% else %}{{ params('--publish', docker_systemd_service_container_ports)~'\\'| trim }}{% endif +%}
{% if docker_systemd_service_container_network %}--network {{ docker_systemd_service_container_network }} {% endif %}\
{% if docker_systemd_service_container_user %}--user {{ docker_systemd_service_container_user }} {% endif %}\
{% if docker_systemd_service_container_hostname %}--hostname {{ docker_systemd_service_container_hostname }} {% endif %}\
{{ params('--link', docker_systemd_service_container_links)~' \\'| trim }}
{{ params('--add-host', docker_systemd_service_container_hosts)~' \\'| trim }}
{{ params('--label', docker_systemd_service_container_labels)~' \\'| trim }}
{{ params('--cap-add', docker_systemd_service_container_cap_add)~' \\'| trim }}
{{ params('--cap-drop', docker_systemd_service_container_cap_drop)~' \\'| trim }}
{{ params('--device', docker_systemd_service_container_devices)~' \\'| trim }}
{% if docker_systemd_service_container_privileged == true %}--privileged {% endif %}\
{{ docker_systemd_service_container_args~' \\'| trim}}
{{ 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 %}
ExecStart={{ docker_systemd_service_docker_path }} run --name {{ docker_systemd_service_container_name }} \
--rm \
--env-file {{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }} \
{{ create_docker_flags(docker_systemd_service_flags) -}}
{{- 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 +%}
{% if not 'ExecStop' in service_systemd_options_keys %}
ExecStop={{ docker_systemd_service_docker_path }} stop {{ docker_systemd_service_container_name }}