Skip to content

Commit 541765b

Browse files
committed
Merge #420: fix: [#409] replace hardcoded /opt/torrust paths with deploy_dir variable in Ansible templates
7365df3 fix: [#409] replace hardcoded /opt/torrust paths with deploy_dir variable in Ansible templates (Jose Celano) Pull request description: ## Summary Fixes #409 — Several Ansible playbook templates under `templates/ansible/` were hardcoding the deployment directory as `/opt/torrust` instead of using the `{{ deploy_dir }}` variable from `variables.yml`. This caused deployment failures when a custom deployment directory was configured. ## Changes ### Phase 1: Fix fully hardcoded templates All the following templates had `vars_files: [variables.yml]` added and all hardcoded `/opt/torrust` paths replaced with `{{ deploy_dir }}`: - `templates/ansible/create-tracker-storage.yml` — 3 loop items - `templates/ansible/create-prometheus-storage.yml` — 1 loop item - `templates/ansible/create-backup-storage.yml` — 2 `path:` params - `templates/ansible/deploy-backup-config.yml` — 4 `dest:` and `path:` params - `templates/ansible/deploy-tracker-config.yml` — 2 `dest:` and `path:` params - `templates/ansible/deploy-prometheus-config.yml` — 2 `dest:` and `path:` params - `templates/ansible/init-tracker-database.yml` — 2 `path:` params - `templates/ansible/deploy-caddy-config.yml` — 6 loop items + `dest:` + `path:` ### Phase 2: Fix inline-variable templates - `templates/ansible/run-compose-services.yml` — Removed inline `vars: deploy_dir: /opt/torrust`; added `vars_files: [variables.yml]` - `templates/ansible/deploy-compose-files.yml` — Removed inline `vars: remote_deploy_dir: /opt/torrust`; added `vars_files: [variables.yml]`; renamed all `remote_deploy_dir` references to `deploy_dir` ### Phase 3: Update comments Updated inline comments in modified templates to reference `{{ deploy_dir }}` instead of `/opt/torrust` example paths. ## Acceptance Criteria - [x] No playbook task in `templates/ansible/` uses a hardcoded `/opt/torrust` path in `path:`, `dest:`, or loop items - [x] All playbooks that reference the deployment directory load `variables.yml` via `vars_files` - [x] The variable name `deploy_dir` is used consistently (no `remote_deploy_dir` or other aliases) - [x] Playbooks that previously had inline `vars:` blocks for the deploy directory no longer define it inline - [x] The default behavior (with `deploy_dir: /opt/torrust`) is unchanged - [x] YAML linter passes (`cargo run --bin linter yaml`) - [x] Markdown linter passes - [x] Spell checker passes ACKs for top commit: josecelano: ACK 7365df3 Tree-SHA512: e44ea4fb8361f3a53c2d6d397ed6a21bcc33dd9958d4f9d62df745de82f941517d04d97f2617c44f724ad6410a571c15ffeda5038d9c93344a1908d5376ae4d1
2 parents 474486b + 7365df3 commit 541765b

10 files changed

Lines changed: 57 additions & 40 deletions

templates/ansible/create-backup-storage.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,31 @@
2121
# The directories are created with appropriate permissions and ownership.
2222
#
2323
# Directory Structure:
24-
# /opt/torrust/storage/backup/
24+
# {{ deploy_dir }}/storage/backup/
2525
# └── etc/ # Backup configuration files
2626
#
2727
# Variables:
28+
# - deploy_dir: Base deployment directory (from variables.yml)
2829
# - ansible_user: The SSH user for the remote host (set automatically)
2930

3031
- name: Create Backup storage directories
3132
hosts: all
3233
become: true
34+
vars_files:
35+
- variables.yml
3336

3437
tasks:
3538
- name: Create backup configuration directory
3639
ansible.builtin.file:
37-
path: /opt/torrust/storage/backup/etc
40+
path: "{{ deploy_dir }}/storage/backup/etc"
3841
state: directory
3942
mode: "0755"
4043
owner: "{{ ansible_user }}"
4144
group: "{{ ansible_user }}"
4245

4346
- name: Verify backup configuration directory exists
4447
ansible.builtin.stat:
45-
path: /opt/torrust/storage/backup/etc
48+
path: "{{ deploy_dir }}/storage/backup/etc"
4649
register: backup_etc_dir
4750

4851
- name: Assert backup directories were created

templates/ansible/create-prometheus-storage.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- name: Create Prometheus storage directories
2121
hosts: all
2222
become: true
23+
vars_files:
24+
- variables.yml
2325

2426
tasks:
2527
- name: Create Prometheus directory structure
@@ -30,4 +32,4 @@
3032
owner: "{{ ansible_user }}"
3133
group: "{{ ansible_user }}"
3234
loop:
33-
- /opt/torrust/storage/prometheus/etc
35+
- "{{ deploy_dir }}/storage/prometheus/etc"

templates/ansible/create-tracker-storage.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- name: Create Tracker storage directories
2121
hosts: all
2222
become: true
23+
vars_files:
24+
- variables.yml
2325

2426
tasks:
2527
- name: Create Tracker directory structure
@@ -30,6 +32,6 @@
3032
owner: "{{ ansible_user }}"
3133
group: "{{ ansible_user }}"
3234
loop:
33-
- /opt/torrust/storage/tracker/etc
34-
- /opt/torrust/storage/tracker/lib/database
35-
- /opt/torrust/storage/tracker/log
35+
- "{{ deploy_dir }}/storage/tracker/etc"
36+
- "{{ deploy_dir }}/storage/tracker/lib/database"
37+
- "{{ deploy_dir }}/storage/tracker/log"

templates/ansible/deploy-backup-config.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,34 @@
3232
- name: Deploy Backup configuration
3333
hosts: all
3434
become: true
35+
vars_files:
36+
- variables.yml
3537

3638
tasks:
3739
- name: Copy backup.conf to VM
3840
ansible.builtin.copy:
3941
src: "{{ playbook_dir }}/../backup/etc/backup.conf"
40-
dest: /opt/torrust/storage/backup/etc/backup.conf
42+
dest: "{{ deploy_dir }}/storage/backup/etc/backup.conf"
4143
mode: "0644"
4244
owner: "{{ ansible_user }}"
4345
group: "{{ ansible_user }}"
4446

4547
- name: Copy backup-paths.txt to VM
4648
ansible.builtin.copy:
4749
src: "{{ playbook_dir }}/../backup/etc/backup-paths.txt"
48-
dest: /opt/torrust/storage/backup/etc/backup-paths.txt
50+
dest: "{{ deploy_dir }}/storage/backup/etc/backup-paths.txt"
4951
mode: "0644"
5052
owner: "{{ ansible_user }}"
5153
group: "{{ ansible_user }}"
5254

5355
- name: Verify backup.conf exists
5456
ansible.builtin.stat:
55-
path: /opt/torrust/storage/backup/etc/backup.conf
57+
path: "{{ deploy_dir }}/storage/backup/etc/backup.conf"
5658
register: backup_conf
5759

5860
- name: Verify backup-paths.txt exists
5961
ansible.builtin.stat:
60-
path: /opt/torrust/storage/backup/etc/backup-paths.txt
62+
path: "{{ deploy_dir }}/storage/backup/etc/backup-paths.txt"
6163
register: backup_paths
6264

6365
- name: Assert backup configuration files were deployed

templates/ansible/deploy-caddy-config.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
# - ansible_user: The SSH user for the remote host (set automatically)
2929
#
3030
# Storage Directories:
31-
# - /opt/torrust/storage/caddy/etc/ - Caddyfile configuration
32-
# - /opt/torrust/storage/caddy/data/ - Caddy data (certificates, etc.)
33-
# - /opt/torrust/storage/caddy/config/ - Caddy config state
31+
# - {{ deploy_dir }}/storage/caddy/etc/ - Caddyfile configuration
32+
# - {{ deploy_dir }}/storage/caddy/data/ - Caddy data (certificates, etc.)
33+
# - {{ deploy_dir }}/storage/caddy/config/ - Caddy config state
3434

3535
- name: Deploy Caddy configuration
3636
hosts: all
3737
become: true
38+
vars_files:
39+
- variables.yml
3840

3941
tasks:
4042
- name: Create Caddy storage directories
@@ -45,23 +47,23 @@
4547
owner: "{{ ansible_user }}"
4648
group: "{{ ansible_user }}"
4749
loop:
48-
- /opt/torrust/storage/caddy
49-
- /opt/torrust/storage/caddy/etc
50-
- /opt/torrust/storage/caddy/data
51-
- /opt/torrust/storage/caddy/config
50+
- "{{ deploy_dir }}/storage/caddy"
51+
- "{{ deploy_dir }}/storage/caddy/etc"
52+
- "{{ deploy_dir }}/storage/caddy/data"
53+
- "{{ deploy_dir }}/storage/caddy/config"
5254

5355
- name: Copy Caddyfile to VM
5456
ansible.builtin.copy:
5557
src: "{{ playbook_dir }}/../caddy/Caddyfile"
5658
# Note: This is the host path. Inside the container, it's mounted to /etc/caddy/Caddyfile
57-
dest: /opt/torrust/storage/caddy/etc/Caddyfile
59+
dest: "{{ deploy_dir }}/storage/caddy/etc/Caddyfile"
5860
mode: "0644"
5961
owner: "{{ ansible_user }}"
6062
group: "{{ ansible_user }}"
6163

6264
- name: Verify Caddy configuration file exists
6365
ansible.builtin.stat:
64-
path: /opt/torrust/storage/caddy/etc/Caddyfile
66+
path: "{{ deploy_dir }}/storage/caddy/etc/Caddyfile"
6567
register: caddy_config
6668

6769
- name: Assert Caddy configuration was deployed

templates/ansible/deploy-compose-files.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121
hosts: all
2222
gather_facts: false
2323
become: true
24+
vars_files:
25+
- variables.yml
2426

2527
vars:
26-
remote_deploy_dir: /opt/torrust
2728
local_compose_dir: "{{ compose_files_source_dir }}"
2829

2930
tasks:
3031
- name: 📦 Starting Docker Compose files deployment
3132
ansible.builtin.debug:
32-
msg: "🚀 Deploying Docker Compose files to {{ inventory_hostname }}:{{ remote_deploy_dir }}"
33+
msg: "🚀 Deploying Docker Compose files to {{ inventory_hostname }}:{{ deploy_dir }}"
3334

3435
- name: Ensure remote deployment directory exists
3536
ansible.builtin.file:
36-
path: "{{ remote_deploy_dir }}"
37+
path: "{{ deploy_dir }}"
3738
state: directory
3839
mode: "0755"
3940
owner: "{{ ansible_user }}"
@@ -42,25 +43,25 @@
4243
- name: Copy Docker Compose files to remote host
4344
ansible.builtin.copy:
4445
src: "{{ local_compose_dir }}/"
45-
dest: "{{ remote_deploy_dir }}/"
46+
dest: "{{ deploy_dir }}/"
4647
mode: "0640"
4748
directory_mode: "0755"
4849
owner: "{{ ansible_user }}"
4950
group: "{{ ansible_user }}"
5051

5152
- name: Verify docker-compose.yml exists on remote
5253
ansible.builtin.stat:
53-
path: "{{ remote_deploy_dir }}/docker-compose.yml"
54+
path: "{{ deploy_dir }}/docker-compose.yml"
5455
register: compose_file_check
5556

5657
- name: Fail if docker-compose.yml was not deployed
5758
ansible.builtin.fail:
58-
msg: "docker-compose.yml was not found at {{ remote_deploy_dir }}/docker-compose.yml after deployment"
59+
msg: "docker-compose.yml was not found at {{ deploy_dir }}/docker-compose.yml after deployment"
5960
when: not compose_file_check.stat.exists
6061

6162
- name: List deployed files
6263
ansible.builtin.find:
63-
paths: "{{ remote_deploy_dir }}"
64+
paths: "{{ deploy_dir }}"
6465
file_type: file
6566
recurse: true
6667
register: deployed_files
@@ -69,8 +70,8 @@
6970
ansible.builtin.debug:
7071
msg: |
7172
✅ Docker Compose files deployed successfully!
72-
📁 Destination: {{ remote_deploy_dir }}
73+
📁 Destination: {{ deploy_dir }}
7374
📄 Files deployed: {{ deployed_files.files | length }}
7475
{% for file in deployed_files.files %}
75-
- {{ file.path | regex_replace(remote_deploy_dir ~ '/', '') }}
76+
- {{ file.path | regex_replace(deploy_dir ~ '/', '') }}
7677
{% endfor %}

templates/ansible/deploy-prometheus-config.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@
3131
- name: Deploy Prometheus configuration
3232
hosts: all
3333
become: true
34+
vars_files:
35+
- variables.yml
3436

3537
tasks:
3638
- name: Copy prometheus.yml to VM
3739
ansible.builtin.copy:
3840
src: "{{ playbook_dir }}/../prometheus/prometheus.yml"
3941
# Note: This is the host path. Inside the container, it's mounted to /etc/prometheus/
40-
dest: /opt/torrust/storage/prometheus/etc/prometheus.yml
42+
dest: "{{ deploy_dir }}/storage/prometheus/etc/prometheus.yml"
4143
mode: "0644"
4244
owner: "{{ ansible_user }}"
4345
group: "{{ ansible_user }}"
4446

4547
- name: Verify Prometheus configuration file exists
4648
ansible.builtin.stat:
47-
path: /opt/torrust/storage/prometheus/etc/prometheus.yml
49+
path: "{{ deploy_dir }}/storage/prometheus/etc/prometheus.yml"
4850
register: prometheus_config
4951

5052
- name: Assert Prometheus configuration was deployed

templates/ansible/deploy-tracker-config.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@
3131
- name: Deploy Tracker configuration
3232
hosts: all
3333
become: true
34+
vars_files:
35+
- variables.yml
3436

3537
tasks:
3638
- name: Copy tracker.toml to VM
3739
ansible.builtin.copy:
3840
src: "{{ playbook_dir }}/../tracker/tracker.toml"
3941
# Note: This is the host path. Inside the container, it's mounted to /var/lib/torrust/tracker/etc/
40-
dest: /opt/torrust/storage/tracker/etc/tracker.toml
42+
dest: "{{ deploy_dir }}/storage/tracker/etc/tracker.toml"
4143
mode: "0644"
4244
owner: "{{ ansible_user }}"
4345
group: "{{ ansible_user }}"
4446

4547
- name: Verify tracker configuration file exists
4648
ansible.builtin.stat:
47-
path: /opt/torrust/storage/tracker/etc/tracker.toml
49+
path: "{{ deploy_dir }}/storage/tracker/etc/tracker.toml"
4850
register: tracker_config
4951

5052
- name: Assert tracker configuration was deployed

templates/ansible/init-tracker-database.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@
2222
#
2323
# Requirements:
2424
# - The tracker storage directories must exist
25-
# - The ansible_user must have write access to /opt/torrust/storage/tracker/lib/database/
25+
# - The ansible_user must have write access to {{ deploy_dir }}/storage/tracker/lib/database/
2626
#
2727
# Variables:
2828
# - ansible_user: The user that will own the database file (default: current user)
2929
#
3030
# Creates:
31-
# - /opt/torrust/storage/tracker/lib/database/tracker.db (SQLite database file)
31+
# - {{ deploy_dir }}/storage/tracker/lib/database/tracker.db (SQLite database file)
3232

3333
- name: Initialize Tracker Database
3434
hosts: all
3535
become: true
36+
vars_files:
37+
- variables.yml
3638
tasks:
3739
- name: Create empty SQLite database file
3840
ansible.builtin.file:
39-
path: /opt/torrust/storage/tracker/lib/database/tracker.db
41+
path: "{{ deploy_dir }}/storage/tracker/lib/database/tracker.db"
4042
state: touch
4143
owner: "{{ ansible_user }}"
4244
group: "{{ ansible_user }}"
@@ -46,7 +48,7 @@
4648

4749
- name: Verify database file exists
4850
ansible.builtin.stat:
49-
path: /opt/torrust/storage/tracker/lib/database/tracker.db
51+
path: "{{ deploy_dir }}/storage/tracker/lib/database/tracker.db"
5052
register: db_file
5153

5254
- name: Assert database file was created

templates/ansible/run-compose-services.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
hosts: all
2222
gather_facts: false
2323
become: true
24-
25-
vars:
26-
deploy_dir: /opt/torrust
24+
vars_files:
25+
- variables.yml
2726

2827
tasks:
2928
- name: 🚀 Starting Docker Compose services

0 commit comments

Comments
 (0)