Compare commits

..

77 Commits

Author SHA1 Message Date
2d1c4c9934 fix: remove dashes in jinja conditions 2026-07-14 20:36:24 +00:00
29ba93e096 fix: typo 2026-07-14 20:30:57 +00:00
09862a53c1 fix: wording 2026-07-14 20:24:40 +00:00
336f1fd466 fix: replace block scalar 2026-07-14 20:08:37 +00:00
2937ba4344 fix: replace dash 2026-07-14 20:02:28 +00:00
52eed8aade fix: replace arrows 2026-07-14 19:58:45 +00:00
6acf547df1 fix: removed carriage return 2026-07-14 19:49:56 +00:00
d7f3a7ef06 fix: typo 2026-07-14 19:45:54 +00:00
eccd940727 change: notification body 2026-07-14 19:40:26 +00:00
52eaeaae93 fix: syntax 2026-07-11 20:13:01 +00:00
c99279cb49 feat: rework notification 2026-07-11 17:03:16 +00:00
2d9c6ffb1d change: use mwexecf instead of deprecated mwexec 2026-07-08 19:43:06 +00:00
240791d22e fix: disable maintenance endpoint 2026-07-02 20:19:20 +00:00
86e0b4a23f fix: target version in notifications 2026-06-29 20:52:00 +00:00
1f5d99b5ae change: timeout from 3600 to 5400 2026-06-29 20:47:37 +00:00
cd0bfd956c change: timeout from 1800 to 3600 2026-06-28 20:16:54 +00:00
6cf28240df fix: typo 2026-06-28 17:49:58 +02:00
697ac311a7 fix: product dict 2026-06-28 17:28:17 +02:00
d08f207fc4 change: timeout after reboot from 900 to 1800 2026-06-28 16:49:25 +02:00
74fa6432db change: timeout after reboot from 300 to 900 2026-06-28 10:45:40 +00:00
8ab282eb95 add: 2nd set_fact for readability 2026-06-27 20:59:43 +00:00
3d18c01d5a fix: undefined variable 2026-06-27 20:50:01 +00:00
2404692a30 add: checks between update and upgrade 2026-06-27 20:45:47 +00:00
557f80979b revert: use fail instead of end_play 2026-06-26 20:49:46 +00:00
fac4a27b75 fix: use end_play instead of end_host 2026-06-26 20:19:07 +00:00
2d80a76353 fix: backslash 2026-06-26 20:08:16 +00:00
d5c36e3118 change: replace fail by end_host 2026-06-26 19:58:26 +00:00
ff19c2faae change: retries for firmware check 2026-06-26 19:56:01 +00:00
4a7e8ef7ca add: message rewrite using filters 2026-06-26 19:54:49 +00:00
3f2b208651 fix: notification title 2026-06-26 07:13:29 +00:00
fa334f90d4 add: firmware upgrade message 2026-06-26 07:12:32 +00:00
43e535143b add: firmware action 2026-06-25 21:09:35 +00:00
f0ded8ac17 fix: assertion on skip_update undefined 2026-06-23 21:39:34 +00:00
f78c8a6cd7 add: workaround for phase 2 also 2026-06-23 20:51:21 +00:00
cbcbc45d00 add: workaround for setting maintenance mode after the update 2026-06-23 20:50:49 +00:00
869faf720a add: any_errors_fatal for phase 2 and 3 2026-06-23 19:27:26 +00:00
5d39cf3172 change: align phase 2 with phase 3 2026-06-23 19:26:22 +00:00
137b13d94e fix: typo 2026-06-23 19:15:50 +00:00
b1008ec425 add: notification on success 2026-06-23 19:15:03 +00:00
22de950cf5 refactor: merge assert with the task before 2026-06-22 20:14:01 +00:00
fd0a9a83e8 change: use proxmox collection 2026-06-22 19:35:46 +00:00
79302e8139 test: add api_user for proxmox snapshot 2026-06-22 19:29:39 +00:00
1ca4807d4b fix: wip on notifications 2026-06-22 10:10:33 +00:00
4c086cfad5 test: define proxmox token ID in the task 2026-06-22 10:01:34 +00:00
b91ea08874 fix: remove useles assert 2026-06-22 08:26:27 +00:00
ac69a7a5c6 fix: check firmware version 2026-06-22 06:53:02 +00:00
0a576b7ec0 change: method to wait for the update 2026-06-21 21:34:05 +00:00
e46f78b971 fix: move the pause for the correct node 2026-06-21 20:44:41 +00:00
a8b0fca263 add: pause after CARP maintenace mode 2026-06-21 20:43:09 +00:00
bbb2e0f024 add: requirements.yml for community.proxmox 2026-06-21 20:05:31 +00:00
fbf38991d7 add: master update with Proxmox snapshoty 2026-06-21 20:05:31 +00:00
f672517cc8 add: skip backuo 2026-06-21 20:05:31 +00:00
03a1b243bc add: firmware update trigger 2026-06-21 20:05:31 +00:00
de745d74dc fix: use inventory_hostname 2026-06-21 20:05:31 +00:00
9c49b131e2 fix: user ansible_inventory instead of host 2026-06-21 20:05:31 +00:00
0912c0a253 add: ntfy notification for pre-check 2026-06-21 20:05:31 +00:00
e2a1e53082 add: changed_when 2026-06-21 20:05:31 +00:00
80d9befa6a add: register when enable maintenance 2026-06-21 20:05:31 +00:00
fcf5753f57 fix: variable 2026-06-21 20:05:31 +00:00
413694f47d add: start phase 2 2026-06-21 20:05:31 +00:00
2e912c7ad5 test: inventory var 2026-06-21 20:05:31 +00:00
755e736ba9 add: get vip status 2026-06-21 20:05:31 +00:00
12f3c65c39 add: reboot info 2026-06-21 20:05:31 +00:00
02b908c4cd fix: typo 2026-06-21 20:05:31 +00:00
d059b86dfc fix: variables 2026-06-21 20:05:31 +00:00
6e3814ce3f fix: var name 2026-06-21 20:05:31 +00:00
fe24fec3a3 fix: host variable for API url 2026-06-21 20:05:31 +00:00
7d51740a1a add: firmware check 2026-06-21 20:05:31 +00:00
ca01e8f17e Revert "test: unset validate certs"
This reverts commit 3c8375d6f4.
2026-06-21 20:05:31 +00:00
b7cf1499a7 test: unset validate certs 2026-06-21 20:05:31 +00:00
63db96d1bd fix: remove port 2026-06-21 20:05:31 +00:00
e1b7a88d1f test: removed vars 2026-06-21 20:05:31 +00:00
aaf111919d test: validate_certs to false 2026-06-21 20:05:31 +00:00
f35bd094c7 add: OPNsense HA cluster update playbook 2026-06-21 20:05:31 +00:00
98758f4fa7 add: proxmox_vmid in the inventory 2026-06-21 20:05:12 +00:00
e4771be50c add: pause after VM move 2026-06-19 09:46:37 +02:00
130db47180 add: OPNsense inventory 2026-06-18 18:45:45 +00:00
5 changed files with 373 additions and 434 deletions

View File

@@ -16,4 +16,5 @@ all:
cerbere-head1:
ansible_host: 192.168.88.2
main_role: MASTER
hypervisor: Proxmox
hypervisor: Proxmox
proxmox_vmid: 122

View File

@@ -0,0 +1,4 @@
---
collections:
- name: community.proxmox
version: "2.0.0"

View File

@@ -6,7 +6,6 @@
hosts: opnsense
any_errors_fatal: true
gather_facts: false
tasks:
- block:
@@ -47,456 +46,387 @@
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _firmware_status
until: _firmware_status.json.status != 'none'
retries: 12
delay: 5
register: _firmware_status
until: _firmware_status.json.status_msg != "Firmware status requires to check for update first to provide more information."
retries: 10
delay: 2
- name: Store firmware facts
- name: Store node facts
ansible.builtin.set_fact:
firmware_action: "{{ opnsense_action | default('update')}}"
firmware_status: "{{ _firmware_status.json.status }}"
fw_current_version: "{{ _firmware_status.json.product_version | default('unknown') }}"
fw_new_version: "{{ _firmware_status.json.upgrade_packages | selectattr('name', 'equalto', 'opnsense') | map(attribute='new_version') | first | default('') }}"
needs_reboot: "{{ true if _firmware_status.json.needs_reboot == '1' else false }}"
# 'pending' means updates exist; will be overwritten to success/rolled_back
# If it stays 'pending' after the run, a pre-update check failed
update_result: "{{ 'pending' if _firmware_status.json.status == 'update' else 'skipped' }}"
firmware_status_msg: "{{ _firmware_status.json.status_msg }}"
firmware_current_version: "{{ _firmware_status.json.product.product_version | default('unknown') }}"
firmware_product_series: "{{ _firmware_status.json.product.product_series | default('unknown') }}"
firmware_update_version: "{{ _firmware_status.json.upgrade_packages | selectattr('name', 'equalto', 'opnsense') | map(attribute='new_version') | first | default('') }}"
firmware_upgrade_version: "{{ _firmware_status.json.upgrade_major_version | default('unknown') }}"
firmware_upgrade_message: "{{ _firmware_status.json.upgrade_major_message | regex_replace('<[^>]+>', ' ') | regex_replace('\\s{2,}', ' ') | trim | default('') }}"
firmware_up_to_date: "{{ _firmware_status.json.status_msg == 'There are no updates available on the selected mirror.' }}"
needs_reboot: "{{ (_firmware_status.json.upgrade_needs_reboot == '1') if _firmware_status.json.status == 'upgrade' else (_firmware_status.json.needs_reboot == '1') }}"
total_vips: "{{ _vip_status.json.rowCount }}"
mismatched_vips: "{{ _vip_status.json.rows | rejectattr('status', 'equalto', main_role) | list | length }}"
in_maintenance: "{{ _vip_status.json.carp.maintenancemode }}"
- name: Display firmware status
ansible.builtin.debug:
msg: >-
{{ inventory_hostname }}: {{ firmware_status }}
({{ fw_current_version }}
{% if fw_new_version %} → {{ fw_new_version }} (reboot: {{ needs_reboot }}){% endif %})
- name: Resolve update-vs-upgrade specifics
ansible.builtin.set_fact:
firmware_target_kind: "{{ 'series' if firmware_status == 'upgrade' else 'version' }}"
firmware_target_value: "{{ firmware_upgrade_version if firmware_status == 'upgrade' else firmware_update_version }}"
- name: Warn — major upgrade skipped, handle manually
ansible.builtin.debug:
msg: >-
NOTICE: {{ inventory_hostname }} has a MAJOR upgrade available
({{ fw_new_version }}). This playbook handles minor updates only.
when: firmware_status == 'upgrade'
- name: Backup already updated
ansible.builtin.set_fact:
skip_update: true
firmware_status: "skipped"
delegate_to: "{{ groups['opnsense_backup'][0] }}"
delegate_facts: true
run_once: true
when: >-
(hostvars[groups['opnsense_backup'][0]].firmware_current_version
if hostvars[groups['opnsense_master'][0]].firmware_target_kind == 'version'
else hostvars[groups['opnsense_backup'][0]].firmware_product_series)
== hostvars[groups['opnsense_master'][0]].firmware_target_value
- name: Skip node {{ firmware_action }}
ansible.builtin.set_fact:
skip_update: true
firmware_status: "skipped"
when: firmware_up_to_date or firmware_action != firmware_status
- name: "Assert"
ansible.builtin.assert:
that:
- not in_maintenance
- mismatched_vips == 0
- total_vips > 0
quiet: true
rescue:
- name: Send Ntfy notification
ansible.builtin.uri:
url: "{{ ntfy_url }}/{{ ntfy_topic }}"
method: POST
user: "{{ ntfy_user }}"
password: "{{ lookup('env', 'NTFY_PASSWORD') }}"
force_basic_auth: true
body: |
Current version: {{ firmware_current_version }}
{% if in_maintenance %}
The node {{ inventory_hostname }} is in CARP maintenance mode.
{% elif mismatched_vips > 0 %}
Some VIP are not {{ main_role }} on {{ ansible_host }}.
{% elif total_vips == 0 %}
No VIPs are managed.
{% else %}
An error occured during the {{ firmware_action }}.
{% endif %}
headers:
Title: "OPNsense {{ firmware_action }} report"
Priority: "default"
Tags: "x"
when: ntfy_url is defined
- ansible.builtin.fail:
msg: "Update aborted"
# ###############################################################
# - name: "Phase 2 | Update backup node (TrueNAS, no snapshot)"
# hosts: opnsense_backup
# gather_facts: false
- name: "Phase 2 | Update backup node (TrueNAS, no snapshot)"
hosts: opnsense_backup
any_errors_fatal: true
gather_facts: false
tasks:
# tasks:
# - name: Skip — no minor updates available on backup
# ansible.builtin.meta: end_host
# when: firmware_status != 'update'
- when: not skip_update | default(false)
block:
# # Safety: confirm backup is actually in BACKUP role before touching it
# - name: Verify backup node CARP role
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/get"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _carp
# # Abort if node is unexpectedly MASTER — updating it would disrupt traffic
# failed_when: _carp.json.status | upper != 'BACKUP'
- name: Enable CARP maintenance mode
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/carp_status/maintenance"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _enable_maintenance
changed_when: _enable_maintenance.status == "ok"
# # Maintenance mode prevents the backup from attempting to take over
# # during its own reboot (avoids CARP flapping)
# - name: Enable CARP maintenance mode
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/maintenancemode"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
- name: Trigger firmware {{ firmware_action }}
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/{{ firmware_action }}"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
# - name: Trigger firmware update
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/update"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
- name: Check if the {{ firmware_action }} is running
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/running"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _update_running
until: _update_running.json.status == 'busy'
retries: 2
delay: 2
# - name: Wait for packages to install
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/running"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _running
# until: _running.json.status != 'running'
# retries: 30
# delay: 15
- name: Wait for node to reboot after the {{ firmware_action }}
ansible.builtin.wait_for:
host: "{{ ansible_host }}"
port: "{{ opnsense_https_port }}"
state: stopped
timeout: 3600
when: needs_reboot
# # Reboot is a separate explicit step — update does NOT auto-reboot
# - name: Reboot node
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/reboot"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
# ignore_errors: true # Node drops the connection immediately
- name: Wait for node to come back online
ansible.builtin.wait_for:
host: "{{ ansible_host }}"
port: "{{ opnsense_https_port }}"
state: started
timeout: 5400
delay: 30
when: needs_reboot
# - name: Wait for node to go offline
# ansible.builtin.wait_for:
# host: "{{ ansible_host }}"
# port: 443
# state: stopped
# timeout: 60
# delegate_to: localhost
- name: Check firmware version
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/status"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _post_firmware_status
until: _post_firmware_status.json.product['product_' ~ firmware_target_kind] | default('unknown') == firmware_target_value
retries: 240
delay: 15
# - name: Wait for node to come back online
# ansible.builtin.wait_for:
# host: "{{ ansible_host }}"
# port: 443
# state: started
# timeout: 300
# delay: 30
# delegate_to: localhost
- name: Check VIP status
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/get_vip_status"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _carp_post_udapte
# - name: Wait for API to be fully responsive
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/status"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _post_status
# until: _post_status.status == 200
# retries: 20
# delay: 10
- name: Disable CARP maintenance mode
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/carp_status/maintenance"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
when: _carp_post_udapte.json.carp.maintenancemode
ignore_errors: true
# - name: Health check — no remaining updates
# ansible.builtin.assert:
# that:
# - _post_status.json.status == 'none'
# fail_msg: >-
# Post-update firmware status is '{{ _post_status.json.status }}'
# on {{ inventory_hostname }}. Expected 'none'.
rescue:
# - name: Health check — CARP role is BACKUP
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/get"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _carp_post
# failed_when: _carp_post.json.status | upper not in ['BACKUP', 'MAINTENANCE']
- name: Send Ntfy notification
ansible.builtin.uri:
url: "{{ ntfy_url }}/{{ ntfy_topic }}"
method: POST
user: "{{ ntfy_user }}"
password: "{{ lookup('env', 'NTFY_PASSWORD') }}"
force_basic_auth: true
body: |
Current version: {{ firmware_current_version }}
{{ firmware_action | capitalize }} to {{ firmware_target_value }} failed on {{ inventory_hostname }} ({{ main_role }}).
No snapshot have been taken before the {{ firmware_action }}.
headers:
Title: "OPNsense {{ firmware_action }} failed on {{ inventory_hostname }}"
Priority: "high"
Tags: "x"
when: ntfy_url is defined
# - name: Disable CARP maintenance mode
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/maintenancemode"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
- ansible.builtin.fail:
msg: "Update failed"
# - name: Record backup update result
# ansible.builtin.set_fact:
# update_result: "success"
# fw_post_version: "{{ _post_status.json.product_version | default('unknown') }}"
- name: "Phase 3 | Update master node (Proxmox, with snapshot)"
hosts: opnsense_master
any_errors_fatal: true
gather_facts: false
vars:
proxmox_snap_name: "preupdate-{{ now().strftime('%Y%m%d') }}"
tasks:
- when: not skip_update | default(false)
block:
- name: Take Proxmox VM snapshot
community.proxmox.proxmox_snap:
vmid: "{{ proxmox_vmid }}"
state: present
snapname: "{{ proxmox_snap_name }}"
description: "Pre-firmware-{{ firmware_action }}: {{ firmware_current_version }} → {{ firmware_target_value }}"
- name: Enable CARP maintenance mode
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/carp_status/maintenance"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _enable_maintenance
changed_when: _enable_maintenance.status == "ok"
- name: Wait for CARP failover to settle
ansible.builtin.pause:
seconds: 5
- name: Trigger firmware {{ firmware_action }}
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/{{ firmware_action }}"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
- name: Check if the {{ firmware_action }} is running
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/running"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _update_running
until: _update_running.json.status == 'busy'
retries: 2
delay: 2
- name: Wait for node to reboot after the {{ firmware_action }}
ansible.builtin.wait_for:
host: "{{ ansible_host }}"
port: "{{ opnsense_https_port }}"
state: stopped
timeout: 3600
when: needs_reboot
- name: Wait for node to come back online
ansible.builtin.wait_for:
host: "{{ ansible_host }}"
port: "{{ opnsense_https_port }}"
state: started
timeout: 5400
delay: 30
when: needs_reboot
- name: Check firmware version
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/core/firmware/status"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _post_firmware_status
until: _post_firmware_status.json.product['product_' ~ firmware_target_kind] | default('unknown') == firmware_target_value
retries: 240
delay: 15
- name: Check VIP status
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/get_vip_status"
method: GET
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
register: _carp_post_udapte
- name: Disable CARP maintenance mode
ansible.builtin.uri:
url: "https://{{ opnsense_host }}/api/diagnostics/interface/carp_status/maintenance"
method: POST
user: "{{ opnsense_api_key }}"
password: "{{ opnsense_api_secret }}"
force_basic_auth: true
validate_certs: false
when: _carp_post_udapte.json.carp.maintenancemode
ignore_errors: true
rescue:
- name: Rollback VM to the pre-{{ firmware_action }} snapshot
community.proxmox.proxmox_snap:
vmid: "{{ proxmox_vmid }}"
state: rollback
snapname: "{{ proxmox_snap_name }}"
register: _proxmox_snapshot_rollback
ignore_errors: true
- name: Send Ntfy notification
ansible.builtin.uri:
url: "{{ ntfy_url }}/{{ ntfy_topic }}"
method: POST
user: "{{ ntfy_user }}"
password: "{{ lookup('env', 'NTFY_PASSWORD') }}"
force_basic_auth: true
body: |
Current version: {{ firmware_current_version }}
{{ firmware_action | capitalize }} to {{ firmware_target_value }} failed on {{ inventory_hostname }} ({{ main_role }}).
A snapshot have been taken before the {{ firmware_action }} and the VM has been rollback.
headers:
Title: "OPNsense {{ firmware_action }} failed on {{ inventory_hostname }}"
Priority: "high"
Tags: "x"
when: ntfy_url is defined
- ansible.builtin.fail:
msg: "Update aborted"
# ###############################################################
# - name: "Phase 3 | Update master node (Proxmox, with snapshot)"
# hosts: opnsense_master
# gather_facts: false
- name: "Phase 4 | Notification"
hosts: localhost
gather_facts: false
tasks:
# vars:
# snap_name: "preupdate-{{ now().strftime('%Y%m%d-%H%M') }}"
# _pve_base: "https://{{ proxmox_api_host }}/api2/json/nodes/{{ proxmox_node }}/qemu/{{ proxmox_vmid }}"
# _pve_auth:
# Authorization: "PVEAPIToken={{ proxmox_api_token_id }}={{ proxmox_api_token_secret }}"
- name: Send Ntfy notification
vars:
master: "{{ groups['opnsense_master'][0] }}"
backup: "{{ groups['opnsense_backup'][0] }}"
m: "{{ hostvars[master] }}"
b: "{{ hostvars[backup] }}"
same_operation: "{{ m.firmware_status == b.firmware_status }}"
# tasks:
# - name: Skip — no minor updates available on master
# ansible.builtin.meta: end_host
# when: firmware_status != 'update'
# - name: Verify master node CARP role
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/get"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _carp
# failed_when: _carp.json.status | upper != 'MASTER'
# # Snapshot BEFORE maintenance mode — the restore point should be
# # a fully functioning MASTER node, not one mid-failover
# - name: Take Proxmox VM snapshot
# community.general.proxmox_snap:
# api_host: "{{ proxmox_api_host }}"
# api_token_id: "{{ proxmox_api_token_id }}"
# api_token_secret: "{{ proxmox_api_token_secret }}"
# vmid: "{{ proxmox_vmid }}"
# state: present
# snapname: "{{ snap_name }}"
# description: "Pre-firmware-update: {{ fw_current_version }} → {{ fw_new_version }}"
# timeout: 60
# delegate_to: localhost
# - name: Enable CARP maintenance mode (triggers controlled failover to backup)
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/maintenancemode"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
# - name: Wait for CARP failover to settle
# ansible.builtin.pause:
# seconds: 10
# # Do not proceed if the backup has not actually taken over.
# # A failed failover means no traffic coverage during master update.
# - name: Confirm backup node is now MASTER
# ansible.builtin.uri:
# url: "https://{{ hostvars[groups['opnsense_backup'][0]].ansible_host }}/api/carp/status/get"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _backup_carp
# delegate_to: localhost
# failed_when: _backup_carp.json.status | upper != 'MASTER'
# - name: Update and verify
# block:
# - name: Trigger firmware update
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/update"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
# - name: Wait for packages to install
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/running"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _running
# until: _running.json.status != 'running'
# retries: 30
# delay: 15
# - name: Reboot node
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/reboot"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
# ignore_errors: true
# - name: Wait for node to go offline
# ansible.builtin.wait_for:
# host: "{{ ansible_host }}"
# port: 443
# state: stopped
# timeout: 60
# delegate_to: localhost
# - name: Wait for node to come back online
# ansible.builtin.wait_for:
# host: "{{ ansible_host }}"
# port: 443
# state: started
# timeout: 300
# delay: 30
# delegate_to: localhost
# - name: Wait for API to be fully responsive
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/core/firmware/status"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _post_status
# until: _post_status.status == 200
# retries: 20
# delay: 10
# - name: Health check — no remaining updates
# ansible.builtin.assert:
# that:
# - _post_status.json.status == 'none'
# fail_msg: >-
# Post-update firmware status is '{{ _post_status.json.status }}'.
# Expected 'none'. Triggering rollback.
# # After maintenance mode failover, master should rejoin as BACKUP
# - name: Health check — CARP role is BACKUP
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/get"
# method: GET
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# register: _carp_post
# failed_when: _carp_post.json.status | upper not in ['BACKUP', 'MAINTENANCE']
# - name: Record master update result
# ansible.builtin.set_fact:
# update_result: "success"
# fw_post_version: "{{ _post_status.json.product_version | default('unknown') }}"
# rescue:
# # Snapshot was taken before maintenance mode, so the restored VM
# # will come back as a healthy MASTER on the previous version.
# # Backup (already updated) continues serving traffic during rollback.
# - name: "ROLLBACK | Stop VM"
# ansible.builtin.uri:
# url: "{{ _pve_base }}/status/stop"
# method: POST
# headers: "{{ _pve_auth }}"
# validate_certs: false
# delegate_to: localhost
# - name: "ROLLBACK | Wait for VM to stop"
# ansible.builtin.uri:
# url: "{{ _pve_base }}/status/current"
# method: GET
# headers: "{{ _pve_auth }}"
# validate_certs: false
# register: _vm_state
# until: _vm_state.json.data.status == 'stopped'
# retries: 12
# delay: 5
# delegate_to: localhost
# - name: "ROLLBACK | Restore snapshot {{ snap_name }}"
# ansible.builtin.uri:
# url: "{{ _pve_base }}/snapshot/{{ snap_name }}/rollback"
# method: POST
# headers: "{{ _pve_auth }}"
# validate_certs: false
# delegate_to: localhost
# # Proxmox rollback tasks run async; wait for completion
# - name: "ROLLBACK | Wait for snapshot restore to complete"
# ansible.builtin.pause:
# seconds: 30
# - name: "ROLLBACK | Start VM"
# ansible.builtin.uri:
# url: "{{ _pve_base }}/status/start"
# method: POST
# headers: "{{ _pve_auth }}"
# validate_certs: false
# delegate_to: localhost
# - name: "ROLLBACK | Wait for VM to come back online"
# ansible.builtin.wait_for:
# host: "{{ ansible_host }}"
# port: 443
# state: started
# timeout: 300
# delay: 30
# delegate_to: localhost
# - name: "ROLLBACK | Record result"
# ansible.builtin.set_fact:
# update_result: "rolled_back"
# fw_post_version: "{{ fw_current_version }}"
# always:
# # Best-effort: disable maintenance mode whether update succeeded or rolled back.
# # On rollback, the restored snapshot predates maintenance mode so this
# # is effectively a no-op, but run it anyway as a safety net.
# - name: Disable CARP maintenance mode
# ansible.builtin.uri:
# url: "https://{{ opnsense_host }}/api/carp/status/maintenancemode"
# method: POST
# user: "{{ opnsense_api_key }}"
# password: "{{ opnsense_api_secret }}"
# force_basic_auth: true
# validate_certs: false
# body_format: json
# body: {}
# ignore_errors: true
# ###############################################################
# # Phase 4 targets localhost — it runs even if Phase 3 failed,
# # because localhost was never marked as a failed host.
# - name: "Phase 4 | Send ntfy notification"
# hosts: localhost
# gather_facts: false
# tasks:
# - name: Build report
# ansible.builtin.set_fact:
# _report: |
# {% for host in groups['opnsense'] %}
# {% set h = hostvars[host] %}
# {{ host }}
# fw check : {{ h.firmware_status | default('unknown') }}
# result : {{ h.update_result | default('unknown') }}
# version : {{ h.fw_post_version | default(h.fw_current_version | default('?')) }}
# {% endfor %}
# _any_issue: >-
# {{ groups['opnsense']
# | map('extract', hostvars, 'update_result')
# | select('in', ['rolled_back', 'pending'])
# | list
# | length > 0 }}
# - name: Send ntfy notification
# ansible.builtin.uri:
# url: "{{ ntfy_url }}/{{ ntfy_topic }}"
# method: POST
# headers:
# Authorization: "Bearer {{ ntfy_token }}"
# Title: "OPNsense HA Update Report"
# Priority: "{{ 'high' if _any_issue else 'default' }}"
# Tags: "{{ 'warning' if _any_issue else 'white_check_mark' }},shield"
# Content-Type: "text/plain"
# body: "{{ _report }}"
# - name: Fail playbook if any node was rolled back or did not complete
# ansible.builtin.fail:
# msg: >-
# One or more nodes did not update successfully.
# Check ntfy report and Ansible output for details.
# when: _any_issue | bool
ansible.builtin.uri:
url: "{{ ntfy_url }}/{{ ntfy_topic }}"
method: POST
user: "{{ ntfy_user }}"
password: "{{ lookup('env', 'NTFY_PASSWORD') }}"
force_basic_auth: true
body: |
{% if same_operation %}
{% if m.skip_update | default(false) %}
Both nodes already on {{ m.firmware_current_version }}, no action taken.
{% else %}
OPNsense cluster: {{ m.firmware_current_version }} → {{ m.firmware_target_value }} ({{ m.firmware_status }})
{% endif %}
{% else %}
{% if m.skip_update | default(false) %}
MASTER ({{ master }}): already on {{ m.firmware_current_version }}, no action taken.
{% else %}
MASTER ({{ master }}): {{ m.firmware_current_version }} → {{ m.firmware_target_value }} ({{ m.firmware_status }})
{% endif %}
{% if b.skip_update | default(false) %}
BACKUP ({{ backup }}): already on {{ b.firmware_current_version }}, no action taken.
{% else %}
BACKUP ({{ backup }}): {{ b.firmware_current_version }} → {{ b.firmware_target_value }} ({{ b.firmware_status }})
{% endif %}
{% endif %}
headers:
Title: >-
{%- if m.skip_update | default(false) and b.skip_update | default(false) -%}
OPNsense cluster - no {{m.firmware_action }} available
{%- else -%}
OPNsense cluster {{m.firmware_action }} completed
{%- endif -%}
Priority: "{{ 'min' if (m.skip_update | default(false) and b.skip_update | default(false)) else 'default' }}"
Tags: "{{ 'information_source' if (m.skip_update | default(false) and b.skip_update | default(false)) else 'white_check_mark' }}"
when: ntfy_url is defined

View File

@@ -94,6 +94,10 @@
retries: 60
delay: 15
- name: Pause 15s
ansible.builtin.pause:
seconds: 15
- name: Update packages
ansible.builtin.apt:
upgrade: full

View File

@@ -84,7 +84,7 @@ if ($type === "MASTER") {
write_config("disable interface '$ifkey' due CARP event '$type'", false);
interface_configure(false, $ifkey, false, false);
// Remove WAN default gateway
mwexec("/sbin/route delete default");
mwexecf("/sbin/route delete default");
foreach ($config['OPNsense']['Gateways']['gateway_item'] as $gw) {
if ($gw['name'] === $gw_name) {
$gw_ip = $gw['gateway'];
@@ -92,9 +92,9 @@ if ($type === "MASTER") {
}
}
// Shutdown WAN interface
mwexec("/sbin/ifconfig {$real_if} down")
mwexecf("/sbin/ifconfig {$real_if} down")
// Add fallback default gateway
mwexec("/sbin/route add default {$gw_ip}");
mwexecf("/sbin/route add default {$gw_ip}");
// Create lock file
file_put_contents($lock_file, time());
sleep($lock_default_age);