Implemented draft for auto database credentials change moodle

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-17 06:31:55 +02:00
parent fd637c58e3
commit 84322f81ef
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
8 changed files with 77 additions and 8 deletions

View File

@ -46,7 +46,7 @@ ports:
web-app-openproject: 8023 web-app-openproject: 8023
gitlab: 8024 gitlab: 8024
web-app-akaunting: 8025 web-app-akaunting: 8025
moodle: 8026 web-app-moodle: 8026
taiga: 8027 taiga: 8027
friendica: 8028 friendica: 8028
web-app-port-ui: 8029 web-app-port-ui: 8029

View File

@ -46,7 +46,7 @@ defaults_networks:
# Use one of the last container ips for dns resolving so that it isn't used # Use one of the last container ips for dns resolving so that it isn't used
dns: 192.168.102.29 dns: 192.168.102.29
subnet: 192.168.102.16/28 subnet: 192.168.102.16/28
moodle: web-app-moodle:
subnet: 192.168.102.32/28 subnet: 192.168.102.32/28
web-app-mybb: web-app-mybb:
subnet: 192.168.102.48/28 subnet: 192.168.102.48/28

View File

@ -1,5 +1,4 @@
site_titel: "Academy on {{primary_domain}}" site_titel: "Academy on {{primary_domain}}"
version: "4.5" # Latest LTS - Necessary for OIDC
features: features:
matomo: true matomo: true
css: false css: false
@ -28,4 +27,12 @@ domains:
docker: docker:
services: services:
database: database:
enabled: true enabled: true
moodle:
version: "4.5" # Latest LTS - Necessary for OIDC
image: bitnami/moodle
name: moodle
volumes:
data:
name: moodle_data

View File

@ -0,0 +1,47 @@
- name: Check if config.php exists
command: docker exec --user root {{ moodle_name }} test -f {{ moodle_config }}
register: config_file_exists
changed_when: false
failed_when: false
- name: Backup config.php to host
when: config_file_exists.rc == 0
block:
- name: Create backup directory on host
ansible.builtin.file:
path: "/opt/docker/moodle/_backup"
state: directory
mode: "0755"
- name: Copy config.php from container to host
command: >
docker cp {{ moodle_name }}:{{ moodle_config }} /opt/docker/moodle/_backup/config.php.bak
- name: Check if config.php exists
command: docker exec --user root {{ moodle_name }} test -f {{ moodle_config }}
register: config_file_exists
changed_when: false
failed_when: false
- name: Patch Moodle config.php with updated DB credentials
when: config_file_exists.rc == 0
block:
- name: Update DB host
command: >
docker exec --user root {{ moodle_name }}
sed -i "s/^\$CFG->dbhost *= *.*/\$CFG->dbhost = '{{ database_host }}';/" {{ moodle_config }}
- name: Update DB name
command: >
docker exec --user root {{ moodle_name }}
sed -i "s/^\$CFG->dbname *= *.*/\$CFG->dbname = '{{ database_name }}';/" {{ moodle_config }}
- name: Update DB user
command: >
docker exec --user root {{ moodle_name }}
sed -i "s/^\$CFG->dbuser *= *.*/\$CFG->dbuser = '{{ database_username }}';/" {{ moodle_config }}
- name: Update DB password
command: >
docker exec --user root {{ moodle_name }}
sed -i "s/^\$CFG->dbpass *= *.*/\$CFG->dbpass = '{{ database_password }}';/" {{ moodle_config }}

View File

@ -3,6 +3,12 @@
include_role: include_role:
name: cmp-db-docker-proxy name: cmp-db-docker-proxy
- name: "Update database credentials"
include_tasks: database.yml
- name: flush docker service
meta: flush_handlers
- name: Wait until the Moodle container is healthy - name: Wait until the Moodle container is healthy
shell: docker inspect --format '{% raw %}{{.State.Health.Status}}{% endraw %}' {{ container_name }} shell: docker inspect --format '{% raw %}{{.State.Health.Status}}{% endraw %}' {{ container_name }}
register: health_check register: health_check

View File

@ -1,4 +1,4 @@
FROM bitnami/moodle:{{ applications | get_app_conf(application_id, 'version', True) }} FROM {{ moodle_image }}:{{ moodle_version }}
{% if applications | get_app_conf(application_id, 'features.oidc', False) %} {% if applications | get_app_conf(application_id, 'features.oidc', False) %}
RUN install_packages unzip curl jq \ RUN install_packages unzip curl jq \

View File

@ -19,7 +19,9 @@
{% include 'roles/docker-compose/templates/volumes.yml.j2' %} {% include 'roles/docker-compose/templates/volumes.yml.j2' %}
code: code:
name: {{ moodle_volume_code }}
data: data:
name: {{ moodle_volume_data }}
{% include 'roles/docker-compose/templates/networks.yml.j2' %} {% include 'roles/docker-compose/templates/networks.yml.j2' %}

View File

@ -1,11 +1,18 @@
--- ---
application_id: "moodle" application_id: "web-app-moodle"
database_type: "mariadb" database_type: "mariadb"
container_name: "{{ application_id }}"
bitnami_code_link: "/bitnami/moodle" bitnami_code_link: "/bitnami/moodle"
bitnami_code_dir: "/opt{{bitnami_code_link}}" bitnami_code_dir: "/opt{{bitnami_code_link}}"
bitnami_data_dir: "/bitnami/moodledata" bitnami_data_dir: "/bitnami/moodledata"
bitnami_oidc_plugin_dir: "{{ bitnami_code_dir }}/auth/oidc" bitnami_oidc_plugin_dir: "{{ bitnami_code_dir }}/auth/oidc"
bitnami_user: "daemon" bitnami_user: "daemon"
bitnami_user_group: "{{ bitnami_user }}:{{ bitnami_user }}" bitnami_user_group: "{{ bitnami_user }}:{{ bitnami_user }}"
docker_compose_flush_handlers: true
docker_compose_flush_handlers: false # Wait for env update
moodle_config: "/bitnami/moodle/config.php"
moodle_version: "{{ applications | get_app_conf(application_id, 'docker.services.moodle.version', True) }}"
moodle_image: "{{ applications | get_app_conf(application_id, 'docker.services.moodle.image', True) }}"
moodle_name: "{{ applications | get_app_conf(application_id, 'docker.services.moodle.name', True) }}"
moodle_volume: "{{ applications | get_app_conf(application_id, 'docker.volumes.data', True) }}"
moodle_code: "{{ applications | get_app_conf(application_id, 'docker.volumes.code', True) }}"