diff options
Diffstat (limited to 'ansible/roles/docker-service')
-rw-r--r-- | ansible/roles/docker-service/defaults/main.yml | 5 | ||||
-rw-r--r-- | ansible/roles/docker-service/handlers/main.yml | 7 | ||||
-rw-r--r-- | ansible/roles/docker-service/tasks/main.yml | 64 |
3 files changed, 76 insertions, 0 deletions
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' |