Files
ansible/playbooks/nextcloud-upgrade.yml
T

141 lines
4.9 KiB
YAML

---
- name: Safe Nextcloud Upgrade Playbook
hosts: nextcloud
become: yes
vars:
nc_path: /var/www/nextcloud
php_bin: /usr/bin/php
tasks:
- name: Pause playbook for 3 seconds
pause:
seconds: 3
prompt: "Press Ctrl+C then A to abort if no backups exist."
###########################################################################
# 1 - CHECK CURRENT INSTALLED VERSION
###########################################################################
- name: 1.1 - Get current NC status
command: "{{ php_bin }} occ status"
args:
chdir: "{{ nc_path }}"
register: nc_status
- name: 1.2 - Extract installed version
set_fact:
installed_version: "{{ nc_status.stdout | regex_search('versionstring: (.*)', '\\1') }}"
- name: 1.3 - Show installed version
debug:
msg: "Current installed version: {{ installed_version }}"
###########################################################################
# 2 - CHECK LATEST AVAILABLE VERSION (via updater.phar)
###########################################################################
- name: 2.1 - Check for available update
command: "{{ php_bin }} updater/updater.phar --check"
args:
chdir: "{{ nc_path }}"
register: update_check
failed_when: false # we want to parse output even if exit code != 0
- name: 2.2 - Parse latest available version
set_fact:
available_version: "{{ update_check.stdout | regex_search('Update to version (.*) available', '\\1') }}"
when: update_check.stdout is search('Update to version')
- name: 2.3 - Show available version
debug:
msg: "Latest available version: {{ available_version | default('NONE') }}"
###########################################################################
# 2.4 - SKIP IF ALREADY UP TO DATE
###########################################################################
- name: 2.4 - Skip upgrade if already on latest version
debug:
msg: "Nextcloud is already up to date ({{ installed_version }}). Skipping upgrade."
when: available_version is not defined
tags: skip_upgrade
changed_when: false
- name: End play if up to date
meta: end_play
when: available_version is not defined
###########################################################################
# 3 - DISABLE APPS + MAINTENANCE MODE
###########################################################################
- name: 3.1 - Enable maintenance mode
command: "{{ php_bin }} occ maintenance:mode --on"
args:
chdir: "{{ nc_path }}"
###########################################################################
# 3.2 - RUN UPDATER WITH RETRIES
###########################################################################
- name: 3.2 - Run Nextcloud CLI updater (retry)
command: "{{ php_bin }} updater/updater.phar --no-interaction"
args:
chdir: "{{ nc_path }}"
register: updater_output
retries: 3
delay: 5
until: updater_output.rc == 0
failed_when: updater_output.rc != 0
###########################################################################
# 3.3 - OCC UPGRADE (DB MIGRATIONS)
###########################################################################
- block:
- name: 3.3 - Run occ upgrade
command: "{{ php_bin }} occ upgrade"
args:
chdir: "{{ nc_path }}"
register: upgrade_run
failed_when: >
upgrade_run.rc != 0 and
"already in process" not in upgrade_run.stdout and
"maintenance mode" not in upgrade_run.stdout
rescue:
- name: 3.3R - Handle locked upgrade
debug:
msg: "Upgrade appears stuck. Removing maintenance lock and retrying."
- name: Remove maintenance mode flag from config.php
lineinfile:
path: "{{ nc_path }}/config/config.php"
regexp: "'maintenance' => true"
line: " 'maintenance' => false,"
backrefs: yes
- name: Retry OCC upgrade
command: "{{ php_bin }} occ upgrade"
args:
chdir: "{{ nc_path }}"
register: upgrade_retry
failed_when: upgrade_retry.rc != 0
###########################################################################
# 4 - ALWAYS DISABLE MAINTENANCE MODE
###########################################################################
- name: ALWAYS disable maintenance mode
command: "{{ php_bin }} occ maintenance:mode --off"
args:
chdir: "{{ nc_path }}"
ignore_errors: true
###########################################################################
# 5 - FINAL RESULT
###########################################################################
- name: Show final upgrade result
debug:
msg: |
✔ Nextcloud upgrade completed.
Installed: {{ installed_version }}
Updated to: {{ available_version }}
Check logs if issues appear:
- {{ nc_path }}/data/updater.log
- {{ nc_path }}/data/nextcloud.log