Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save punnie/537b8f9e380c63e0726f9eac06d584b5 to your computer and use it in GitHub Desktop.
Save punnie/537b8f9e380c63e0726f9eac06d584b5 to your computer and use it in GitHub Desktop.
Deploy a docker service using Ansible within Docker Swarm 1.12
---
- name: Check if network {{ name }} exists
delegate_to: "{{ groups['docker_swarm_issuer'][0] }}"
run_once: true
command: docker network ls -q --filter name=^{{ name }}$
register: network_exists
changed_when: false
- name: Create network {{ name }}
command: docker network create --driver {{ driver }} {{ name }}
when: network_exists.stdout == ""
delegate_to: "{{ groups['docker_swarm_issuer'][0] }}"
run_once: true
registry: 127.0.0.1:5000
networks: []
---
- name: Pull git repository for service {{ name }}
git:
repo: "{{ repo }}"
dest: "/tmp/{{ name }}"
accept_hostkey: true
when: repo is defined
- set_fact:
image: "{{ registry }}/{{ name }}"
when: repo is defined
- name: Build image {{ image }}
docker_image:
name: "{{ image }}"
path: "/tmp/{{ name }}"
push: yes
force: yes
when: repo is defined
# Create networks if needed
- include: ../../docker-network/tasks/main.yml
vars:
name: "{{ item }}"
driver: overlay
with_items: "{{ networks | default([]) }}"
# Deploy service
- name: "Service {{ name }} is created"
shell: docker service ls --filter name={{ name }} | grep '\s{{ name }}\s' | awk '{ print $1 }'
register: service_exists
changed_when: false
- name: "Service {{ name }} is running"
shell: docker service ls --filter name={{ name }} | grep '\s{{ name }}\s' | awk '{ printf $3 }' | sed 's/\([0-9]\)\+.*/\1/'
register: running_replicas
when: service_exists.stdout != ""
failed_when: running_replicas.stdout == "0"
changed_when: false
- fail: msg="The service {{ name }} is down"
when: running_replicas is defined and running_replicas.stdout == "0"
- set_fact:
networks_flags: "{{ networks|default([]) | map('regex_replace', '^(.*)$', '--network \\1') | list | join(' ') }}"
volumes_flags: "{{ volumes|default([]) | map('regex_replace', '^(.*)$', '--mount \\1') | list | join(' ') }}"
ports_flags: "{{ ports|default([]) | map('regex_replace', '^(.*)$', '-p \\1') | list | join(' ') }}"
additional_flags: "{{ additional_args|default('') | replace('\n', '') }}"
labels_flags: "{{ labels|default([]) | map('regex_replace', '^(.*)$', '--label \\1') | list | join(' ') }}"
env_flags: "{{ env|default([]) | map('regex_replace', '^(.*)$', '-e \"\\1\"') | list | join(' ') }}"
- set_fact:
replicas_flags: "{{ replicas | regex_replace('^(.*)$', '--replicas \\1') }}"
when: replicas is defined
- set_fact:
replicas_flags: ""
when: replicas is not defined
- set_fact:
virtual_host_env_var: "{{ virtual_host | regex_replace('^(.*)$', '--env VIRTUAL_HOST=\\1') }}"
when: virtual_host is defined
- set_fact:
virtual_host_env_var: ""
when: virtual_host is not defined
# - debug: var=networks_flags
# - debug: var=volumes_flags
# - debug: var=ports_flags
# - debug: var=replicas_flags
# - debug: var=additional_flags
- name: "Create service {{ name }}"
shell: "docker service create --name {{ name }} {{ env_flags }} {{ virtual_host_env_var }} {{ replicas_flags }} {{ networks_flags }} {{ ports_flags }} {{ volumes_flags }} {{ labels_flags }} {{ additional_flags }} {{ image }} {{ command | default() }}"
when: service_exists.stdout == ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment