Update playbooks/nextcloud-upgrade.yml

This commit is contained in:
2025-11-27 15:57:02 +00:00
parent b7d041f72e
commit 911b2993e5
+80 -150
View File
@@ -1,200 +1,130 @@
- name: Fast-Track Nextcloud Upgrade (Skip Discovery) ---
hosts: all - name: Nextcloud Upgrade
hosts: nextcloud
become: yes become: yes
vars: vars:
nextcloud_dir: /var/www/nextcloud ansible_remote_tmp: "/var/tmp/.ansible"
php_cli_path: /usr/bin/php nextcloud_path: "/var/www/nextcloud"
web_service_name: httpd php_bin: "/usr/bin/php"
nc_web_user: apache pre_tasks:
- name: Ensure remote tmp exists
occ_env: file:
TMPDIR: /tmp path: "/var/tmp/.ansible"
state: directory
core_apps: owner: root
- activity group: root
- twofactor_nextcloud_notification mode: "0755"
- files
- settings
- dav
- updater
- workflowengine
- theming
- user_ldap
- dashboard
- federation
- updatestatus
- accessibility
- serverinfo
tasks: tasks:
- name: Reminder - ensure backups exist
debug:
msg: "⚠️ CRITICAL: Ensure you have valid DB + data backups BEFORE proceeding!"
- name: Pause playbook for 3 seconds - name: Pause playbook for 3 seconds
pause: pause:
seconds: 3 seconds: 3
prompt: "Press Ctrl+C then A to abort if no backups exist."
# ---------------------------------------------------------------------- - name: 1.1 - Get current Nextcloud status
# VERSION CHECK command: "{{ php_bin }} occ status --output=json"
# ----------------------------------------------------------------------
- name: 1.1 - Get current NC status
command: "{{ php_cli_path }} occ status"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}" register: nc_status_json
register: occ_status
changed_when: false
failed_when: "'installed: true' not in occ_status.stdout"
- name: 1.2 - Parse current version - name: 1.2 - Parse current version
set_fact: set_fact:
current_version: "{{ occ_status.stdout | regex_search('versionstring: (.*)', '\\1') | first | default('unknown') }}" nc_current_version: "{{ (nc_status_json.stdout | from_json).versionstring }}"
- name: 1.3 - Show current version - name: 1.3 - Show current version
debug: debug:
msg: "Current Nextcloud version: {{ current_version }}" msg: "Current Nextcloud version: {{ nc_current_version }}"
# ----------------------------------------------------------------------
# PRE-UPGRADE PREPARATION
# ----------------------------------------------------------------------
- name: 2.1 - Get list of installed apps - name: 2.1 - Get list of installed apps
command: "{{ php_cli_path }} occ app:list" command: "{{ php_bin }} occ app:list --output=json"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}"
register: app_list register: app_list
changed_when: false
- name: 2.2 - Disable all non-core 3rd party apps - name: 2.2 - Disable 3rd-party apps (non-core)
command: "{{ php_cli_path }} occ app:disable {{ item }}" command: "{{ php_bin }} occ app:disable {{ item }}"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}" loop: "{{ (app_list.stdout | from_json).enabled | difference(['files','activity','dav','federation','files_external','files_sharing','files_trashbin','files_versions','lookup_server_connector','notifications','provisioning_api','settings','systemtags','theming','updatenotification','user_status']) }}"
environment: "{{ occ_env }}" register: disabled_apps
loop: "{{ app_list.stdout_lines | select('match', '^[ ]*-[ ](.*):[ ]enabled$') | map('regex_replace', '^[ ]*-[ ](.*):[ ]enabled$', '\\1') | list }}" changed_when: "'Disabled' in disabled_apps.stdout"
register: app_disable_result when: false # Optional: Disabled if you want.
when: item not in core_apps
ignore_errors: yes
- name: 2.3 - Save disabled apps list - name: 2.3 - Save disabled apps list
set_fact: set_fact:
disabled_apps: "{{ app_disable_result.results | selectattr('changed', 'defined') | selectattr('changed') | map(attribute='item') | list }}" nc_disabled_apps: "{{ disabled_apps.results | map(attribute='item') | list }}"
when: app_disable_result is defined and app_disable_result.results is defined
# ----------------------------------------------------------------------
# UPGRADE BLOCK WITH RESCUE + ALWAYS
# ----------------------------------------------------------------------
- name: 3.0 - Perform Nextcloud Upgrade (Transactional)
block:
- name: 3.1 - Enable maintenance mode - name: 3.1 - Enable maintenance mode
command: "{{ php_cli_path }} occ maintenance:mode --on" command: "{{ php_bin }} occ maintenance:mode --on"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}" register: maintenance_on
environment: "{{ occ_env }}" changed_when: true
- name: 3.2 - Run Nextcloud CLI updater (retry) - block:
command: "{{ php_cli_path }} updater/updater.phar --no-interaction"
# ───────────────────────────────
# 3.2 — RUN NEXTCLOUD UPDATER
# ───────────────────────────────
- name: 3.2 - Run Nextcloud CLI updater
command: "{{ php_bin }} updater/updater.phar --no-interaction"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}" register: updater_output
environment: "{{ occ_env }}"
register: updater_result
until:
- updater_result.rc == 0
- "'Update successful' in updater_result.stdout or 'Nextcloud is already latest version' in updater_result.stdout"
retries: 3 retries: 3
delay: 10 delay: 3
failed_when: until: updater_output.rc == 0
- updater_result.rc != 0
- "'Nextcloud is already latest version' not in updater_result.stdout"
- name: 3.3 - Run occ upgrade (DB update) - name: Check if update actually happened
command: "{{ php_cli_path }} occ upgrade" set_fact:
args: update_message: "{{ updater_output.stdout }}"
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
- name: 3.4 - Cleanup commands - name: If already newest version, show message
command: "{{ item }}"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
loop:
- "{{ php_cli_path }} occ db:add-missing-indices"
- "{{ php_cli_path }} occ db:convert-filecache-bigint --no-interaction"
ignore_errors: yes
- name: 3.5 - Disable maintenance mode (success)
command: "{{ php_cli_path }} occ maintenance:mode --off"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
- name: 3.6 - Re-enable disabled apps
command: "{{ php_cli_path }} occ app:enable {{ item }}"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
loop: "{{ disabled_apps | default([]) }}"
when: disabled_apps is defined and disabled_apps | length > 0
ignore_errors: yes
- name: 3.7 - Restart web service
service:
name: "{{ web_service_name }}"
state: restarted
- name: 3.8 - Verify version after upgrade
command: "{{ php_cli_path }} occ status"
args:
chdir: "{{ nextcloud_dir }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
register: occ_status_after
changed_when: false
- name: 3.9 - Final version info
debug: debug:
var: occ_status_after.stdout msg: "Nextcloud is already at the latest version. Nothing to upgrade."
when: "'No update available' in updater_output.stdout"
- name: 3.10 - Upgrade complete # ───────────────────────────────
debug: # 3.3 — DATABASE UPGRADE
msg: "Nextcloud successfully upgraded!" # ───────────────────────────────
- name: 3.3 - Run occ upgrade (only if update occurred)
command: "{{ php_bin }} occ upgrade"
args:
chdir: "{{ nextcloud_path }}"
register: occ_upgrade_output
when: "'No update available' not in updater_output.stdout"
rescue: rescue:
- name: ❌ UPGRADE FAILED - Details - name: ❌ UPGRADE FAILED - Details
debug: debug:
msg: | msg: |
!!!!!!!!!!! NEXTCLOUD UPGRADE FAILED !!!!!!!!!!!! !!!!!!!!!!! NEXTCLOUD UPGRADE FAILED !!!!!!!!!!!!
Host: {{ inventory_hostname }}
Maintenance mode will be disabled.
Check logs: Check logs:
- {{ nextcloud_dir }}/data/updater.log - {{ nextcloud_path }}/data/updater.log
- {{ nextcloud_dir }}/data/nextcloud.log - {{ nextcloud_path }}/data/nextcloud.log
- name: Mark failure
set_fact:
upgrade_failed: true
always: always:
- name: ALWAYS disable maintenance mode (success or failure) - name: ALWAYS disable maintenance mode
command: "{{ php_cli_path }} occ maintenance:mode --off" command: "{{ php_bin }} occ maintenance:mode --off"
args: args:
chdir: "{{ nextcloud_dir }}" chdir: "{{ nextcloud_path }}"
become_user: "{{ nc_web_user }}"
environment: "{{ occ_env }}"
ignore_errors: yes
- name: Final failure (only triggered if rescue was hit) post_tasks:
fail:
- name: Final status
debug:
msg: >-
Upgrade finished successfully (or nothing to update).
when: upgrade_failed is not defined
- name: Final failure
debug:
msg: "Nextcloud upgrade failed — maintenance mode disabled." msg: "Nextcloud upgrade failed — maintenance mode disabled."
when: ansible_failed_task is defined when: upgrade_failed is defined