Browse Source

Working playbook for docker compose

main
gabriel becker 1 year ago
commit
baa5bae5f2
  1. 2
      .gitignore
  2. 18
      README.md
  3. 5
      ansible.cfg
  4. 10
      group_vars/server/vars.yml
  5. 8
      inventory.yml
  6. 3
      requirements.yml
  7. 10
      roles/webserver/tasks/dependencies.yml
  8. 46
      roles/webserver/tasks/install_docker.yml
  9. 5
      roles/webserver/tasks/main.yml
  10. 21
      roles/webserver/tasks/nginx_config.yml
  11. 11
      roles/webserver/tasks/start_service.yml
  12. 5
      run.yml

2
.gitignore vendored

@ -0,0 +1,2 @@
.venv
.vscode

18
README.md

@ -0,0 +1,18 @@
# Ansible Server
Ansible playbook to setup docker compose and start arbitraty number of compose stacks by using group_variables.
This playbook was tested against a debian 12 image.
## Usage
```bash
ansible-playbook run.yml
```
## Variables
Should be defined in `group_vars/server/vars.yml`:
- **services_dir**: fodler path containing all folders with docker-compose stacks. they should have the scructure `<service>/docker-compose.yml` and optionally include `<service>/.env` files
- **services**: list of services in the service_dir container all `<service>` folders
- **packages**: utils packages for linxu environment

5
ansible.cfg

@ -0,0 +1,5 @@
[defaults]
INVENTORY = inventory.yml
[ssh_connections]
pipelining = true

10
group_vars/server/vars.yml

@ -0,0 +1,10 @@
packages:
- git
- htop
services:
- pinry
- immich
- monica
- piwigo
- vikunja
services_dir: /home/gabriel/hosted

8
inventory.yml

@ -0,0 +1,8 @@
server:
hosts:
ec2-13-239-10-136.ap-southeast-2.compute.amazonaws.com:
vars:
ansible_user: admin
ansible_ssh_private_key_file: /home/gabriel/dev/terraform/terraform-lab/ec2ml/my_ssh.pem
PERSISTENT_DIRECTORY: /persistent
VOLATILE_DIRECTORY: /volatile %

3
requirements.yml

@ -0,0 +1,3 @@
---
roles:
- name: community.docker.docker_compose

10
roles/webserver/tasks/dependencies.yml

@ -0,0 +1,10 @@
---
- name: Update packages
apt:
update_cache: yes
upgrade: yes
- name: Install essential packages
package:
name: "{{ packages }}"
state: latest

46
roles/webserver/tasks/install_docker.yml

@ -0,0 +1,46 @@
---
- name: Install aptitude
apt:
name: aptitude
state: latest
update_cache: true
- name: Install required system packages
apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
state: latest
update_cache: true
- name: Add Docker GPG apt Key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker Repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu jammy stable
state: present
- name: Update apt and install docker-ce
apt:
name: docker-ce
state: latest
update_cache: true
- name: Update apt and install docker-compose
apt:
name: docker-compose
state: latest
update_cache: true
- name: Install Docker Module for Python
ansible.builtin.pip:
name: docker
extra_args: --break-system-packages

5
roles/webserver/tasks/main.yml

@ -0,0 +1,5 @@
---
- include_tasks: dependencies.yml
- include_tasks: install_docker.yml
- include_tasks: start_service.yml
# - include_tasks: nginx_config.yml

21
roles/webserver/tasks/nginx_config.yml

@ -0,0 +1,21 @@
---
- name: Copy service folder
copy:
src: "{{ services_dir }}/nginx"
dest: /services/
- name: Copy nginx subfolder file
copy:
src: "{{ item }}/nginx.conf"
dest: "/nginx.../{{ item }}.config.conf"
with_items: "{{ services }}"
- name: Copy service folder
copy:
src: "{{ item }}/docker-compose.yml"
dest: /tmp/docker-compose.yml
with_items: "{{ services }}"
- name: Start Docker compose
community.docker.docker_compose:
project_src: "/services/nginx/"

11
roles/webserver/tasks/start_service.yml

@ -0,0 +1,11 @@
---
- name: Copy service folder
ansible.posix.synchronize:
src: "{{ services_dir }}/{{ item }}"
dest: /services/
with_items: "{{ services }}"
- name: Start Docker compose
community.docker.docker_compose:
project_src: "/services/{{ item }}"
with_items: "{{ services }}"

5
run.yml

@ -0,0 +1,5 @@
---
- hosts: server
become: yes
roles:
- role: webserver
Loading…
Cancel
Save