You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/contributing/templates.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -274,6 +274,72 @@ When adding a static Ansible playbook:
274
274
- [ ] Create application step to execute the playbook
275
275
- [ ] Verify playbook appears in `build/` directory during execution
276
276
277
+
## 🎯 Using Centralized Variables in Ansible Playbooks
278
+
279
+
When creating new Ansible playbooks that need dynamic variables (ports, paths, etc.), use the **centralized variables pattern** instead of creating new Tera templates.
280
+
281
+
### DO ✅
282
+
283
+
**Add variables to `templates/ansible/variables.yml.tera`:**
284
+
285
+
```yaml
286
+
# System Configuration
287
+
ssh_port: {{ ssh_port }}
288
+
my_service_port: {{ my_service_port }} # ← Add your new variable
289
+
```
290
+
291
+
**Reference variables in static playbook using `vars_files`:**
292
+
293
+
```yaml
294
+
# templates/ansible/my-new-service.yml (static playbook, no .tera extension)
295
+
---
296
+
- name: Configure My Service
297
+
hosts: all
298
+
vars_files:
299
+
- variables.yml # Load centralized variables
300
+
301
+
tasks:
302
+
- name: Configure service port
303
+
ansible.builtin.lineinfile:
304
+
path: /etc/myservice/config
305
+
line: "port={{ my_service_port }}"
306
+
```
307
+
308
+
**Register playbook in `copy_static_templates()` method:**
309
+
310
+
```rust
311
+
for playbook in &[
312
+
"update-apt-cache.yml",
313
+
"install-docker.yml",
314
+
"my-new-service.yml", // ← Add here
315
+
] {
316
+
// ...
317
+
}
318
+
```
319
+
320
+
### DON'T ❌
321
+
322
+
- ❌ Create a new `.tera` template for the playbook
323
+
- ❌ Create a new renderer/wrapper/context for each playbook
324
+
- ❌ Add variables directly in `inventory.yml.tera` (unless inventory-specific)
325
+
326
+
### Benefits
327
+
328
+
1. **Minimal Code**: No Rust boilerplate (renderer, wrapper, context) needed
329
+
2. **Centralized Management**: All variables in one place
330
+
3. **Runtime Resolution**: Variables resolved by Ansible, not at template rendering
331
+
4. **Easy Maintenance**: Adding new variables requires minimal changes
332
+
333
+
### When to Create a New Tera Template
334
+
335
+
Only create a new `.tera` template if:
336
+
337
+
1. The file **cannot** use Ansible's `vars_files` directive (e.g., inventory files)
338
+
2. The file requires **complex logic** that Tera provides but Ansible doesn't
339
+
3. The file needs **different variable scopes** than what centralized variables provide
340
+
341
+
Otherwise, use the centralized variables pattern for simplicity.
342
+
277
343
### Related Documentation
278
344
279
345
- **Architecture**: [`docs/technical/template-system-architecture.md`](../technical/template-system-architecture.md) - Understanding the two-phase template system
6.**`configure-firewall.yml.tera`** - Configure UFW firewall (VM-only, skipped in containers)
57
+
6.**`configure-firewall.yml`** - Configure UFW firewall (VM-only, skipped in containers)
54
58
55
59
## CI/Testing Considerations
56
60
@@ -63,6 +67,23 @@ For a typical deployment:
63
67
- The deployer sets `TORRUST_TD_SKIP_FIREWALL_IN_CONTAINER=true` for container tests (accepts `"true"` or `"false"` only)
64
68
- VM-based tests (LXD) have full kernel access and run the firewall playbook normally
65
69
70
+
## Variables Pattern
71
+
72
+
This directory uses a **centralized variables pattern**:
73
+
74
+
-**`variables.yml.tera`** - Centralized variables (rendered at runtime with Tera)
75
+
-**`inventory.yml.tera`** - Connection variables (rendered at runtime with Tera)
76
+
-**`*.yml`** - Static playbooks that load `variables.yml` via `vars_files` directive
77
+
78
+
### When Adding New Playbooks
79
+
80
+
1.**Add variables** to `variables.yml.tera`
81
+
2.**Create static**`.yml` playbook (not `.tera`)
82
+
3.**Add `vars_files: [variables.yml]`** to playbook
83
+
4.**Register** in `copy_static_templates()` if new static playbook
84
+
85
+
This pattern reduces Rust boilerplate (no per-playbook renderer/wrapper/context needed) while providing centralized variable management.
86
+
66
87
## Template Processing
67
88
68
-
These files are processed by the Tera templating engine and written to the `build/ansible/` directory during the build process.
89
+
Files with `.tera` extension are processed by the Tera templating engine. All files are written to the `build/ansible/` directory during the build process.
0 commit comments