From 3c7ce0f8c520891b804c0b652f7462353853132f Mon Sep 17 00:00:00 2001 From: max Date: Sun, 22 Jun 2025 18:39:27 +0200 Subject: [PATCH] Added truenas update && playbook for fedora and debian hosts --- Inventory/hosts.yaml | 5 +- .../update-debian-hosts.yaml} | 3 +- .../update-fedora-hosts.yaml} | 0 Playbooks/machine-update/update-machines.yaml | 10 +++ Playbooks/truenas/README.md | 2 + Playbooks/truenas/update-truenas-task.yaml | 61 +++++++++++++++++++ Playbooks/truenas/update-truenas.yaml | 14 +++++ 7 files changed, 93 insertions(+), 2 deletions(-) rename Playbooks/{update-apt.yaml => machine-update/update-debian-hosts.yaml} (62%) rename Playbooks/{update-dnf.yaml => machine-update/update-fedora-hosts.yaml} (100%) create mode 100644 Playbooks/machine-update/update-machines.yaml create mode 100644 Playbooks/truenas/README.md create mode 100644 Playbooks/truenas/update-truenas-task.yaml create mode 100644 Playbooks/truenas/update-truenas.yaml diff --git a/Inventory/hosts.yaml b/Inventory/hosts.yaml index 4568980..74bf383 100644 --- a/Inventory/hosts.yaml +++ b/Inventory/hosts.yaml @@ -34,4 +34,7 @@ all: compute-1: pve: hosts: - pve-R730-3GDWF82: \ No newline at end of file + pve-R730-3GDWF82: + nas: + hosts: + truenas: \ No newline at end of file diff --git a/Playbooks/update-apt.yaml b/Playbooks/machine-update/update-debian-hosts.yaml similarity index 62% rename from Playbooks/update-apt.yaml rename to Playbooks/machine-update/update-debian-hosts.yaml index 03f05fb..7a3c803 100644 --- a/Playbooks/update-apt.yaml +++ b/Playbooks/machine-update/update-debian-hosts.yaml @@ -5,4 +5,5 @@ - name: update APT packages apt: upgrade: yes - update_cache: yes \ No newline at end of file + update_cache: yes + cache_valid_time: 86400 \ No newline at end of file diff --git a/Playbooks/update-dnf.yaml b/Playbooks/machine-update/update-fedora-hosts.yaml similarity index 100% rename from Playbooks/update-dnf.yaml rename to Playbooks/machine-update/update-fedora-hosts.yaml diff --git a/Playbooks/machine-update/update-machines.yaml b/Playbooks/machine-update/update-machines.yaml new file mode 100644 index 0000000..c55ca56 --- /dev/null +++ b/Playbooks/machine-update/update-machines.yaml @@ -0,0 +1,10 @@ +--- +- hosts: fedora + tasks: + - name: Update fedora hosts + include_tasks: update-fedora-hosts.yaml + +- hosts: debian + tasks: + - name: Update debian hosts + include_tasks: update-debian-hosts.yaml diff --git a/Playbooks/truenas/README.md b/Playbooks/truenas/README.md new file mode 100644 index 0000000..cae8aba --- /dev/null +++ b/Playbooks/truenas/README.md @@ -0,0 +1,2 @@ +# Git +https://github.com/fanuelsen/ansible/tree/master/ansible-truenas-api \ No newline at end of file diff --git a/Playbooks/truenas/update-truenas-task.yaml b/Playbooks/truenas/update-truenas-task.yaml new file mode 100644 index 0000000..6d4992a --- /dev/null +++ b/Playbooks/truenas/update-truenas-task.yaml @@ -0,0 +1,61 @@ +- name: Checks if there is an update available from update server. + uri: + method: POST + url: https://{{ item.ip }}/api/v2.0/update/check_available + return_content: true + validate_certs: '{{ item.validate_certs }}' + headers: + Content-Type: application/json + Authorization: 'Bearer {{ item.token }}' + register: truenas_pending_updates + +- name: Downloads (if not already in cache) and apply an update. + uri: + method: POST + url: https://{{ item.ip }}/api/v2.0/update/update + return_content: true + validate_certs: '{{ item.validate_certs }}' + headers: + Content-Type: application/json + Authorization: 'Bearer {{ item.token }}' + when: "truenas_pending_updates.json.status == 'AVAILABLE'" + +- name: Wait for update to finish. + uri: + method: POST + url: https://{{ item.ip }}/api/v2.0/update/check_available + return_content: true + validate_certs: '{{ item.validate_certs }}' + headers: + Content-Type: application/json + Authorization: 'Bearer {{ item.token }}' + register: truenas_reboot_required + when: "truenas_pending_updates.json.status == 'AVAILABLE' or truenas_pending_updates.json.status == 'REBOOT_REQUIRED'" + until: "(truenas_reboot_required.json|default(dict(status=WAITING))).status == 'REBOOT_REQUIRED'" + retries: 20 + delay: 30 + +- name: Reboot host + uri: + method: POST + url: https://{{ item.ip }}/api/v2.0/system/reboot + return_content: true + validate_certs: '{{ item.validate_certs }}' + headers: + Content-Type: application/json + Authorization: 'Bearer {{ item.token }}' + when: truenas_reboot_required.json.status | default (false) == 'REBOOT_REQUIRED' + +- name: Returns whether the system completed boot and is ready to use. + uri: + method: GET + url: https://{{ item.ip }}/api/v2.0/system/state + return_content: true + validate_certs: '{{ item.validate_certs }}' + headers: + Content-Type: application/json + Authorization: 'Bearer {{ item.token }}' + register: truenas_system_ready + until: truenas_system_ready.content == '"READY"' + retries: 20 + delay: 30 \ No newline at end of file diff --git a/Playbooks/truenas/update-truenas.yaml b/Playbooks/truenas/update-truenas.yaml new file mode 100644 index 0000000..8e4a665 --- /dev/null +++ b/Playbooks/truenas/update-truenas.yaml @@ -0,0 +1,14 @@ +--- +- hosts: truenas + gather_facts: false + + vars: + sites: + - ip: 192.168.3.1 + token: 1-gAIQbRICSPPqM1JCsfofc8gJgLvV8SglXV2xemYzBr2rzdZvQd5J0pMYeG45Jiqx + validate_certs: false + + tasks: + - name: update-truenas-api + include_tasks: update-truenas-task.yml + loop: '{{ sites }}' \ No newline at end of file