Update playbooks/nextcloud-upgrade.yml

This commit is contained in:
2025-11-27 15:46:58 +00:00
parent 20083f4577
commit f1851afe9a
+20 -122
View File
@@ -1,104 +1,3 @@
- name: Fast-Track Nextcloud Upgrade (Skip Discovery)
hosts: all
become: yes
vars:
# ----------------------------------------------------------------------
# ⚠️ REQUIRED USER VARIABLES (TRUSTED)
# ----------------------------------------------------------------------
nextcloud_dir: /var/www/nextcloud
php_cli_path: /usr/bin/php
web_service_name: httpd
# User confirmed by 'ls -ld' output (e.g., apache, www-data, etc.)
nc_web_user: apache
# ----------------------------------------------------------------------
# INTERNAL PLAYBOOK VARIABLES
# ----------------------------------------------------------------------
occ_env:
TMPDIR: /tmp
core_apps:
- activity
- twofactor_nextcloud_notification
- files
- settings
- dav
- updater
- workflowengine
- theming
- user_ldap
- dashboard
- federation
- updatestatus
- accessibility
- serverinfo
tasks:
- name: Reminder - ensure backups exist
debug:
msg: "⚠️ CRITICAL: Ensure you have valid DB + data directory backups BEFORE PROCEEDING!"
- name: Pause playbook for 3 seconds to allow backup cancellation
pause:
seconds: 3
prompt: "Press Ctrl+C and then 'A' to abort if you do not have backups."
# ----------------------------------------------------------------------
# 🚀 STAGE 1: VERSION CHECK
# (Discovery steps have been skipped, directly checking status)
# ----------------------------------------------------------------------
- name: 1.1 - Get current NC status (and check if installed)
command: "{{ php_cli_path }} occ status"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
register: occ_status
changed_when: false
# This still fails if Nextcloud is not installed or paths are severely wrong
failed_when: "'installed: true' not in occ_status.stdout"
- name: 1.2 - Parse and show current version
set_fact:
current_version: "{{ occ_status.stdout | regex_search('versionstring: (.*)', '\\1') | first | default('unknown') }}"
when: occ_status.stdout is defined
- name: 1.3 - Show current version
debug:
msg: "Current Nextcloud version: {{ current_version }}"
# ----------------------------------------------------------------------
# 🔄 STAGE 2: PRE-UPGRADE PREPARATION
# ----------------------------------------------------------------------
- name: 2.1 - Get list of installed apps
command: "{{ php_cli_path }} occ app:list"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
register: app_list
changed_when: false
- name: 2.2 - Disable all non-core 3rd party apps
command: "{{ php_cli_path }} occ app:disable {{ item }}"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
loop: "{{ app_list.stdout_lines | select('match', '^[ ]*-[ ](.*):[ ]enabled$') | map('regex_replace', '^[ ]*-[ ](.*):[ ]enabled$', '\\1') | list }}"
register: app_disable_result
when: item not in core_apps
ignore_errors: yes
- name: 2.3 - Save list of successfully disabled apps for re-enabling
set_fact:
disabled_apps: "{{ app_disable_result.results | selectattr('changed', 'defined') | selectattr('changed') | map(attribute='item') | list }}"
when: app_disable_result is defined and app_disable_result.results is defined
# ----------------------------------------------------------------------
# 🚀 STAGE 3: TRANSACTIONAL UPGRADE BLOCK
# ----------------------------------------------------------------------
- name: 3.0 - Perform Nextcloud Upgrade (Transactional)
block:
@@ -125,7 +24,7 @@
- updater_result.rc != 0
- "'Nextcloud is already latest version' not in updater_result.stdout"
- name: 3.3 - Run occ upgrade (Critical Database Step)
- name: 3.3 - Run occ upgrade
command: "{{ php_cli_path }} occ upgrade"
args:
chdir: "{{ nextcloud_dir }}"
@@ -143,7 +42,7 @@
- "{{ php_cli_path }} occ db:convert-filecache-bigint --no-interaction"
ignore_errors: yes
- name: 3.5 - Disable maintenance mode (SUCCESS)
- name: 3.5 - SUCCESS – disable maintenance mode
command: "{{ php_cli_path }} occ maintenance:mode --off"
args:
chdir: "{{ nextcloud_dir }}"
@@ -178,35 +77,34 @@
debug:
var: occ_status_after.stdout
- name: 3.10 - ✅ Upgrade Complete
- name: 3.10 - Upgrade Complete
debug:
msg: "Nextcloud successfully upgraded!"
# ----------------------------------------------------------------------
# 🆘 STAGE 4: RESCUE BLOCK
# ----------------------------------------------------------------------
rescue:
- name: ❌ UPGRADE FAILED - REPORTING
- name: ❌ UPGRADE FAILED - Details
debug:
msg: |
!!!!!!!!!! CRITICAL UPGRADE FAILURE !!!!!!!!!!
The playbook has failed during the upgrade block.
The server is being LEFT IN MAINTENANCE MODE for manual inspection.
!!!!!!!!!!! NEXTCLOUD UPGRADE FAILED !!!!!!!!!!!!
Host: {{ inventory_hostname }}
Web User: {{ nc_web_user }}
Please SSH to the host and check logs to fix manually.
Logs to check:
The upgrade process encountered a fatal error.
Maintenance mode will now be DISABLED automatically.
Check logs:
- {{ nextcloud_dir }}/data/updater.log
- {{ nextcloud_dir }}/data/nextcloud.log
Once fixed, run this command manually as the web user:
'{{ php_cli_path }} occ maintenance:mode --off'
always:
- name: 4.1 - Fail the playbook run
- name: ALWAYS disable maintenance mode (success or failure)
command: "{{ php_cli_path }} occ maintenance:mode --off"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
ignore_errors: yes
- name: Final failure state (only if rescue was triggered)
fail:
msg: "Nextcloud upgrade failed. Host is in maintenance mode."
msg: "Nextcloud upgrade failed — maintenance mode has been turned off."
when: ansible_failed_task is defined