--- - name: Check current Traefik version ansible.builtin.command: "{{ traefik_bin_path }} version" register: traefik_version_cmd changed_when: false # Failure is expected before the first install (binary missing): empty stdout falls through # to the default version below ("v0.0.0"), which differs from traefik_version and so triggers # the update 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 an up-to-date binary would look like it needs an install. check_mode: false tags: [traefik, update] - name: Extract installed Traefik version ansible.builtin.set_fact: traefik_installed_version: >- {{ 'v' + (traefik_version_cmd.stdout | regex_findall('v?([0-9]+\.[0-9]+\.[0-9]+)') | first | default('0.0.0')) }} tags: [traefik, update] - name: Determine if Traefik update required ansible.builtin.set_fact: traefik_update_required: "{{ traefik_installed_version != traefik_version }}" # Next to the live binary, so the swap is a rename on one filesystem. traefik_staging_dir: "{{ traefik_bin_path | dirname }}/.traefik-staging" tags: [traefik, update] # Download and verify before anything is stopped: a failed download or checksum leaves the # running Traefik untouched. mortis.kamino._binary_update then stops, backs up, swaps, restarts, # checks the version and rolls back on failure, and removes the staged binary. - name: Stage and swap in the new Traefik binary when: traefik_update_required block: - name: Create the Traefik staging directory become: true ansible.builtin.file: path: "{{ traefik_staging_dir }}" state: directory owner: root group: root mode: "0700" tags: [traefik, update] # 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 Traefik binary block: - name: Download and verify into the staging directory ansible.builtin.include_tasks: download.yml vars: traefik_download_dir: "{{ traefik_staging_dir }}" tags: [traefik, update] rescue: - name: Fail the staging ansible.builtin.fail: msg: >- Staging Traefik {{ traefik_version }} failed ({{ ansible_failed_result.msg | default('see the task above') }}). Traefik was not stopped and still runs {{ traefik_installed_version }}. tags: [traefik, update] # An include does not pass its tags on to the included tasks, hence apply. - name: Swap in the staged Traefik binary ansible.builtin.include_role: name: mortis.kamino._binary_update apply: tags: [traefik, update] vars: _binary_update_service: traefik _binary_update_binary_path: "{{ traefik_bin_path }}" _binary_update_staged_path: "{{ traefik_staging_dir }}/traefik" _binary_update_version_command: ["{{ traefik_bin_path }}", version] _binary_update_expected_version: "{{ traefik_version }}" tags: [traefik, update] always: - name: Remove the Traefik staging directory become: true ansible.builtin.file: path: "{{ traefik_staging_dir }}" state: absent tags: [traefik, update]