--- - name: Check installed cloudflared version ansible.builtin.command: argv: ["{{ cloudflared_install_dir }}/cloudflared", --version] register: __cloudflared_version_cmd changed_when: false # Failure is expected when the binary is missing (rc 2): empty stdout falls through to 0.0.0 # below, which triggers the install path. failed_when: false # Read-only, so it also runs in the --check preview. Skipped there, stdout would be empty, the # version would fall back to 0.0.0 and the sha256 guard below would fail against an up-to-date # binary. check_mode: false tags: [cloudflared, update] - name: Extract installed cloudflared version ansible.builtin.set_fact: __cloudflared_installed_version: >- {{ (__cloudflared_version_cmd.stdout | regex_findall('([0-9]{4}\.[0-9]+\.[0-9]+)') | first) | default('0.0.0') }} tags: [cloudflared, update] - name: Determine if cloudflared update required ansible.builtin.set_fact: __cloudflared_update_required: "{{ __cloudflared_installed_version != cloudflared_version }}" # Next to the live binary, so the swap is a rename on one filesystem. __cloudflared_staged_binary: "{{ cloudflared_install_dir }}/cloudflared.new" tags: [cloudflared, update] # A hash left over from an older release (a version bumped without its hash) would fail the # staged download below with a bare checksum mismatch. Say what is wrong instead. Read-only, so # it also runs in the --check preview. - name: Check the installed binary against the configured sha256 ansible.builtin.stat: path: "{{ cloudflared_install_dir }}/cloudflared" checksum_algorithm: sha256 get_checksum: true register: __cloudflared_installed_stat when: __cloudflared_update_required tags: [cloudflared, update] - name: Fail when cloudflared_sha256_amd64 is the hash of the installed binary ansible.builtin.assert: that: - (__cloudflared_installed_stat.stat.checksum | default('')) | lower != cloudflared_sha256_amd64 | lower fail_msg: >- cloudflared_sha256_amd64 equals the sha256 of the binary installed on {{ inventory_hostname }} (version {{ __cloudflared_installed_version }}), but cloudflared_version is {{ cloudflared_version }}. The hash is stale: set it to the sha256 of the cloudflared-linux-amd64 asset of release {{ cloudflared_version }}. quiet: true when: __cloudflared_update_required tags: [cloudflared, update] # Download and verify before anything is stopped: a failed download or checksum leaves the # running daemon untouched. mortis.kamino._binary_update then stops, backs up, swaps, restarts, # checks the version and rolls back on failure, and removes the staged binary. A binary that # reports no version (0.0.0 above) is replaced too; _binary_update backs it up when it exists. - name: Stage and swap in the new cloudflared binary when: __cloudflared_update_required block: # Its own rescue, so that only a staging failure gets this message: a failure in the swap # below keeps the rollback message of mortis.kamino._binary_update. - name: Stage the new cloudflared binary block: - name: Download and verify into the staging path ansible.builtin.include_tasks: download.yml vars: __cloudflared_download_dest: "{{ __cloudflared_staged_binary }}" tags: [cloudflared, update] rescue: - name: Fail the staging ansible.builtin.fail: msg: >- Staging cloudflared {{ cloudflared_version }} failed ({{ ansible_failed_result.msg | default('see the task above') }}). cloudflared was not stopped and still runs {{ __cloudflared_installed_version }}. tags: [cloudflared, update] # An include does not pass its tags on to the included tasks, hence apply. - name: Swap in the staged cloudflared binary ansible.builtin.include_role: name: mortis.kamino._binary_update apply: tags: [cloudflared, update] vars: _binary_update_service: cloudflared _binary_update_binary_path: "{{ cloudflared_install_dir }}/cloudflared" _binary_update_staged_path: "{{ __cloudflared_staged_binary }}" _binary_update_version_command: ["{{ cloudflared_install_dir }}/cloudflared", --version] _binary_update_expected_version: "{{ cloudflared_version }}" _binary_update_version_regex: '([0-9]{4}\.[0-9]+\.[0-9]+)' tags: [cloudflared, update] always: # Left behind only when the run failed before the swap; get_url would reuse a matching # file anyway, but a stale binary next to the live one is clutter. - name: Remove the staged cloudflared binary become: true ansible.builtin.file: path: "{{ __cloudflared_staged_binary }}" state: absent tags: [cloudflared, update]