feat(template): move flag formatting to filter_plugin
All checks were successful
test / Linting (push) Successful in 8s

This commit is contained in:
Bertrand Lanson 2023-12-09 18:36:25 +01:00
parent b466741102
commit 562fc0215e
3 changed files with 31 additions and 24 deletions

View File

@ -0,0 +1,26 @@
# filter_plugins/docker_filters.py
def create_docker_flags(flags):
return "\n".join([create_docker_flag(item) for item in flags])
def create_docker_flag(item):
if isinstance(item, dict):
key = list(item.keys())[0]
value = item[key]
if isinstance(value, list):
return "\n".join(['--{} "{}" \\'.format(key, val) for val in value])
else:
return '--{} "{}" \\'.format(key, value)
elif isinstance(item, str):
return "--{} \\".format(item)
else:
return ""
class FilterModule(object):
def filters(self):
return {
"create_docker_flags": create_docker_flags,
}

View File

@ -12,6 +12,9 @@ docker_systemd_service_flags:
- /var:/test:ro
- health-cmd: curl localhost:80
- health-retries: 10
- cap-add:
- SYS_ADMIN
- dns: "8.8.8.8"
docker_systemd_service_container_cmd: []
#! TO REPLACE

View File

@ -1,26 +1,4 @@
# {{ ansible_managed }}
{%- macro create_docker_flags(flags) %}
{% for item in flags %}
{{ create_docker_flag(item) }}
{% endfor %}
{% endmacro -%}
{%- macro create_docker_flag(item) %}
{%- if item is mapping %}
{% set key = item.keys() | first %}
{% set value = item[key] %}
{%- if value.__class__.__name__ == 'list' %}
{% for val in value %}
--{{ key }} "{{ val }}" \{% if not loop.last +%}
{% endif %}{% endfor %}
{%- else %}
--{{ key }} "{{ value }}" \
{%- endif %}
{%- elif item is string %}
--{{ item }} \
{%- 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 %}
@ -41,8 +19,8 @@ ExecStartPre=-{{ docker_systemd_service_docker_path }} rm -f {{ docker_systemd_s
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 %}
{{ docker_systemd_service_flags|create_docker_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 }}