feat: make everything mostly work

This commit is contained in:
Bertrand Lanson 2023-12-04 23:10:02 +01:00
parent 1c1a301bec
commit 769b07c814
16 changed files with 157 additions and 108 deletions

View File

@ -1,33 +1,28 @@
--- ---
# defaults file for docker_systemd_service # defaults file for docker_systemd_service
container_name: "{{ name }}" docker_systemd_service_container_name: "{{ name }}"
container_docker_pull: true docker_systemd_service_image:
container_docker_pull_force_source: true docker_systemd_service_container_env: {}
container_labels: [] docker_systemd_service_container_pull_image: true
container_cmd: [] docker_systemd_service_container_pull_force_source: true
container_host_network: false docker_systemd_service_container_labels: []
container_network: "" docker_systemd_service_container_cmd: []
container_user: "" docker_systemd_service_container_host_network: false
container_hostname: "" docker_systemd_service_container_network: ""
container_links: [] docker_systemd_service_container_user: ""
container_ports: [] docker_systemd_service_container_hostname: ""
container_hosts: [] docker_systemd_service_container_links: []
container_volumes: [] docker_systemd_service_container_ports: []
container_cap_add: [] docker_systemd_service_container_hosts: []
container_cap_drop: [] docker_systemd_service_container_volumes: []
container_devices: [] docker_systemd_service_container_cap_add: []
container_privileged: false docker_systemd_service_container_cap_drop: []
container_args: "" docker_systemd_service_container_devices: []
docker_path: "/usr/bin/docker" docker_systemd_service_container_privileged: false
service_name: "{{ container_name }}_container" docker_systemd_service_container_args: ""
service_systemd_options: [] docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
service_systemd_unit_options: docker_systemd_service_systemd_options: []
After: docker.service docker_systemd_service_enabled: true
PartOf: docker.service docker_systemd_service_masked: false
Requires: docker.service docker_systemd_service_state: started
service_enabled: true docker_systemd_service_restart: true
service_masked: false
service_state: started
service_restart: true
template_env_path: "env.j2"
template_unit_path: "unit.j2"

View File

@ -1,7 +1,10 @@
--- ---
# handlers file for docker_systemd_service # handlers file for docker_systemd_service
- name: "restart container {{ container_name }}" - name: "Restart service(s)"
service: ansible.builtin.service:
name: '{{ service_name }}.service' name: "{{ docker_systemd_service_name }}.service"
state: restarted state: restarted
when: service_restart and service_state != "stopped" and not enable_and_start.changed listen: "systemctl-restart-service"
when: docker_systemd_service_restart
and docker_systemd_service_state != "stopped"
and not _enable_and_start.changed

View File

@ -1,6 +1,7 @@
--- ---
- name: Converge - name: Converge
hosts: all hosts: all
become: true
tasks: tasks:
- name: "Include ednxzu.docker_systemd_service" - name: "Include ednxzu.docker_systemd_service"
ansible.builtin.include_role: ansible.builtin.include_role:

View File

@ -0,0 +1,6 @@
---
# requirements file for molecule
roles:
- name: ednxzu.manage_repositories
- name: ednxzu.manage_apt_packages
- name: ednxzu.install_docker

View File

@ -2,4 +2,5 @@
- name: Verify - name: Verify
hosts: all hosts: all
gather_facts: true gather_facts: true
become: true
tasks: tasks:

View File

@ -1,7 +1,8 @@
--- ---
- name: Converge - name: Converge
hosts: all hosts: all
become: true
tasks: tasks:
- name: "Include ednxzu.${REPO_NAME}" - name: "Include ednxzu.docker_systemd_service"
ansible.builtin.include_role: ansible.builtin.include_role:
name: "ednxzu.${REPO_NAME}" name: "ednxzu.docker_systemd_service"

View File

@ -0,0 +1,27 @@
---
docker_systemd_service_container_name: "nginx"
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_container_cmd: []
docker_systemd_service_container_host_network: false
docker_systemd_service_container_network: ""
docker_systemd_service_container_user: ""
docker_systemd_service_container_hostname: ""
docker_systemd_service_container_links: []
docker_systemd_service_container_ports: []
docker_systemd_service_container_hosts: []
docker_systemd_service_container_volumes: []
docker_systemd_service_container_cap_add: []
docker_systemd_service_container_cap_drop: []
docker_systemd_service_container_devices: []
docker_systemd_service_container_privileged: false
docker_systemd_service_container_args: ""
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"
docker_systemd_service_systemd_options: []
docker_systemd_service_enabled: true
docker_systemd_service_masked: false
docker_systemd_service_state: started
docker_systemd_service_restart: true

View File

@ -0,0 +1,8 @@
---
- name: Prepare
hosts: all
become: true
tasks:
- name: "Install docker"
ansible.builtin.include_role:
name: ednxzu.install_docker

View File

@ -0,0 +1,6 @@
---
# requirements file for molecule
roles:
- name: ednxzu.manage_repositories
- name: ednxzu.manage_apt_packages
- name: ednxzu.install_docker

View File

@ -2,4 +2,5 @@
- name: Verify - name: Verify
hosts: all hosts: all
gather_facts: true gather_facts: true
become: true
tasks: tasks:

View File

@ -1,37 +1,36 @@
--- ---
# task/install file for docker_systemd_service # task/install file for docker_systemd_service
- name: Create ENV file for {{ service_name }}.service - name: "Create ENV file(s) for docker service(s)"
template: ansible.builtin.template:
src: "{{ template_env_path }}" src: env.j2
dest: "{{ sysconf_dir }}/{{ container_name }}" dest: "{{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }}"
owner: root owner: root
group: root group: root
mode: '0600' mode: '0600'
when: container_env is defined notify: systemctl-restart-service
notify: restart container {{ container_name }}
- name: Pull image {{ container_image }} - name: "Pull docker image(s)"
docker_image: community.docker.docker_image:
name: '{{ container_image }}' name: "{{ docker_systemd_service_image }}"
force_source: '{{ container_docker_pull_force_source | bool }}' force_source: "{{ docker_systemd_service_container_pull_force_source | bool }}"
source: pull source: pull
when: container_docker_pull when: docker_systemd_service_container_pull_image
notify: restart container {{ container_name }} notify: systemctl-restart-service
- name: Create unit {{ service_name }}.service - name: "Create unit file(s) for service(s)"
template: ansible.builtin.template:
src: "{{ template_unit_path }}" src: unit.j2
dest: /etc/systemd/system/{{ service_name }}.service dest: "/etc/systemd/system/{{ docker_systemd_service_name }}.service"
owner: root owner: root
group: root group: root
mode: '0644' mode: '0644'
notify: restart container {{ container_name }} notify: systemctl-restart-service
- name: Enable and start {{ container_name }} - name: "Enable and start service(s)"
systemd: ansible.builtin.systemd:
name: '{{ service_name }}.service' name: '{{ docker_systemd_service_name }}.service'
daemon_reload: true daemon_reload: true
enabled: "{{ service_enabled }}" enabled: "{{ docker_systemd_service_enabled }}"
masked: "{{ service_masked }}" masked: "{{ docker_systemd_service_masked }}"
state: "{{ service_state }}" state: "{{ docker_systemd_service_state }}"
register: enable_and_start register: _enable_and_start

View File

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

View File

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

View File

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

View File

@ -2,54 +2,51 @@
{% macro params(name, vals) %} {% macro params(name, vals) %}
{% for v in vals %}{{ name }} {{ v }} {% endfor %} {% for v in vals %}{{ name }} {{ v }} {% endfor %}
{% endmacro %} {% endmacro %}
{% set service_systemd_options_keys = service_systemd_options | selectattr("key") | map(attribute="key") | list %} {% set service_systemd_options_keys = docker_systemd_service_systemd_options | selectattr("key") | map(attribute="key") | list %}
[Unit] [Unit]
{% for key, value in service_systemd_unit_options | dictsort %} {% for key, value in docker_systemd_service_systemd_unit_options | dictsort %}
{{ key }}={{ value }} {{ key }}={{ value }}
{% endfor %} {% endfor %}
[Service] [Service]
{% for item in service_systemd_options %} {% for item in docker_systemd_service_systemd_options %}
{{ item['key'] }}={{ item['value'] }} {{ item['key'] }}={{ item['value'] }}
{% endfor %} {% endfor %}
{% if container_env is defined %} {% if docker_systemd_service_container_env is defined %}
{% if not 'EnvironmentFile' in service_systemd_options_keys %} {% if not 'EnvironmentFile' in service_systemd_options_keys %}
EnvironmentFile={{ sysconf_dir }}/{{ container_name }} EnvironmentFile={{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if not 'ExecStartPre' in service_systemd_options_keys %} {% if not 'ExecStartPre' in service_systemd_options_keys %}
ExecStartPre=-{{ docker_path }} rm -f {{ 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_options_keys %}
ExecStart={{ docker_path }} run \ {% 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 %}\
--name {{ container_name }} \ {{ params('--volume', docker_systemd_service_container_volumes)~' \\'| trim }}
--rm \ {% if docker_systemd_service_container_host_network == true %}--network host \{% else %}{{ params('--publish', docker_systemd_service_container_ports)~'\\'| trim }}{% endif +%}
{% if container_env is defined %}--env-file {{ sysconf_dir }}/{{ container_name }} {% endif %}\ {% if docker_systemd_service_container_network %}--network {{ docker_systemd_service_container_network }} {% endif %}\
{{ params('--volume', container_volumes) }}\ {% if docker_systemd_service_container_user %}--user {{ docker_systemd_service_container_user }} {% endif %}\
{% if container_host_network == true %}--network host {% else %}{{ params('--publish', container_ports) }}{% endif %}\ {% if docker_systemd_service_container_hostname %}--hostname {{ docker_systemd_service_container_hostname }} {% endif %}\
{% if container_network %}--network {{ container_network }}{% endif %} \ {{ params('--link', docker_systemd_service_container_links)~' \\'| trim }}
{% if container_user %}--user {{ container_user }}{% endif %} \ {{ params('--add-host', docker_systemd_service_container_hosts)~' \\'| trim }}
{% if container_hostname %}--hostname {{ container_hostname }}{% endif %} \ {{ params('--label', docker_systemd_service_container_labels)~' \\'| trim }}
{{ params('--link', container_links) }}\ {{ params('--cap-add', docker_systemd_service_container_cap_add)~' \\'| trim }}
{{ params('--add-host', container_hosts) }}\ {{ params('--cap-drop', docker_systemd_service_container_cap_drop)~' \\'| trim }}
{{ params('--label', container_labels) }}\ {{ params('--device', docker_systemd_service_container_devices)~' \\'| trim }}
{{ params('--cap-add', container_cap_add) }}\ {% if docker_systemd_service_container_privileged == true %}--privileged {% endif %}\
{{ params('--cap-drop', container_cap_drop) }}\ {{ docker_systemd_service_container_args~' \\'| trim}}
{{ params('--device', container_devices) }}\ {{ docker_systemd_service_image }} {% if docker_systemd_service_container_cmd is string %}{{ docker_systemd_service_container_cmd | trim }}{% else %}{{ docker_systemd_service_container_cmd | join(' ') | trim }}{% endif %}
{% 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 %} {% endif %}
{% if not 'ExecStop' in service_systemd_options_keys %} {% if not 'ExecStop' in service_systemd_options_keys %}
ExecStop={{ docker_path }} stop {{ 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_options_keys %}
SyslogIdentifier={{ container_name }} SyslogIdentifier={{ docker_systemd_service_container_name }}
{% endif %} {% endif %}
{% if not 'Restart' in service_systemd_options_keys %} {% if not 'Restart' in service_systemd_options_keys %}
Restart=always Restart=always

View File

@ -1,3 +1,8 @@
--- ---
# vars file for docker_systemd_service # vars file for 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_systemd_unit_options:
After: docker.service
PartOf: docker.service
Requires: docker.service