diff --git a/defaults/main.yml b/defaults/main.yml index 6672764..21145aa 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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" diff --git a/handlers/main.yml b/handlers/main.yml index 83b80bd..2b0c091 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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 diff --git a/meta/main.yml b/meta/main.yml index 935aabe..73f4395 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -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: [] diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..fed9c7c --- /dev/null +++ b/tasks/install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index b495ad0..d06798c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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" diff --git a/tasks/uninstall.yml b/tasks/uninstall.yml new file mode 100644 index 0000000..d4420dc --- /dev/null +++ b/tasks/uninstall.yml @@ -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 diff --git a/templates/.gitkeep b/templates/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/templates/env.j2 b/templates/env.j2 new file mode 100644 index 0000000..8e25a65 --- /dev/null +++ b/templates/env.j2 @@ -0,0 +1,3 @@ +{% for k,v in container_env|dictsort %} +{{ k }}={{ v }} +{% endfor %} \ No newline at end of file diff --git a/templates/unit.j2 b/templates/unit.j2 new file mode 100644 index 0000000..2a75907 --- /dev/null +++ b/templates/unit.j2 @@ -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 \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index df082ff..1476f66 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,3 @@ --- # vars file for docker_systemd_service +sysconf_dir: /etc/default \ No newline at end of file