feat: simple copy from mhutter role

This commit is contained in:
Bertrand Lanson 2023-12-03 22:56:21 +01:00
parent caf3780f73
commit 1c1a301bec
10 changed files with 173 additions and 4 deletions

View File

@ -1,2 +1,33 @@
---
# defaults file for docker_systemd_service
container_name: "{{ name }}"
container_docker_pull: true
container_docker_pull_force_source: true
container_labels: []
container_cmd: []
container_host_network: false
container_network: ""
container_user: ""
container_hostname: ""
container_links: []
container_ports: []
container_hosts: []
container_volumes: []
container_cap_add: []
container_cap_drop: []
container_devices: []
container_privileged: false
container_args: ""
docker_path: "/usr/bin/docker"
service_name: "{{ container_name }}_container"
service_systemd_options: []
service_systemd_unit_options:
After: docker.service
PartOf: docker.service
Requires: docker.service
service_enabled: true
service_masked: false
service_state: started
service_restart: true
template_env_path: "env.j2"
template_unit_path: "unit.j2"

View File

@ -1,2 +1,7 @@
---
# handlers file for docker_systemd_service
- name: "restart container {{ container_name }}"
service:
name: '{{ service_name }}.service'
state: restarted
when: service_restart and service_state != "stopped" and not enable_and_start.changed

View File

@ -1,10 +1,10 @@
---
# meta file for deploy_adguard
# meta file for docker_systemd_service
galaxy_info:
namespace: 'ednxzu'
role_name: 'docker_systemd_service'
author: 'Bertrand Lanson'
description: ''
description: 'Create Systemd services for docker containers.'
license: 'license (BSD, MIT)'
min_ansible_version: '2.10'
platforms:
@ -15,11 +15,11 @@ galaxy_info:
- name: Debian
versions:
- bullseye
- bookworm
galaxy_tags:
- 'ubuntu'
- 'debian'
- 'adguard'
- 'adblock'
- 'docker'
- 'systemd'
dependencies: []

37
tasks/install.yml Normal file
View File

@ -0,0 +1,37 @@
---
# task/install file for docker_systemd_service
- name: Create ENV file for {{ service_name }}.service
template:
src: "{{ template_env_path }}"
dest: "{{ sysconf_dir }}/{{ container_name }}"
owner: root
group: root
mode: '0600'
when: container_env is defined
notify: restart container {{ container_name }}
- name: Pull image {{ container_image }}
docker_image:
name: '{{ container_image }}'
force_source: '{{ container_docker_pull_force_source | bool }}'
source: pull
when: container_docker_pull
notify: restart container {{ container_name }}
- name: Create unit {{ service_name }}.service
template:
src: "{{ template_unit_path }}"
dest: /etc/systemd/system/{{ service_name }}.service
owner: root
group: root
mode: '0644'
notify: restart container {{ container_name }}
- name: Enable and start {{ container_name }}
systemd:
name: '{{ service_name }}.service'
daemon_reload: true
enabled: "{{ service_enabled }}"
masked: "{{ service_masked }}"
state: "{{ service_state }}"
register: enable_and_start

View File

@ -1,2 +1,10 @@
---
# task/main file for docker_systemd_service
- name: Load distro-specific vars
include_vars: "{{ ansible_os_family }}.yml"
tags: always
- include_tasks: install.yml
when: service_state != "absent"
- include_tasks: uninstall.yml
when: service_state == "absent"

22
tasks/uninstall.yml Normal file
View File

@ -0,0 +1,22 @@
---
# task/uninstall file for docker_systemd_service
- name: Remove ENV file for {{ service_name }}.service
file:
path: "{{ sysconf_dir }}/{{ container_name }}"
state: absent
- name: Disable and stop {{ container_name }}
systemd:
name: '{{ service_name }}.service'
enabled: false
state: stopped
- name: Remove unit {{ service_name }}.service
file:
path: /etc/systemd/system/{{ service_name }}.service
state: absent
- name: Reload systemd units
systemd:
daemon_reload: true
changed_when: false

View File

3
templates/env.j2 Normal file
View File

@ -0,0 +1,3 @@
{% for k,v in container_env|dictsort %}
{{ k }}={{ v }}
{% endfor %}

62
templates/unit.j2 Normal file
View File

@ -0,0 +1,62 @@
# {{ ansible_managed }}
{% macro params(name, vals) %}
{% for v in vals %}{{ name }} {{ v }} {% endfor %}
{% endmacro %}
{% set service_systemd_options_keys = service_systemd_options | selectattr("key") | map(attribute="key") | list %}
[Unit]
{% for key, value in service_systemd_unit_options | dictsort %}
{{ key }}={{ value }}
{% endfor %}
[Service]
{% for item in service_systemd_options %}
{{ item['key'] }}={{ item['value'] }}
{% endfor %}
{% if container_env is defined %}
{% if not 'EnvironmentFile' in service_systemd_options_keys %}
EnvironmentFile={{ sysconf_dir }}/{{ container_name }}
{% endif %}
{% endif %}
{% if not 'ExecStartPre' in service_systemd_options_keys %}
ExecStartPre=-{{ docker_path }} rm -f {{ container_name }}
{% endif %}
{% if not 'ExecStart' in service_systemd_options_keys %}
ExecStart={{ docker_path }} run \
--name {{ container_name }} \
--rm \
{% if container_env is defined %}--env-file {{ sysconf_dir }}/{{ container_name }} {% endif %}\
{{ params('--volume', container_volumes) }}\
{% if container_host_network == true %}--network host {% else %}{{ params('--publish', container_ports) }}{% endif %}\
{% if container_network %}--network {{ container_network }}{% endif %} \
{% if container_user %}--user {{ container_user }}{% endif %} \
{% if container_hostname %}--hostname {{ container_hostname }}{% endif %} \
{{ params('--link', container_links) }}\
{{ params('--add-host', container_hosts) }}\
{{ params('--label', container_labels) }}\
{{ params('--cap-add', container_cap_add) }}\
{{ params('--cap-drop', container_cap_drop) }}\
{{ params('--device', container_devices) }}\
{% if container_privileged == true %}--privileged{% endif %}\
{{ container_args | trim }} \
{{ container_image }} {% if container_cmd is string %}{{ container_cmd | trim }}{% else %}{{ container_cmd | join(' ') | trim }}{% endif %}
{% endif %}
{% if not 'ExecStop' in service_systemd_options_keys %}
ExecStop={{ docker_path }} stop {{ container_name }}
{% endif %}
{% if container_start_post is defined %}
ExecStartPost=-{{ container_start_post }}
{% endif %}
{% if not 'SyslogIdentifier' in service_systemd_options_keys %}
SyslogIdentifier={{ container_name }}
{% endif %}
{% if not 'Restart' in service_systemd_options_keys %}
Restart=always
{% endif %}
{% if not 'RestartSec' in service_systemd_options_keys %}
RestartSec=10s
{% endif %}
[Install]
WantedBy=docker.service

View File

@ -1,2 +1,3 @@
---
# vars file for docker_systemd_service
sysconf_dir: /etc/default