Skip to content

Commit f0acdaf

Browse files
committed
Fix ansible syntax problems
FQCN (Fully Qualified Collection Names) Fixed task key order - name - when - delegate_to - run_once - block Fix shell command issues - set -o pipefail to shell commands to handle pipe failures - changed_when: false for read-only shell commands - changed_when: true for shell commands that modify files Jinja2 spacing - remove extra space (newline) before {%- endif -%} Structure Issues - Fixed duplicated when conditions that were appearing at both block and task levels - Removed duplicate delegate_to and run_once declarations - Properly structured task hierarchies
1 parent 3f2f5d9 commit f0acdaf

File tree

1 file changed

+49
-48
lines changed
  • src/roles/migrate-to-declarative-wikis/tasks

1 file changed

+49
-48
lines changed

src/roles/migrate-to-declarative-wikis/tasks/main.yml

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# to populate the wikis: section in public.yml
55

66
- name: Check if wikis directory exists
7-
stat:
7+
ansible.builtin.stat:
88
path: "{{ m_local_public }}/wikis"
99
register: wikis_dir_exists
1010
delegate_to: localhost
1111
run_once: true
1212

1313
- name: Find existing wiki directories
14-
find:
14+
ansible.builtin.find:
1515
paths: "{{ m_local_public }}/wikis"
1616
file_type: directory
1717
register: existing_wiki_dirs
@@ -20,9 +20,10 @@
2020
run_once: true
2121

2222
- name: Extract wiki information from existing base.php files
23+
when: existing_wiki_dirs is defined and existing_wiki_dirs.files | length > 0
2324
block:
2425
- name: Check which wikis have base.php files
25-
stat:
26+
ansible.builtin.stat:
2627
path: "{{ item.path }}/preLocalSettings.d/base.php"
2728
register: base_php_files
2829
loop: "{{ existing_wiki_dirs.files }}"
@@ -31,7 +32,7 @@
3132
run_once: true
3233

3334
- name: Read base.php files to extract wiki names
34-
slurp:
35+
ansible.builtin.slurp:
3536
src: "{{ item.item.path }}/preLocalSettings.d/base.php"
3637
register: base_php_contents
3738
loop: "{{ base_php_files.results }}"
@@ -42,7 +43,8 @@
4243
run_once: true
4344

4445
- name: Extract wiki site names from base.php files
45-
shell: |
46+
ansible.builtin.shell: |
47+
set -o pipefail
4648
if [ -f "{{ item.item.path }}/preLocalSettings.d/base.php" ]; then
4749
grep "^\$wgSitename" "{{ item.item.path }}/preLocalSettings.d/base.php" | sed -E "s/.*['\"]([^'\"]*)['\"].*/\1/" | head -1
4850
else
@@ -56,18 +58,18 @@
5658
delegate_to: localhost
5759
run_once: true
5860
failed_when: false
61+
changed_when: false
5962

6063
- name: Parse wiki information from base.php files
61-
set_fact:
64+
ansible.builtin.set_fact:
6265
discovered_wikis: "{{ discovered_wikis | default([]) + [wiki_info] }}"
6366
vars:
6467
wiki_id: "{{ item.item.item.path | basename }}"
6568
# Find matching extracted sitename by wiki_id
6669
extracted_name: >-
6770
{%- for result in extracted_sitenames.results -%}
6871
{%- if result.item.item.path | basename == wiki_id and result.stdout_lines is defined and result.stdout_lines -%}
69-
{{ result.stdout_lines[-1] }}
70-
{%- endif -%}
72+
{{ result.stdout_lines[-1] }}{%- endif -%}
7173
{%- endfor -%}
7274
wiki_info:
7375
id: "{{ wiki_id }}"
@@ -86,16 +88,16 @@
8688
- name: Preserve existing primary wiki settings or set defaults
8789
block:
8890
- name: Get existing primary wikis
89-
set_fact:
91+
ansible.builtin.set_fact:
9092
existing_primary_wikis: "{{ current_config.wikis | default([]) | selectattr('primary', 'equalto', true) | map(attribute='id') | list }}"
91-
92-
- name: Update primary flags based on existing configuration
93-
set_fact:
93+
94+
- name: Update primary flags based on existing configuration
95+
ansible.builtin.set_fact:
9496
discovered_wikis: "{{ discovered_wikis | map('combine', {'primary': (item.id in existing_primary_wikis)}) | list }}"
9597
when: existing_primary_wikis | length > 0
96-
97-
- name: Set default primary wiki when no existing configuration
98-
set_fact:
98+
99+
- name: Set default primary wiki when no existing configuration
100+
ansible.builtin.set_fact:
99101
discovered_wikis: >-
100102
{%- set updated_wikis = [] -%}
101103
{%- set demo_exists = discovered_wikis | selectattr('id', 'equalto', 'demo') | list | length > 0 -%}
@@ -113,7 +115,7 @@
113115
when: existing_primary_wikis | length == 0
114116

115117
- name: Add wikis without base.php files (fallback)
116-
set_fact:
118+
ansible.builtin.set_fact:
117119
discovered_wikis: "{{ discovered_wikis | default([]) + [wiki_info] }}"
118120
vars:
119121
wiki_id: "{{ item.item.path | basename }}"
@@ -129,105 +131,108 @@
129131
delegate_to: localhost
130132
run_once: true
131133

132-
when: existing_wiki_dirs is defined and existing_wiki_dirs.files | length > 0
133-
134134
- name: Check if public.yml already exists
135-
stat:
135+
ansible.builtin.stat:
136136
path: "{{ m_local_public }}/public.yml"
137137
register: public_yml_exists
138138
delegate_to: localhost
139139
run_once: true
140140

141141
- name: Read existing public.yml configuration
142-
slurp:
142+
ansible.builtin.slurp:
143143
src: "{{ m_local_public }}/public.yml"
144144
register: public_yml_content
145145
when: public_yml_exists.stat.exists
146146
delegate_to: localhost
147147
run_once: true
148148

149149
- name: Create or update public.yml with discovered wikis
150+
when: discovered_wikis is defined and discovered_wikis | length > 0
150151
block:
151152
- name: Parse existing YAML configuration (if exists)
152-
set_fact:
153+
ansible.builtin.set_fact:
153154
current_config: "{{ public_yml_content['content'] | b64decode | from_yaml | default({}) }}"
154155
when: public_yml_content is defined
155156

156157
- name: Initialize current config if no existing public.yml
157-
set_fact:
158+
ansible.builtin.set_fact:
158159
current_config: {}
159160
when: current_config is not defined
160161

161162
- name: Check if wikis are already properly declared
162-
set_fact:
163+
ansible.builtin.set_fact:
163164
wikis_already_exist: "{{ (current_config.wikis is defined) and (current_config.wikis | length > 0) }}"
164165

165166
- name: Compare existing wikis with discovered wikis
166-
set_fact:
167+
ansible.builtin.set_fact:
167168
existing_wiki_ids: "{{ current_config.wikis | map(attribute='id') | list | sort if current_config.wikis is defined else [] }}"
168169
discovered_wiki_ids: "{{ discovered_wikis | map(attribute='id') | list | sort if discovered_wikis is defined else [] }}"
169170

170171
- name: Determine if migration is needed
171-
set_fact:
172+
ansible.builtin.set_fact:
172173
migration_needed: "{{ not wikis_already_exist or (existing_wiki_ids != discovered_wiki_ids) }}"
173174

174175
- name: Display idempotency message when no changes needed
175-
debug:
176+
ansible.builtin.debug:
176177
msg: |
177178
Migration skipped: wikis are already properly declared in public.yml
178179
Existing wikis: {{ existing_wiki_ids | join(', ') }}
179180
Discovered wikis: {{ discovered_wiki_ids | join(', ') }}
180181
when: not migration_needed
181182

182183
- name: Merge discovered wikis with existing configuration
183-
set_fact:
184+
ansible.builtin.set_fact:
184185
updated_config: "{{ current_config | combine({'wikis': discovered_wikis}, recursive=true) }}"
185186
when: discovered_wikis is defined and discovered_wikis | length > 0 and migration_needed
186187

187188
- name: Update only the wikis section in public.yml
189+
when: updated_config is defined and migration_needed
190+
delegate_to: localhost
191+
run_once: true
188192
block:
189193
- name: Read current public.yml content as text
190-
slurp:
194+
ansible.builtin.slurp:
191195
src: "{{ m_local_public }}/public.yml"
192196
register: current_yml_text
193-
197+
194198
- name: Generate wikis YAML block
195-
set_fact:
199+
ansible.builtin.set_fact:
196200
wikis_yaml: "{{ {'wikis': discovered_wikis} | to_nice_yaml(indent=2) | regex_replace('^---\\n', '') }}"
197201

198-
- name: Update public.yml with new wikis section
199-
shell: |
202+
- name: Update public.yml with new wikis section
203+
ansible.builtin.shell: |
204+
set -o pipefail
200205
# Create backup
201206
cp "{{ m_local_public }}/public.yml" "{{ m_local_public }}/public.yml.backup-$(date +%Y%m%d-%H%M%S)"
202-
207+
203208
# Write the wikis YAML to a temporary file first
204209
cat << 'EOF' > /tmp/wikis_section.yml
205210
{{ wikis_yaml }}
206211
EOF
207-
212+
208213
# Check if wikis section already exists
209214
if grep -q "^wikis:" "{{ m_local_public }}/public.yml"; then
210215
# Use Python to safely replace the wikis section
211216
python3 << 'PYTHON_SCRIPT'
212217
import re
213218
import sys
214-
219+
215220
# Read the original file
216221
with open("{{ m_local_public }}/public.yml", 'r') as f:
217222
content = f.read()
218-
223+
219224
# Read the new wikis section
220225
with open("/tmp/wikis_section.yml", 'r') as f:
221226
wikis_content = f.read().strip()
222-
227+
223228
# Replace wikis section using regex
224229
# Pattern matches from "wikis:" to either next top-level key or end of file
225230
pattern = r'^wikis:[\s\S]*?(?=^[a-zA-Z_][a-zA-Z0-9_]*:|$)'
226231
replacement = wikis_content
227-
232+
228233
# Replace the wikis section
229234
new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
230-
235+
231236
# Write the updated content back
232237
with open("{{ m_local_public }}/public.yml", 'w') as f:
233238
f.write(new_content)
@@ -237,17 +242,15 @@
237242
echo "" >> "{{ m_local_public }}/public.yml"
238243
cat /tmp/wikis_section.yml >> "{{ m_local_public }}/public.yml"
239244
fi
240-
245+
241246
# Clean up
242247
rm -f /tmp/wikis_section.yml
243248
args:
244249
executable: /bin/bash
245-
when: updated_config is defined and migration_needed
246-
delegate_to: localhost
247-
run_once: true
250+
changed_when: true
248251

249252
- name: Display migration results
250-
debug:
253+
ansible.builtin.debug:
251254
msg: |
252255
Migration completed! Found {{ discovered_wikis | length }} existing wikis:
253256
{% for wiki in discovered_wikis %}
@@ -259,12 +262,10 @@
259262
when: discovered_wikis is defined and discovered_wikis | length > 0 and migration_needed
260263

261264
- name: Set migration status for playbook
262-
set_fact:
265+
ansible.builtin.set_fact:
263266
migration_was_performed: "{{ migration_needed | default(false) }}"
264267

265-
when: discovered_wikis is defined and discovered_wikis | length > 0
266-
267268
- name: Display message when no existing wikis found
268-
debug:
269+
ansible.builtin.debug:
269270
msg: "No existing wiki directories found to migrate. Declarative configuration is ready for new wikis."
270271
when: discovered_wikis is not defined or discovered_wikis | length == 0

0 commit comments

Comments
 (0)