diff --git a/windows/update-beszel.yaml b/windows/update-beszel.yaml new file mode 100644 index 0000000..4e91a34 --- /dev/null +++ b/windows/update-beszel.yaml @@ -0,0 +1,54 @@ +--- +# Upgrade the Beszel agent on Windows hosts. +# Agent is installed with scoop (in the login user's profile) and run as an NSSM service "beszel-agent". +# Service is stopped during the update because Windows locks the running exe. + +- name: Upgrade Beszel agent (Windows) + hosts: windows + gather_facts: false + vars: + scoop: 'C:\Users\Justin\scoop\shims\scoop.ps1' + agent_exe: 'C:\Users\Justin\scoop\shims\beszel-agent.exe' + service: beszel-agent + + tasks: + - name: Get current version + ansible.windows.win_command: '"{{ agent_exe }}" -v' + register: before + changed_when: false + + - name: Update scoop buckets + ansible.windows.win_shell: '& "{{ scoop }}" update' + changed_when: false + + - name: Check whether an update is available + ansible.windows.win_shell: '& "{{ scoop }}" status 2>&1 | Out-String' + register: status + changed_when: false + + - name: Update agent + when: "'beszel-agent' in status.stdout" + block: + - name: Stop service + ansible.windows.win_service: + name: "{{ service }}" + state: stopped + + - name: Scoop update beszel-agent + ansible.windows.win_shell: '& "{{ scoop }}" update beszel-agent 2>&1 | Out-String' + register: upd + + always: + - name: Start service + ansible.windows.win_service: + name: "{{ service }}" + state: started + + - name: Get new version + ansible.windows.win_command: '"{{ agent_exe }}" -v' + register: after + changed_when: before.stdout != after.stdout + + - name: Report + ansible.builtin.debug: + msg: "{{ before.stdout | trim }} -> {{ after.stdout | trim }}" diff --git a/windows/update.yaml b/windows/update.yaml index 4498b38..fd8ec9c 100644 --- a/windows/update.yaml +++ b/windows/update.yaml @@ -42,12 +42,19 @@ reboot_timeout: 3600 register: result when: not search_only | bool + # With reboot=false, updates blocked by the pending restart (WU_E_INSTALL_NOT_ALLOWED) + # are expected; only fail on other errors. + failed_when: >- + result.failed_update_count | default(0) | int > 0 and + (reboot | bool or + result.updates | dict2items | map(attribute='value') + | selectattr('failure_hresult_code', 'defined') + | rejectattr('failure_hresult_code', 'equalto', 2149842966) | list | length > 0) - name: Report ansible.builtin.debug: msg: - - "Installed: {{ result.installed_update_count }}" - - "Failed: {{ result.failed_update_count | default(0) }}" - - "Reboot still required: {{ result.reboot_required }}" - - "{{ result.updates | dict2items | map(attribute='value.title') | list }}" + - "Installed: {{ result.updates | dict2items | map(attribute='value') | selectattr('installed') | map(attribute='title') | list }}" + - "Not installed: {{ result.updates | dict2items | map(attribute='value') | rejectattr('installed') | map(attribute='title') | list }}" + - "Reboot still required: {{ result.reboot_required }}{{ ' - reboot and re-run to finish' if result.reboot_required else '' }}" when: not search_only | bool