Skip to content

Instantly share code, notes, and snippets.

@olvesh
Last active April 6, 2022 12:30
Show Gist options
  • Save olvesh/11f7e726a96e3f5df2a26f92f7733255 to your computer and use it in GitHub Desktop.
Save olvesh/11f7e726a96e3f5df2a26f92f7733255 to your computer and use it in GitHub Desktop.
Ansible docker rolling deploy

Ansible rolling update using docker

Include docker-deploy.yml:

[...]
- name: "Deploying {{ service_name }} "
  hosts: "{{ service_name }}"
  serial: 1
  max_fail_percentage: 50
  tasks: 
  - include: docker-deploy.yml
[...]

Copy the wait_for_service.yml and docker-deploy.yml into the service deploy scripts belonging to each service (or parameterize further to make it general for all of your services).

---
- name: install vimond-eventservice image
docker:
name: example-service
image: example.org/example-service:{{ version }}
pull: "{{ docker_pull | d('always') }}"
state: reloaded
restart_policy: always
publish_all_ports: "true"
register: docker
# This include has a dependency on the `docker` result variable from the docker task above.
# It also assumes that there are two ports that can be used for health-checks worth waiting for 8080 and 8081
- include: wait_for_service.yml
checks:
- port: 8080
check: "/version"
- port: 8081
check: "/healthcheck?pretty=true"
when: docker | changed
---
- set_fact:
docker_host_local: "{{ docker_host | d('localhost') }}"
when: docker | changed
- set_fact:
real_port: "{{ docker.ansible_facts.docker_containers[0].NetworkSettings.Ports[(item.port | string)+'/tcp'][0].HostPort}}"
with_items: checks
register: real_ports
when: docker | changed
- debug: var=real_ports
when: debug is defined
- set_fact:
real_check: "http://{{ docker_host_local }}:{{ item.ansible_facts.real_port }}{{ item.item.check }}"
with_items: real_ports.results
register: real_checks
when: (docker | changed) and real_ports.results is defined
- debug: var=real_checks
when: debug is defined
- wait_for: host={{ docker_host_local }} port={{ item.ansible_facts.real_port }} timeout=30
with_items: real_ports.results
when: (docker | changed) and real_ports.results is defined
tags:
- wait_for_ports
- wait_for_service
- name: Wait until health check is ok
uri: url={{ item.ansible_facts.real_check }}
register: result
until: result.status is defined and result.status == 200
retries: 12
delay: 5
changed_when: false
with_items: real_checks.results
when: (docker | changed) and real_checks.results is defined
tags:
- wait_for_healthchecks
- wait_for_service
- debug: var=result
when: debug is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment