Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Last active June 20, 2019 21:35
Show Gist options
  • Select an option

  • Save alexesDev/f11337e94d94166ce8a46d47be1dd7ae to your computer and use it in GitHub Desktop.

Select an option

Save alexesDev/f11337e94d94166ce8a46d47be1dd7ae to your computer and use it in GitHub Desktop.
Deploy App+Postgres+Traefik Using Ansible
Bootstrap
ansible-galaxy install -p roles geerlingguy.docker
ansible-playbook site.yml
chmod +x deploy.sh
Deploy
./deploy.sh
[defaults]
inventory = hosts
pipelining = True
timeout = 30
#!/bin/sh
export TAG=`git rev-parse HEAD`
docker build -t registry.example.com:$TAG .
docker push registry.example.com:$TAG
ansible-playbook site.yml --tags install --extra-vars app_image_tag=$TAG
[web]
x.x.x.x # change me
[all:vars]
ansible_python_interpreter=/usr/bin/python3
- name: Install Docker
hosts: all
become: yes
tags: bootstrap
roles:
- geerlingguy.docker
tasks:
- name: Install pip
apt: name=python3-pip
- name: Install pip docker
pip: name=docker
- name: Create a docker network
docker_network:
name: projectname
- name: Install App
hosts: all
tags: install
gather_facts: no
become: true
vars:
app_image_tag: latest
app_image: "registry.example.com/app:{{ app_image_tag }}"
app_domain: example.com
tasks:
- name: Log into Registry
docker_login:
registry: registry.example.com
username: xxxxx
password: xxxxx
- name: Copy traefik.ini
tags: traefik
copy:
src: traefik.toml
dest: /etc/traefik.toml
- name: Run traefik
tags: traefik
docker_container:
name: traefik
image: traefik:v1.7.12
restart_policy: always
networks:
- name: projectname
ports:
- 80:80
- 443:443
volumes:
- /etc/traefik.toml:/etc/traefik/traefik.toml
- /var/run/docker.sock:/var/run/docker.sock
- /srv/traefik:/data
- name: Run postgres
tags: postgres
docker_container:
name: projectname-postgres
image: postgres:10.7
restart_policy: always
env:
POSTGRES_USER: projectname
POSTGRES_PASSWORD: UVGZt4ZhpmqhfDbep7X0XHSkk3Nc
networks:
- name: projectname
volumes:
- /srv/projectname-postgres:/var/lib/postgresql/data
- name: Run postgres
tags: app
docker_container:
name: projectname-app
image: "{{ app_image }}"
restart_policy: always
env:
DATABASE_URL: postgres://projectname:UVGZt4ZhpmqhfDbep7X0XHSkk3Nc@postgres/projectname
links:
- projectname-postgres:postgres
networks:
- name: projectname
labels:
traefik.enable: "true"
traefik.frontend.rule: "Host:{{ app_domain }}"
traefik.port: "3000"
defaultEntryPoints = ["http", "https"]
[docker]
[acme]
email = "[email protected]" # change me
storage = "/data/acme.json"
entryPoint = "https"
acmeLogging = true
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment