Add option to deploy external docker-compose stack

This commit is contained in:
Kris Lamoureux 2023-07-20 03:51:44 -04:00
parent 56aee460ad
commit a635c7aa48
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925
3 changed files with 70 additions and 1 deletions

View File

@ -1,3 +1,6 @@
docker_compose_root: /var/lib/compose
docker_compose: /usr/bin/docker-compose
docker_compose_service: compose
docker_compose: /usr/bin/docker-compose
docker_repos_keys: "{{ docker_repos_path }}/.keys"
docker_repos_keytype: rsa
docker_repos_path: /srv/compose_repos

View File

@ -17,6 +17,57 @@
mode: 0400
notify: compose_systemd
- name: Create directories to clone docker-compose repositories
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0400
loop:
- "{{ docker_repos_path }}"
- "{{ docker_repos_keys }}"
when: docker_compose_deploy is defined
- name: Generate OpenSSH deploy keys for docker-compose clones
community.crypto.openssh_keypair:
path: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
type: "{{ docker_repos_keytype }}"
mode: 0400
state: present
- name: Clone external docker-compose projects
ansible.builtin.git:
repo: "{{ item.url }}"
dest: "{{ docker_repos_path }}/{{ item.name }}"
version: "{{ item.version | default('main') }}"
force: true
key_file: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
when: docker_compose_deploy is defined
loop: "{{ docker_compose_deploy }}"
- name: Create directories for docker-compose projects using the systemd service
ansible.builtin.file:
path: "{{ docker_compose_root }}/{{ item.name }}"
state: directory
mode: 0400
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined
- name: Copy docker-compose.yml files to their service directories
ansible.builtin.copy:
src: "{{ docker_repos_path }}/{{ item.name }}/{{ item.path | default('docker-compose.yml') }}"
dest: "{{ docker_compose_root }}/{{ item.name }}/docker-compose.yml"
remote_src: yes
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined
- name: Set environment variables for docker-compose projects
ansible.builtin.template:
src: docker-compose-env.j2
dest: "{{ docker_compose_root }}/{{ item.name }}/.env"
mode: 0400
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined and item.env is defined
- name: Add users to docker group
ansible.builtin.user:
name: "{{ item }}"
@ -30,3 +81,11 @@
name: docker
state: started
enabled: true
- name: Start docker-compose services and enable on boot
ansible.builtin.service:
name: "{{ docker_compose_service }}@{{ item.name }}"
state: started
enabled: true
loop: "{{ docker_compose_deploy }}"
when: item.enabled is defined and item.enabled is true

View File

@ -0,0 +1,7 @@
# {{ ansible_managed }}
{% if item.env is defined %}
{% for kvpair in item.env.items() %}
{{ kvpair.0 }}={{ kvpair.1 }}
{% endfor %}
{% endif %}