aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2020-09-01 20:10:10 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2020-09-01 20:10:10 +0200
commit6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e (patch)
tree3ed44554680e965adf6e6453abac32746600d273 /ansible/roles
parente581bb7e3dd37572b739083133ffaf8d0962cb15 (diff)
downloadinfra-6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e.tar.gz
infra-6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e.tar.bz2
infra-6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e.tar.xz
infra-6ad4c172d23b88a0e0bace8488e4f53f6ee4ce6e.zip
ops
Diffstat (limited to 'ansible/roles')
-rw-r--r--ansible/roles/docker-service/defaults/main.yml5
-rw-r--r--ansible/roles/docker-service/handlers/main.yml7
-rw-r--r--ansible/roles/docker-service/tasks/main.yml64
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'