From 4f3cf9eaaba6740b2a4417c61886cf72a03d4bf2 Mon Sep 17 00:00:00 2001 From: Alex Kretzschmar Date: Wed, 8 Aug 2018 15:37:36 +0100 Subject: [PATCH] initial commit --- README.md | 78 +++++++++++++++++++++++++++++++++ defaults/main.yml | 3 ++ tasks/main.yml | 11 +++++ templates/docker-compose.yml.j2 | 55 +++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/docker-compose.yml.j2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..667d007 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# ansible-role-docker-compose-generator + +Pass this role a hash and it will generate a docker-compose.yml file. The following structure is supported and is designed to be passed to the role using `group_vars`. + +Rendered files are output to the `output` directory. + +``` +--- + +# global vars +global_env_vars: + - "PUID=1313" + - "PGID=1313" +appdata_path: /opt/appdata +container_config_path: /config +container_data_path: /data + +# container definitions +containers: + - service_name: letsencrypt + active: true + image: linuxserver/letsencrypt + container_name: le + ports: + - 80:80 + - 443:443 + volumes: + - "{{ appdata_path }}/letsencrypt/config:{{ container_config_path }}" + restart: always + depends_on: + - unifi + - nextcloud + - quassel + include_global_env_vars: true + environment: + - EMAIL=email@email.com + - URL=some.tld + - SUBDOMAINS=nc, irc, unifi + - ONLY_SUBDOMAINS=true + - DHLEVEL=4096 + - TZ=Europe/London + - VALIDATION=http + mem_limit: 256m + - service_name: nextcloud + active: true + image: nextcloud + container_name: nextcloud + include_global_env_vars: false + volumes: + - "{{ appdata_path }}/nextcloud/html:/var/www/html" + - "{{ appdata_path }}/nextcloud/apps:/var/www/html/custom_apps" + - "{{ appdata_path }}/nextcloud/config:/var/www/html/config" + - "{{ appdata_path }}/nextcloud/data:/var/www/html/data" + - "{{ appdata_path }}/nextcloud/theme:/var/www/html/themes" + mem_limit: 256m + restart: "{{ unless_stopped }}" + ports: + - "8081:80" + - service_name: unifi + active: true + image: linuxserver/unifi + container_name: unifi + mem_limit: 512m + volumes: + - "{{ appdata_path }}/unifi:{{ container_config_path }}" + include_global_env_vars: true + restart: "{{ unless_stopped }}" + - service_name: quassel + active: true + image: linuxserver/quassel + container_name: quassel + include_global_env_vars: true + volumes: + - "{{ appdata_path }}/quassel:{{ container_config_path }}" + mem_limit: 128m + ports: + - "4242:4242" +``` \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..1f07e0e --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +output_path: output/ +include_global_env_vars: false \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..75fa1ac --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,11 @@ +--- +- name: create destination dir for generated readme + file: + path: "{{ output_path }}" + state: directory + +- name: write docker-compose file + template: + src: ../templates/docker-compose.yml.j2 + dest: "{{ output_path }}/docker-compose.yml" + delegate_to: localhost \ No newline at end of file diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..113f434 --- /dev/null +++ b/templates/docker-compose.yml.j2 @@ -0,0 +1,55 @@ +# {{ ansible_managed }} +--- +version: "2" +services: +{% for item in containers %} +{% if item.active %} + {{ item.service_name }}: + image: {{ item.image }} + container_name: {{ item.container_name }} +{% if item.network_mode is defined %} + network_mode: {{ item.network_mode }} +{% endif %} +{% if item.volumes is defined %} + volumes: +{% for item in item.volumes %} + - {{ item }} +{% endfor %} +{% endif %} +{% if item.ports is defined %} + ports: +{% for item in item.ports %} + - {{ item }} +{% endfor %} +{% endif %} +{% if ( item.environment is defined ) or ( item.include_global_env_vars ) %} + environment: +{% if item.include_global_env_vars %} +{% for item2 in global_env_vars %} + - {{ item2 }} +{% endfor %} +{% endif %} +{% if item.environment is defined %} +{% for item in item.environment %} + - {{ item }} +{% endfor %} +{% endif %} +{% endif %} +{% if item.depends_on is defined %} + depends_on: +{% for item in item.depends_on %} + - {{ item }} +{% endfor %} +{% endif %} +{% if item.hostname is defined %} + hostname: {{ item.hostname }} +{% endif %} +{% if item.mem_limit is defined %} + mem_limit: {{ item.mem_limit }} +{% endif %} +{% if item.restart is defined %} + restart: {{ item.restart }} +{% endif %} + +{% endif %} +{% endfor %}