-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
309 lines (259 loc) · 10.6 KB
/
Copy pathjustfile
File metadata and controls
309 lines (259 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# /Users/ivotrompert/Code/shell/installscript/justfile
# ------------------------------------------------------------------------------
# Aliases and Settings
# ------------------------------------------------------------------------------
set export # Export all variables to sub-processes
set shell := ["bash", "-euo", "pipefail", "-uc"] # Use bash for command execution with strict mode
# Default Ansible arguments
ANSIBLE_ARGS := ""
# Default file path used for the inventory hosts
DEFAULT_INVENTORY := inventory/development/hosts.yml
# Default inventory for run recipe
INVENTORY := "{{DEFAULT_INVENTORY}}"
# Default file path used for vault variables
DEFAULT_VAULT_FILE := group_vars/all/vault.yml
# Default path for Ansible log file
ANSIBLE_LOG_PATH := "./ansible.log"
# ------------------------------------------------------------------------------
# High-Level Targets
# ------------------------------------------------------------------------------
# Run a quick ping against all hosts
check-all:
@just ping GROUP="all"
# ------------------------------------------------------------------------------
# Setup and Dependencies
# ------------------------------------------------------------------------------
# Install Ansible Galaxy collections and roles from requirements.yml
install:
@echo "Installing Ansible dependencies from requirements.yml..."
ansible-galaxy collection install -r requirements.yml
# Verify all required dependencies are installed
REQUIRED_DEPS := "ansible git ansible-playbook ansible-lint ansible-vault"
check-deps:
@echo "Checking for required command-line tools..."
@for dep in $REQUIRED_DEPS; do \
command -v "$$dep" >/dev/null 2>&1 || { echo "ERROR: $$dep not found in PATH"; exit 1; }; \
done
@echo "All required dependencies are available."
# Complete project setup
setup: install check-deps
@echo "Project setup complete."
# ------------------------------------------------------------------------------
# Ansible Playbook Operations
# ------------------------------------------------------------------------------
# Execute the main site.yml playbook with optional arguments
run INVENTORY=INVENTORY:
@echo "Running main playbook: site.yml..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook -i "$INVENTORY" "site.yml"
# Run playbook against development inventory
run-dev:
just run INVENTORY="inventory/development/hosts.yml"
# Run playbook against staging inventory
run-staging:
just run INVENTORY="inventory/staging/hosts.yml"
# Run playbook against production inventory with confirmation
run-prod:
@read -r -p "Are you sure you want to run the playbook against PRODUCTION? (y/N) " confirm && [[ $confirm =~ ^(y|Y|yes|YES)$ ]] || exit 1
just run INVENTORY="inventory/production/hosts.yml"
# Execute playbook in check mode (dry run)
dry-run INVENTORY="{{DEFAULT_INVENTORY}}":
@echo "Executing playbook in check mode (dry run)..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook -i "$INVENTORY" "site.yml" --check
# Validate playbook syntax
syntax-check:
@echo "Validating playbook syntax..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "site.yml" --syntax-check
# ------------------------------------------------------------------------------
# Testing and Quality Assurance
# ------------------------------------------------------------------------------
# Test inventory path
TEST_INV := "tests/inventory/test_hosts.yml"
# Run all test playbooks
test:
@echo "Running all test playbooks..."
@if ls test-*.yml 1> /dev/null 2>&1; then \
for playbook in test-*.yml; do \
echo "Running $$playbook..."; \
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "$$playbook" --inventory "$TEST_INV" --check; \
done \
else \
echo "No test playbooks found"; \
fi
# Run a specific test playbook
test-playbook PLAYBOOK:
@echo "Running test playbook: {{PLAYBOOK}}..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "$PLAYBOOK" --inventory "$TEST_INV" --check
# Run Docker-specific tests
test-docker:
@echo "Running Docker test playbook..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "test-docker.yml" --inventory "$TEST_INV" --check
# Run Atuin-specific tests
test-atuin:
@echo "Running Atuin test playbook..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "test-atuin.yml" --inventory "$TEST_INV" --check
# Run Rust-specific tests
test-rust:
@echo "Running Rust test playbook..."
ANSIBLE_LOG_PATH="$ANSIBLE_LOG_PATH" ansible-playbook "test-rust.yml" --inventory "$TEST_INV" --check
# Run ansible-lint on all files
lint:
@echo "Running ansible-lint..."
ansible-lint
# Run ansible-lint with auto-fix where possible
lint-fix:
@if ! command -v ansible-lint >/dev/null 2>&1; then \
echo "ansible-lint is not installed. Please install it using 'pip install ansible-lint' or your package manager."; \
exit 1; \
fi
@echo "Running ansible-lint with auto-fix..."
@HELP_OUTPUT=$$(ansible-lint --help 2>&1); \
if [ $$? -eq 0 ]; then \
if echo "$$HELP_OUTPUT" | grep -q -- '--fix'; then \
ansible-lint --fix; \
elif echo "$$HELP_OUTPUT" | grep -q -- '--write'; then \
ansible-lint --write; \
else \
echo "Auto-fix not supported in this version of ansible-lint. Please upgrade or run 'just lint' for linting only."; \
ansible-lint; \
fi \
else \
echo "Failed to get help from ansible-lint. Please check your installation."; \
exit 1; \
fi
# Run comprehensive validation (syntax + lint + connectivity)
validate: syntax-check lint
@echo "Running comprehensive validation..."
@just ping
# ------------------------------------------------------------------------------
# Vault Management
# ------------------------------------------------------------------------------
# Edit encrypted vault files
vault-edit FILE={{DEFAULT_VAULT_FILE}}:
@echo "Editing vault file: {{FILE}}..."
@if [ -f "$FILE" ] && [ -w "$FILE" ]; then \
ansible-vault edit "$FILE"; \
else \
echo "ERROR: Vault file '$FILE' does not exist or is not writable."; \
exit 1; \
fi
# View encrypted vault files
vault-view FILE={{DEFAULT_VAULT_FILE}}:
@echo "Viewing vault file: {{FILE}}..."
ansible-vault view "$FILE"
# Encrypt a file with ansible-vault
vault-encrypt FILE:
@echo "Encrypting file: {{FILE}}..."
ansible-vault encrypt "$FILE"
# Decrypt a file with ansible-vault
vault-decrypt FILE:
@echo "Decrypting file: {{FILE}}..."
ansible-vault decrypt "$FILE"
# Change vault password
vault-rekey FILE={{DEFAULT_VAULT_FILE}}:
@echo "Rekeying vault file: {{FILE}}..."
ansible-vault rekey "$FILE"
# ------------------------------------------------------------------------------
# Inventory and Host Management
# ------------------------------------------------------------------------------
# Test connectivity to hosts
ping GROUP="all":
@echo "Pinging hosts in group: {{GROUP}}..."
ansible "$GROUP" -i "$DEFAULT_INVENTORY" -m ping
# List all hosts in inventory
list-hosts GROUP="all":
@echo "Listing hosts in group: {{GROUP}}..."
ansible-inventory -i "$DEFAULT_INVENTORY" --graph "$GROUP"
# Gather facts from a specific host
facts HOST:
@echo "Gathering facts from host: {{HOST}}..."
ansible "$HOST" -i "$DEFAULT_INVENTORY" -m setup
# Show inventory structure as a graph
inventory-graph:
@if [ ! -f "$DEFAULT_INVENTORY" ]; then \
echo "ERROR: Inventory file '$DEFAULT_INVENTORY' does not exist."; \
exit 1; \
elif [ ! -r "$DEFAULT_INVENTORY" ]; then \
echo "ERROR: Inventory file '$DEFAULT_INVENTORY' is not readable."; \
exit 1; \
fi
@echo "Generating inventory graph..."
ansible-inventory -i "$DEFAULT_INVENTORY" --graph
# ------------------------------------------------------------------------------
# Development Workflow
# ------------------------------------------------------------------------------
# Clean up temporary files and caches
clean:
@echo "Cleaning up temporary files..."
@find . -type f -name "*.retry" -delete
@rm -rf .ansible/
@echo "Clean-up complete."
# Format YAML files consistently
format:
@echo "Formatting YAML files..."
@if ! command -v "yamlfmt" >/dev/null 2>&1; then \
echo "yamlfmt is not installed. Please install it using 'go install github.com/google/yamlfmt/cmd/yamlfmt@latest' or your package manager."; \
exit 1; \
fi
@find . -name "*.yml" -o -name "*.yaml" | xargs yamlfmt -w
@echo "YAML formatting complete."
# List available documentation
docs:
@echo "Available documentation files:"
@if [ -d "docs/" ]; then \
find docs/ -name "*.md" -printf ' - %P\n' | sort; \
else \
echo "No docs directory found."; \
fi
@echo ""
@echo "Use 'just view-doc <filename>' to open a specific document"
# View a specific documentation file
view-doc FILE:
@echo "Opening documentation file: {{FILE}}"
@if [ -f "docs/$FILE" ]; then \
${EDITOR:-nano} "docs/$FILE"; \
else \
echo "File docs/$FILE not found. Available files:"; \
ls docs/*.md 2>/dev/null | sed 's|docs/||' | sort; \
fi
# View recent Ansible logs
logs:
@echo "Viewing Ansible logs..."
cat "$ANSIBLE_LOG_PATH" || echo "Ansible log file not found or permission denied."
# ------------------------------------------------------------------------------
# Git Integration
# ------------------------------------------------------------------------------
# Add all changes and commit with message
commit MESSAGE:
@echo "Committing changes..."
if [ -z "$MESSAGE" ]; then echo "Commit message cannot be empty."; exit 1; fi
git add .
if git diff-index --cached --quiet HEAD --; then echo "No changes staged for commit."; exit 1; fi
git commit -m "$MESSAGE"
# Push changes to remote repository
push:
@echo "Pushing changes to remote..."
git push
# Show git status with Ansible-specific file highlighting
status:
@echo "Git status:"
git status
# Show git diff with YAML-aware formatting
diff:
@echo "Git diff:"
git diff
# ------------------------------------------------------------------------------
# Utility Commands
# ------------------------------------------------------------------------------
# Show detailed help for all commands
help:
@just --list
# Show Ansible and project version information
version:
@echo "Ansible version:"
@"ansible" --version
@echo ""
@echo "Project version: (Not defined)"
# Show current Ansible configuration
config:
@echo "Current Ansible configuration:"
ansible-config dump