From baa5bae5f234f10162f47ad9909d0697305b52fe Mon Sep 17 00:00:00 2001 From: gabriel becker Date: Sat, 23 Sep 2023 00:07:31 +1000 Subject: [PATCH] Working playbook for docker compose --- .gitignore | 2 ++ README.md | 18 ++++++++++ ansible.cfg | 5 +++ group_vars/server/vars.yml | 10 ++++++ inventory.yml | 8 +++++ requirements.yml | 3 ++ roles/webserver/tasks/dependencies.yml | 10 ++++++ roles/webserver/tasks/install_docker.yml | 46 ++++++++++++++++++++++++ roles/webserver/tasks/main.yml | 5 +++ roles/webserver/tasks/nginx_config.yml | 21 +++++++++++ roles/webserver/tasks/start_service.yml | 11 ++++++ run.yml | 5 +++ 12 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 ansible.cfg create mode 100644 group_vars/server/vars.yml create mode 100644 inventory.yml create mode 100644 requirements.yml create mode 100644 roles/webserver/tasks/dependencies.yml create mode 100644 roles/webserver/tasks/install_docker.yml create mode 100644 roles/webserver/tasks/main.yml create mode 100644 roles/webserver/tasks/nginx_config.yml create mode 100644 roles/webserver/tasks/start_service.yml create mode 100644 run.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1d6aa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +.vscode diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb7f2d8 --- /dev/null +++ b/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 `/docker-compose.yml` and optionally include `/.env` files + - **services**: list of services in the service_dir container all `` folders + - **packages**: utils packages for linxu environment + + + diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..3e5885a --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,5 @@ +[defaults] +INVENTORY = inventory.yml + +[ssh_connections] +pipelining = true \ No newline at end of file diff --git a/group_vars/server/vars.yml b/group_vars/server/vars.yml new file mode 100644 index 0000000..98b423d --- /dev/null +++ b/group_vars/server/vars.yml @@ -0,0 +1,10 @@ +packages: + - git + - htop +services: + - pinry + - immich + - monica + - piwigo + - vikunja +services_dir: /home/gabriel/hosted \ No newline at end of file diff --git a/inventory.yml b/inventory.yml new file mode 100644 index 0000000..69913bd --- /dev/null +++ b/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 % diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..e65176c --- /dev/null +++ b/requirements.yml @@ -0,0 +1,3 @@ +--- +roles: + - name: community.docker.docker_compose \ No newline at end of file diff --git a/roles/webserver/tasks/dependencies.yml b/roles/webserver/tasks/dependencies.yml new file mode 100644 index 0000000..b02a2cd --- /dev/null +++ b/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 diff --git a/roles/webserver/tasks/install_docker.yml b/roles/webserver/tasks/install_docker.yml new file mode 100644 index 0000000..9a2d51a --- /dev/null +++ b/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 diff --git a/roles/webserver/tasks/main.yml b/roles/webserver/tasks/main.yml new file mode 100644 index 0000000..92cedbb --- /dev/null +++ b/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 \ No newline at end of file diff --git a/roles/webserver/tasks/nginx_config.yml b/roles/webserver/tasks/nginx_config.yml new file mode 100644 index 0000000..1390026 --- /dev/null +++ b/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/" diff --git a/roles/webserver/tasks/start_service.yml b/roles/webserver/tasks/start_service.yml new file mode 100644 index 0000000..a0ead62 --- /dev/null +++ b/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 }}" diff --git a/run.yml b/run.yml new file mode 100644 index 0000000..db5166b --- /dev/null +++ b/run.yml @@ -0,0 +1,5 @@ +--- +- hosts: server + become: yes + roles: + - role: webserver