From 7edffc5771d671524587ca7d09dbe2eb55cbe372 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Mon, 6 Feb 2023 18:50:05 +0100 Subject: [PATCH] feat(mariadb): add ansible role for deployment --- roles/mariadb/defaults/main.yml | 32 ++++++++++++++++++++++++++++++++ roles/mariadb/tasks/main.yml | 20 ++++++++++++++++++++ roles/mariadb/vars/main.yml | 10 ++++++++++ 3 files changed, 62 insertions(+) create mode 100644 roles/mariadb/defaults/main.yml create mode 100644 roles/mariadb/tasks/main.yml create mode 100644 roles/mariadb/vars/main.yml diff --git a/roles/mariadb/defaults/main.yml b/roles/mariadb/defaults/main.yml new file mode 100644 index 0000000..864c220 --- /dev/null +++ b/roles/mariadb/defaults/main.yml @@ -0,0 +1,32 @@ +--- + +mariadb_version: "10.6.11" +mariadb_base_path: /var/lib/mariadb +mariadb_data_path: "{{ mariadb_base_path }}/{{ mariadb_version }}" + +mariadb_root_password: ~ +mariadb_database: ~ +mariadb_username: ~ +mariadb_password: ~ + +mariadb_container_base_environment: + MARIADB_ROOT_PASSWORD: "{{ mariadb_root_password }}" +mariadb_container_extra_environment: {} + +mariadb_container_name: mariadb +mariadb_container_image_name: docker.io/mariadb +mariadb_container_image_tag: ~ +mariadb_container_image: "{{ mariadb_container_image_name }}:{{ mariadb_container_image_tag | default(mariadb_version, true) }}" +mariadb_container_base_volumes: + - "{{ mariadb_data_path }}:{{ mariadb_container_data_path }}:z" +mariadb_container_extra_volumes: [] +mariadb_container_base_labels: + version: "{{ mariadb_version }}" +mariadb_container_extra_labels: {} +mariadb_container_restart_policy: "unless-stopped" +mariadb_container_environment: >-2 + {{ mariadb_container_base_environment + | combine(mariadb_container_database_environment + if (mariadb_database and mariadb_username and mariadb_password) + else {}, recursive=True) + | combine(mariadb_container_extra_environment) }} diff --git a/roles/mariadb/tasks/main.yml b/roles/mariadb/tasks/main.yml new file mode 100644 index 0000000..885f52c --- /dev/null +++ b/roles/mariadb/tasks/main.yml @@ -0,0 +1,20 @@ +--- +- name: Ensure mariaDB container image is present on host + community.docker.docker_image: + name: "{{ mariadb_container_image }}" + state: present + source: pull + +- name: Ensure mariaDB {{ mariadb_version }} is running as '{{ mariadb_container_name }}' + community.docker.docker_container: + name: "{{ mariadb_container_name }}" + image: "{{ mariadb_container_image }}" + env: "{{ mariadb_container_environment }}" + ports: "{{ mariadb_container_ports }}" + labels: "{{ mariadb_container_labels }}" + volumes: "{{ mariadb_container_volumes }}" + networks: "{{ mariadb_container_networks | default(omit, true) }}" + etc_hosts: "{{ mariadb_container_etc_hosts | default(omit, true) }}" + purge_networks: "{{ mariadb_container_purge_networks | default(omit, true) }}" + restart_policy: "{{ mariadb_container_restart_policy }}" + state: started diff --git a/roles/mariadb/vars/main.yml b/roles/mariadb/vars/main.yml new file mode 100644 index 0000000..b1bb083 --- /dev/null +++ b/roles/mariadb/vars/main.yml @@ -0,0 +1,10 @@ +--- + +mariadb_container_database_environment: + MARIADB_DATABASE: "{{ mariadb_database }}" + MARIADB_USER: "{{ mariadb_username }}" + MARIADB_PASSWORD: "{{ mariadb_password }}" + +mariadb_container_data_path: /var/lib/mysql +mariadb_container_volumes: "{{ mariadb_container_base_volumes + mariadb_container_extra_volumes }}" +mariadb_container_labels: "{{ mariadb_container_base_labels | combine(mariadb_container_extra_labels, recursive=True) }}"