Refactor docker swarm configuration to separate role

This commit is contained in:
Kacper Donat 2021-03-12 19:11:08 +01:00
parent 46f99a7fd4
commit a90296fa41
17 changed files with 138 additions and 38 deletions

View File

@ -9,6 +9,7 @@
roles:
- geerlingguy.docker
- geerlingguy.pip
- kadet.docker-swarm
tasks:
- name: "Add '{{ ansible_user }}' to docker group"
@ -16,17 +17,6 @@
user: "{{ ansible_user }}"
groups: docker
append: yes
- name: Init a new swarm with default parameters
community.docker.docker_swarm:
state: present
advertise_addr: "{{ swarm_addr|default(omit) }}"
- name: Init docker network for ingress
community.docker.docker_network:
name: traefik
scope: swarm
driver: overlay
- name: Deploy services
include_tasks: tasks/deploy.yml

View File

@ -0,0 +1,29 @@
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

View File

@ -0,0 +1,2 @@
---
swarm_global_networks: []

View File

@ -0,0 +1,2 @@
---
# handlers file for kadet.docker-swarm

View File

@ -0,0 +1,7 @@
galaxy_info:
author: Kacper Donat <kacper@kadet.net>
description: Docker Swarm role
license: MIT
min_ansible_version: 2.10
dependencies: []

View File

@ -0,0 +1,12 @@
---
- name: Init a new swarm with default parameters
community.docker.docker_swarm:
state: present
advertise_addr: "{{ swarm_addr|default(omit) }}"
- name: Init global networks for swarm
community.docker.docker_network:
name: "{{ item.name }}"
driver: "{{ item.driver|default('overlay') }}"
scope: swarm
loop: "{{ swarm_global_networks }}"

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- kadet.docker-swarm

View File

@ -0,0 +1,2 @@
---
# vars file for kadet.docker-swarm

View File

@ -7,7 +7,7 @@ services:
labels:
- traefik.enable=true
- traefik.http.routers.{{ service }}.rule=Host(`kadet.local`) && PathPrefix(`/hello/`)
- traefik.http.services.{{ service }}.loadbalancer.server.port=8080
- traefik.http.routers.{{ service }}.middlewares={{ service }}-strip
- traefik.http.services.{{ service }}.loadbalancer.server.port=8080
- traefik.http.middlewares.{{ service }}-strip.stripprefix.prefixes=/hello/
networks: ['traefik']

View File

@ -7,8 +7,9 @@ services:
- 80:80
- 443:443
volumes:
- ./config/traefik.yaml:/etc/traefik/traefik.yaml
- /var/run/docker.sock:/var/run/docker.sock
- ./config/traefik.yaml:/etc/traefik/traefik.yaml:ro
- ./config/dynamic:/etc/traefik/dynamic:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
deploy:
placement:
constraints:

View File

@ -0,0 +1,11 @@
---
- name: 'Copy static config for "{{ service }}"'
copy:
src: "{{ service_path }}/config/traefik.yaml"
dest: "{{ remote_service_path }}/config/traefik.yaml"
- name: 'Ensure dynamic config directory exists'
file:
path: "{{ remote_service_path }}/config/dynamic"
state: directory
owner: "{{ ansible_user }}"

View File

@ -7,37 +7,28 @@
- name: 'Ensure "{{ service }}" service config directory exist'
file:
path: "{{ remote_services_root }}/{{ service }}"
path: "{{ remote_service_path }}/config"
state: directory
recurse: true
owner: "{{ ansible_user }}"
- name: 'Ensure "{{ service }}" service config directory exist'
file:
path: "{{ remote_services_root }}/{{ service }}/config"
state: directory
recurse: true
owner: "{{ ansible_user }}"
- name: 'Synchronise "{{ service }}" configuration'
template:
src: "{{ services_root }}/{{ service }}/stack.yml"
dest: "{{ remote_services_root }}/{{ service }}/stack.yml"
- name: 'Include vars for "{{ service }}" service'
copy:
src: "{{ file }}"
dest: "{{ remote_services_root }}/{{ file | replace(services_root, '') }}"
with_fileglob: "{{ services_root }}/{{ service }}/config/*"
- name: 'Load configuration tasks for "{{ service }}"'
include_tasks: "{{ file }}"
with_fileglob: "{{ services_root }}/{{ service }}/tasks/*"
loop_control:
loop_var: file
- name: 'Deploy "{{ service }}" docker stack'
- name: 'Copy stack file of "{{ service }}" to remote server'
template:
src: "{{ services_root }}/{{ service }}/stack.yml"
dest: "{{ remote_service_path }}/stack.yml"
- name: 'Deploy "{{ service }}" stack to docker'
community.docker.docker_stack:
name: "{{ service }}"
prune: yes
compose:
- "{{ remote_services_root }}/{{ service }}/stack.yml"
- version: "{{ compose_version }}"
networks:
traefik:
external: true
- "{{ remote_service_path }}/stack.yml"
- version: "{{ compose_version }}"
networks: "{{ dict(swarm_global_networks | map(attribute='name') | zip_longest([], fillvalue={ 'external': True })) }}"

View File

@ -7,3 +7,7 @@ pip_install_packages:
ansible_python_interpreter: /usr/bin/python3
pip_package: python3-pip
pip_executable: pip3
swarm_addr: eth0
swarm_global_networks:
- name: traefik

View File

@ -1,6 +1,10 @@
---
services_root: "{{ './services' | realpath }}"
service_path: "{{ services_root }}/{{ service }}"
services: "{{ lookup('pipe', 'ls -1 ' + services_root).split('\n') | list }}"
remote_services_root: /var/services
remote_service_path: "{{ remote_services_root }}/{{ service }}"
compose_version: "3.7"