From 6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 1 Sep 2020 20:10:10 +0200 Subject: ops --- ansible/roles/docker-service/defaults/main.yml | 5 ++ ansible/roles/docker-service/handlers/main.yml | 7 +++ ansible/roles/docker-service/tasks/main.yml | 64 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 ansible/roles/docker-service/defaults/main.yml create mode 100644 ansible/roles/docker-service/handlers/main.yml create mode 100644 ansible/roles/docker-service/tasks/main.yml (limited to 'ansible/roles') diff --git a/ansible/roles/docker-service/defaults/main.yml b/ansible/roles/docker-service/defaults/main.yml new file mode 100644 index 0000000..e07508c --- /dev/null +++ b/ansible/roles/docker-service/defaults/main.yml @@ -0,0 +1,5 @@ +docker_service__etc: /etc/docker-service +systemd_unit: "docker-service-{{ service }}" +systemd_enabled: "yes" +systemd_state: "started" +docker_service__restart: [] diff --git a/ansible/roles/docker-service/handlers/main.yml b/ansible/roles/docker-service/handlers/main.yml new file mode 100644 index 0000000..475c016 --- /dev/null +++ b/ansible/roles/docker-service/handlers/main.yml @@ -0,0 +1,7 @@ +- name: systemctl restart + become: yes + loop: "{{ docker_service__restart }}" + systemd: + name: "{{ item }}" + state: restarted +# debug: var=docker_service__restart diff --git a/ansible/roles/docker-service/tasks/main.yml b/ansible/roles/docker-service/tasks/main.yml new file mode 100644 index 0000000..3b4f703 --- /dev/null +++ b/ansible/roles/docker-service/tasks/main.yml @@ -0,0 +1,64 @@ +- name: apt install docker + tags: + - never + - packages + become: yes + apt: + name: + - docker.io + - docker-compose + install_recommends: no + +- name: "{{ docker_service__etc }}/{{ service }}" + become: yes + file: + path: "{{ docker_service__etc }}/{{ service }}" + state: directory + +- name: "{{ docker_service__etc }}/{{ service }}/docker-compose.yml" + become: yes + template: + dest: "{{ docker_service__etc }}/{{ service }}/docker-compose.yml" + src: "{{ template }}" + notify: systemctl restart + register: docker_descriptor + +- name: "/etc/systemd/system/{{ systemd_unit }}.service" + become: yes + copy: + dest: "/etc/systemd/system/{{ systemd_unit }}.service" + content: | + [Unit] + Description=Docker service: {{ service }} + After=network-online.target + After=docker.service + Requires=docker.service + + [Service] + WorkingDirectory={{ docker_service__etc }}/{{ service }}/ + ExecStartPre=-/usr/bin/docker-compose stop + ExecStartPre=-/usr/bin/docker-compose pull + ExecStart=/usr/bin/docker-compose up -d + ExecReload=/usr/bin/docker-compose up -d + + [Install] + WantedBy=multi-user.target + notify: systemctl restart + register: systemd_descriptor + +- name: systemd daemon-reload + become: yes + systemd: + daemon_reload: yes + when: systemd_descriptor.changed + +- name: systemd start "{{ systemd_unit }}" + become: yes + systemd: + name: "{{ systemd_unit }}" + enabled: "{{ systemd_enabled }}" + state: "{{ systemd_state }}" + +- set_fact: + docker_service__restart: "{{ docker_service__restart + [systemd_unit] }}" + when: (systemd_descriptor.changed or docker_descriptor.changed) and systemd_state == 'started' -- cgit v1.2.3