|
4 | 4 | # to populate the wikis: section in public.yml |
5 | 5 |
|
6 | 6 | - name: Check if wikis directory exists |
7 | | - stat: |
| 7 | + ansible.builtin.stat: |
8 | 8 | path: "{{ m_local_public }}/wikis" |
9 | 9 | register: wikis_dir_exists |
10 | 10 | delegate_to: localhost |
11 | 11 | run_once: true |
12 | 12 |
|
13 | 13 | - name: Find existing wiki directories |
14 | | - find: |
| 14 | + ansible.builtin.find: |
15 | 15 | paths: "{{ m_local_public }}/wikis" |
16 | 16 | file_type: directory |
17 | 17 | register: existing_wiki_dirs |
|
20 | 20 | run_once: true |
21 | 21 |
|
22 | 22 | - name: Extract wiki information from existing base.php files |
| 23 | + when: existing_wiki_dirs is defined and existing_wiki_dirs.files | length > 0 |
23 | 24 | block: |
24 | 25 | - name: Check which wikis have base.php files |
25 | | - stat: |
| 26 | + ansible.builtin.stat: |
26 | 27 | path: "{{ item.path }}/preLocalSettings.d/base.php" |
27 | 28 | register: base_php_files |
28 | 29 | loop: "{{ existing_wiki_dirs.files }}" |
|
31 | 32 | run_once: true |
32 | 33 |
|
33 | 34 | - name: Read base.php files to extract wiki names |
34 | | - slurp: |
| 35 | + ansible.builtin.slurp: |
35 | 36 | src: "{{ item.item.path }}/preLocalSettings.d/base.php" |
36 | 37 | register: base_php_contents |
37 | 38 | loop: "{{ base_php_files.results }}" |
|
42 | 43 | run_once: true |
43 | 44 |
|
44 | 45 | - name: Extract wiki site names from base.php files |
45 | | - shell: | |
| 46 | + ansible.builtin.shell: | |
| 47 | + set -o pipefail |
46 | 48 | if [ -f "{{ item.item.path }}/preLocalSettings.d/base.php" ]; then |
47 | 49 | grep "^\$wgSitename" "{{ item.item.path }}/preLocalSettings.d/base.php" | sed -E "s/.*['\"]([^'\"]*)['\"].*/\1/" | head -1 |
48 | 50 | else |
|
56 | 58 | delegate_to: localhost |
57 | 59 | run_once: true |
58 | 60 | failed_when: false |
| 61 | + changed_when: false |
59 | 62 |
|
60 | 63 | - name: Parse wiki information from base.php files |
61 | | - set_fact: |
| 64 | + ansible.builtin.set_fact: |
62 | 65 | discovered_wikis: "{{ discovered_wikis | default([]) + [wiki_info] }}" |
63 | 66 | vars: |
64 | 67 | wiki_id: "{{ item.item.item.path | basename }}" |
65 | 68 | # Find matching extracted sitename by wiki_id |
66 | 69 | extracted_name: >- |
67 | 70 | {%- for result in extracted_sitenames.results -%} |
68 | 71 | {%- 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 -%} |
71 | 73 | {%- endfor -%} |
72 | 74 | wiki_info: |
73 | 75 | id: "{{ wiki_id }}" |
|
86 | 88 | - name: Preserve existing primary wiki settings or set defaults |
87 | 89 | block: |
88 | 90 | - name: Get existing primary wikis |
89 | | - set_fact: |
| 91 | + ansible.builtin.set_fact: |
90 | 92 | 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: |
94 | 96 | discovered_wikis: "{{ discovered_wikis | map('combine', {'primary': (item.id in existing_primary_wikis)}) | list }}" |
95 | 97 | 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: |
99 | 101 | discovered_wikis: >- |
100 | 102 | {%- set updated_wikis = [] -%} |
101 | 103 | {%- set demo_exists = discovered_wikis | selectattr('id', 'equalto', 'demo') | list | length > 0 -%} |
|
113 | 115 | when: existing_primary_wikis | length == 0 |
114 | 116 |
|
115 | 117 | - name: Add wikis without base.php files (fallback) |
116 | | - set_fact: |
| 118 | + ansible.builtin.set_fact: |
117 | 119 | discovered_wikis: "{{ discovered_wikis | default([]) + [wiki_info] }}" |
118 | 120 | vars: |
119 | 121 | wiki_id: "{{ item.item.path | basename }}" |
|
129 | 131 | delegate_to: localhost |
130 | 132 | run_once: true |
131 | 133 |
|
132 | | - when: existing_wiki_dirs is defined and existing_wiki_dirs.files | length > 0 |
133 | | - |
134 | 134 | - name: Check if public.yml already exists |
135 | | - stat: |
| 135 | + ansible.builtin.stat: |
136 | 136 | path: "{{ m_local_public }}/public.yml" |
137 | 137 | register: public_yml_exists |
138 | 138 | delegate_to: localhost |
139 | 139 | run_once: true |
140 | 140 |
|
141 | 141 | - name: Read existing public.yml configuration |
142 | | - slurp: |
| 142 | + ansible.builtin.slurp: |
143 | 143 | src: "{{ m_local_public }}/public.yml" |
144 | 144 | register: public_yml_content |
145 | 145 | when: public_yml_exists.stat.exists |
146 | 146 | delegate_to: localhost |
147 | 147 | run_once: true |
148 | 148 |
|
149 | 149 | - name: Create or update public.yml with discovered wikis |
| 150 | + when: discovered_wikis is defined and discovered_wikis | length > 0 |
150 | 151 | block: |
151 | 152 | - name: Parse existing YAML configuration (if exists) |
152 | | - set_fact: |
| 153 | + ansible.builtin.set_fact: |
153 | 154 | current_config: "{{ public_yml_content['content'] | b64decode | from_yaml | default({}) }}" |
154 | 155 | when: public_yml_content is defined |
155 | 156 |
|
156 | 157 | - name: Initialize current config if no existing public.yml |
157 | | - set_fact: |
| 158 | + ansible.builtin.set_fact: |
158 | 159 | current_config: {} |
159 | 160 | when: current_config is not defined |
160 | 161 |
|
161 | 162 | - name: Check if wikis are already properly declared |
162 | | - set_fact: |
| 163 | + ansible.builtin.set_fact: |
163 | 164 | wikis_already_exist: "{{ (current_config.wikis is defined) and (current_config.wikis | length > 0) }}" |
164 | 165 |
|
165 | 166 | - name: Compare existing wikis with discovered wikis |
166 | | - set_fact: |
| 167 | + ansible.builtin.set_fact: |
167 | 168 | existing_wiki_ids: "{{ current_config.wikis | map(attribute='id') | list | sort if current_config.wikis is defined else [] }}" |
168 | 169 | discovered_wiki_ids: "{{ discovered_wikis | map(attribute='id') | list | sort if discovered_wikis is defined else [] }}" |
169 | 170 |
|
170 | 171 | - name: Determine if migration is needed |
171 | | - set_fact: |
| 172 | + ansible.builtin.set_fact: |
172 | 173 | migration_needed: "{{ not wikis_already_exist or (existing_wiki_ids != discovered_wiki_ids) }}" |
173 | 174 |
|
174 | 175 | - name: Display idempotency message when no changes needed |
175 | | - debug: |
| 176 | + ansible.builtin.debug: |
176 | 177 | msg: | |
177 | 178 | Migration skipped: wikis are already properly declared in public.yml |
178 | 179 | Existing wikis: {{ existing_wiki_ids | join(', ') }} |
179 | 180 | Discovered wikis: {{ discovered_wiki_ids | join(', ') }} |
180 | 181 | when: not migration_needed |
181 | 182 |
|
182 | 183 | - name: Merge discovered wikis with existing configuration |
183 | | - set_fact: |
| 184 | + ansible.builtin.set_fact: |
184 | 185 | updated_config: "{{ current_config | combine({'wikis': discovered_wikis}, recursive=true) }}" |
185 | 186 | when: discovered_wikis is defined and discovered_wikis | length > 0 and migration_needed |
186 | 187 |
|
187 | 188 | - 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 |
188 | 192 | block: |
189 | 193 | - name: Read current public.yml content as text |
190 | | - slurp: |
| 194 | + ansible.builtin.slurp: |
191 | 195 | src: "{{ m_local_public }}/public.yml" |
192 | 196 | register: current_yml_text |
193 | | - |
| 197 | + |
194 | 198 | - name: Generate wikis YAML block |
195 | | - set_fact: |
| 199 | + ansible.builtin.set_fact: |
196 | 200 | wikis_yaml: "{{ {'wikis': discovered_wikis} | to_nice_yaml(indent=2) | regex_replace('^---\\n', '') }}" |
197 | 201 |
|
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 |
200 | 205 | # Create backup |
201 | 206 | cp "{{ m_local_public }}/public.yml" "{{ m_local_public }}/public.yml.backup-$(date +%Y%m%d-%H%M%S)" |
202 | | - |
| 207 | +
|
203 | 208 | # Write the wikis YAML to a temporary file first |
204 | 209 | cat << 'EOF' > /tmp/wikis_section.yml |
205 | 210 | {{ wikis_yaml }} |
206 | 211 | EOF |
207 | | - |
| 212 | +
|
208 | 213 | # Check if wikis section already exists |
209 | 214 | if grep -q "^wikis:" "{{ m_local_public }}/public.yml"; then |
210 | 215 | # Use Python to safely replace the wikis section |
211 | 216 | python3 << 'PYTHON_SCRIPT' |
212 | 217 | import re |
213 | 218 | import sys |
214 | | - |
| 219 | +
|
215 | 220 | # Read the original file |
216 | 221 | with open("{{ m_local_public }}/public.yml", 'r') as f: |
217 | 222 | content = f.read() |
218 | | - |
| 223 | +
|
219 | 224 | # Read the new wikis section |
220 | 225 | with open("/tmp/wikis_section.yml", 'r') as f: |
221 | 226 | wikis_content = f.read().strip() |
222 | | - |
| 227 | +
|
223 | 228 | # Replace wikis section using regex |
224 | 229 | # Pattern matches from "wikis:" to either next top-level key or end of file |
225 | 230 | pattern = r'^wikis:[\s\S]*?(?=^[a-zA-Z_][a-zA-Z0-9_]*:|$)' |
226 | 231 | replacement = wikis_content |
227 | | - |
| 232 | +
|
228 | 233 | # Replace the wikis section |
229 | 234 | new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE) |
230 | | - |
| 235 | +
|
231 | 236 | # Write the updated content back |
232 | 237 | with open("{{ m_local_public }}/public.yml", 'w') as f: |
233 | 238 | f.write(new_content) |
|
237 | 242 | echo "" >> "{{ m_local_public }}/public.yml" |
238 | 243 | cat /tmp/wikis_section.yml >> "{{ m_local_public }}/public.yml" |
239 | 244 | fi |
240 | | - |
| 245 | +
|
241 | 246 | # Clean up |
242 | 247 | rm -f /tmp/wikis_section.yml |
243 | 248 | args: |
244 | 249 | executable: /bin/bash |
245 | | - when: updated_config is defined and migration_needed |
246 | | - delegate_to: localhost |
247 | | - run_once: true |
| 250 | + changed_when: true |
248 | 251 |
|
249 | 252 | - name: Display migration results |
250 | | - debug: |
| 253 | + ansible.builtin.debug: |
251 | 254 | msg: | |
252 | 255 | Migration completed! Found {{ discovered_wikis | length }} existing wikis: |
253 | 256 | {% for wiki in discovered_wikis %} |
|
259 | 262 | when: discovered_wikis is defined and discovered_wikis | length > 0 and migration_needed |
260 | 263 |
|
261 | 264 | - name: Set migration status for playbook |
262 | | - set_fact: |
| 265 | + ansible.builtin.set_fact: |
263 | 266 | migration_was_performed: "{{ migration_needed | default(false) }}" |
264 | 267 |
|
265 | | - when: discovered_wikis is defined and discovered_wikis | length > 0 |
266 | | - |
267 | 268 | - name: Display message when no existing wikis found |
268 | | - debug: |
| 269 | + ansible.builtin.debug: |
269 | 270 | msg: "No existing wiki directories found to migrate. Declarative configuration is ready for new wikis." |
270 | 271 | when: discovered_wikis is not defined or discovered_wikis | length == 0 |
0 commit comments