--- - name: Verify the converged daemon hosts: all gather_facts: false tasks: - name: Read config.yml ansible.builtin.slurp: src: /etc/cloudflared/config.yml register: cloudflared_test_config_raw - name: Parse config.yml ansible.builtin.set_fact: cloudflared_test_config: "{{ cloudflared_test_config_raw.content | b64decode | from_yaml }}" - name: Config.yml routes the ingress hostnames and ends with the catch-all ansible.builtin.assert: that: - cloudflared_test_config.tunnel == cloudflared_test_tunnel_id - cloudflared_test_config['credentials-file'] == '/etc/cloudflared/' ~ cloudflared_test_tunnel_id ~ '.json' - cloudflared_test_config.ingress | length == 3 - cloudflared_test_config.ingress[0].hostname == 'app.example.test' - cloudflared_test_config.ingress[0].service == 'https://127.0.0.1:443' - "cloudflared_test_config.ingress[0].originRequest == {'originServerName': 'app.example.test'}" - cloudflared_test_config.ingress[1].hostname == 'other.example.test' - cloudflared_test_config.ingress[1].originRequest is not defined - "cloudflared_test_config.ingress[2] == {'service': 'http_status:404'}" quiet: true - name: Read the credentials file ansible.builtin.slurp: src: "/etc/cloudflared/{{ cloudflared_test_tunnel_id }}.json" register: cloudflared_test_credentials no_log: true - name: Stat the configuration, the seeded files and what an update leaves behind ansible.builtin.stat: path: "{{ cloudflared_test_path }}" loop: - /etc/cloudflared/config.yml - "/etc/cloudflared/{{ cloudflared_test_tunnel_id }}.json" - /etc/cloudflared/6f1a6e2c-0000-4000-8000-000000000000.json - /etc/cloudflared/notes.json - /usr/local/bin/cloudflared.new - /usr/local/bin/cloudflared.bak loop_control: loop_var: cloudflared_test_path register: cloudflared_test_files - name: The files are private to the daemon, the stale credentials are gone and the update left nothing behind ansible.builtin.assert: that: - cloudflared_test_files.results[0].stat.mode == '0640' - cloudflared_test_files.results[0].stat.pw_name == 'cloudflared' - cloudflared_test_files.results[1].stat.mode == '0600' - cloudflared_test_files.results[1].stat.pw_name == 'cloudflared' - (cloudflared_test_credentials.content | b64decode | from_json) == (cloudflared_test_credentials_json | from_json) - not cloudflared_test_files.results[2].stat.exists - cloudflared_test_files.results[3].stat.exists - not cloudflared_test_files.results[4].stat.exists - not cloudflared_test_files.results[5].stat.exists quiet: true - name: Ask the installed binary for its version ansible.builtin.command: argv: [/usr/local/bin/cloudflared, --version] register: cloudflared_test_version changed_when: false - name: Ask systemd about the daemon ansible.builtin.systemd: name: cloudflared register: cloudflared_test_unit # The stub's `run` only keeps running when it can read the config it was given, so an active # unit shows the service user reads config.yml. - name: Stat the daemon's process ansible.builtin.stat: path: "/proc/{{ cloudflared_test_unit.status.MainPID }}" register: cloudflared_test_process - name: The updated daemon runs as the service user with the rendered config ansible.builtin.assert: that: - "'2026.2.0' in cloudflared_test_version.stdout" - cloudflared_test_unit.status.ActiveState == 'active' - cloudflared_test_unit.status.UnitFileState == 'enabled' - cloudflared_test_unit.status.ExecStart is search('--config /etc/cloudflared/config.yml run') - cloudflared_test_process.stat.exists - cloudflared_test_process.stat.pw_name == 'cloudflared' quiet: true # cloudflared_manage_dns is false in the converge: the tunnel was checked, no DNS call made. - name: Read the requests the API stub received delegate_to: localhost ansible.builtin.slurp: src: "{{ cloudflared_test_stub_dir }}/requests.log" register: cloudflared_test_requests - name: The converge checked the tunnel and made no DNS call vars: cloudflared_test_log: "{{ cloudflared_test_requests.content | b64decode }}" ansible.builtin.assert: that: - >- cloudflared_test_log is search('"GET", "path": "/client/v4/accounts/molecule-account/cfd_tunnel/' ~ cloudflared_test_tunnel_id ~ '"') - cloudflared_test_log is not search('/zones') quiet: true # Every case fails, before any change or at a guard ahead of the first change; the last play # checks that the daemon was left as the converge set it up. - name: Verify the failure paths hosts: all gather_facts: false tasks: - name: Checksum config.yml and the binary before the failure cases ansible.builtin.stat: path: "{{ cloudflared_test_path }}" checksum_algorithm: sha256 loop: [/etc/cloudflared/config.yml, /usr/local/bin/cloudflared] loop_control: loop_var: cloudflared_test_path register: cloudflared_test_before - name: Ask systemd about the daemon before the failure cases ansible.builtin.systemd: name: cloudflared register: cloudflared_test_unit_before - name: Run each case ansible.builtin.include_tasks: tasks/failed_run.yml loop: - name: version-with-v vars: {cloudflared_version: v2026.2.0} expect: must be a release tag - name: sha256-not-hex vars: {cloudflared_sha256_amd64: not-a-hash} expect: must be the 64-digit hex sha256 - name: ingress-without-service vars: {cloudflared_ingress: [{hostname: app.example.test}]} expect: each with hostname and service - name: tunnel-not-minted vars: {cloudflared_tunnel_id: ""} expect: run the mint entry point first - name: credentials-not-minted vars: {cloudflared_credentials_json: ""} expect: run the mint entry point first - name: account-missing vars: {cloudflared_account_id: ""} expect: cloudflared_account_id and cloudflared_tunnel_api_token (vaulted) have no role default - name: zone-missing vars: {cloudflared_manage_dns: true} expect: cloudflared_manage_dns is true, so cloudflared_zone_name - name: tunnel-deleted vars: {cloudflared_tunnel_id: 6f1a6e2c-0000-4000-8000-000000000002} expect: no longer exists on Cloudflare # Retried three times, 5 s apart, before the 404 is trusted. - name: tunnel-unknown vars: {cloudflared_tunnel_id: 6f1a6e2c-0000-4000-8000-00000000000f} expect: (HTTP 404) - name: zone-unknown vars: {cloudflared_manage_dns: true, cloudflared_zone_name: missing.example.test} expect: No Cloudflare zone found named missing.example.test - name: record-owned-by-something-else vars: cloudflared_manage_dns: true cloudflared_zone_name: example.test cloudflared_ingress: [{hostname: taken.example.test, service: "http://127.0.0.1:8080"}] expect: do not point at a Cloudflare Tunnel # The configured hash is the installed binary's, so the version was bumped without it. - name: stale-sha256 vars: cloudflared_version: 2026.3.0 cloudflared_sha256_amd64: "{{ cloudflared_test_stub | replace('@VERSION@', '2026.2.0') | hash('sha256') }}" expect: The hash is stale - name: checksum-mismatch vars: {cloudflared_version: 2026.3.0, cloudflared_sha256_amd64: "{{ 'f' * 64 }}"} expect: cloudflared was not stopped and still runs 2026.2.0 loop_control: loop_var: cloudflared_test_case label: "{{ cloudflared_test_case.name }}" - name: Checksum config.yml and the binary after the failure cases ansible.builtin.stat: path: "{{ cloudflared_test_path }}" checksum_algorithm: sha256 loop: [/etc/cloudflared/config.yml, /usr/local/bin/cloudflared] loop_control: loop_var: cloudflared_test_path register: cloudflared_test_after - name: Stat the staged binary ansible.builtin.stat: path: /usr/local/bin/cloudflared.new register: cloudflared_test_staged - name: Ask systemd about the daemon after the failure cases ansible.builtin.systemd: name: cloudflared register: cloudflared_test_unit_after - name: The failures changed nothing and the daemon still runs ansible.builtin.assert: that: - cloudflared_test_after.results | map(attribute='stat.checksum') | list == cloudflared_test_before.results | map(attribute='stat.checksum') | list - not cloudflared_test_staged.stat.exists - cloudflared_test_unit_after.status.ActiveState == 'active' # not restarted, not even by the failed update - cloudflared_test_unit_after.status.MainPID == cloudflared_test_unit_before.status.MainPID quiet: true # The mint entry point. The molecule job runs with CI variables set, so every mint that should # get past the CI guard runs as a child process with them blanked (mint.yml). - name: Verify the mint entry point hosts: all gather_facts: false vars: cloudflared_test_mint_inputs: cloudflared_api_base_url: "{{ cloudflared_api_base_url }}" cloudflared_account_id: molecule-account cloudflared_tunnel_api_token: molecule-not-a-token cloudflared_tunnel_name: molecule-tunnel cloudflared_test_no_ci: {GITHUB_ACTIONS: "", GITEA_ACTIONS: "", CI: "", ANSIBLE_NOCOLOR: "1"} tasks: - name: Mint without confirmation block: - name: Include the mint entry point with cloudflared_mint_confirm unset ansible.builtin.include_role: name: mortis.kamino.cloudflared tasks_from: mint # Only reached when the include returned, so the guard did not fire. The message # must not contain the guard's text, so the rescue below cannot mistake it for it. - name: Fail because the include returned ansible.builtin.fail: msg: The include returned without the confirm guard firing rescue: - name: The confirm guard fired, and nothing else failed first ansible.builtin.assert: that: - "'mint is opt-in' in ansible_failed_result.msg" - "'cloudflared_mint_confirm=true' in ansible_failed_result.msg" quiet: true - name: Mint under each CI variable, run as a child process with only that one set delegate_to: localhost run_once: true ansible.builtin.command: argv: - ansible-playbook - -i - localhost, - "{{ playbook_dir }}/mint.yml" - -e - "{{ cloudflared_test_mint_inputs | to_json }}" environment: "{{ cloudflared_test_no_ci | combine({item: 'true'}) }}" loop: [GITHUB_ACTIONS, GITEA_ACTIONS, CI] register: cloudflared_test_mint_ci # The child is expected to fail (the CI guard fires). Its rc and output are asserted next. failed_when: false changed_when: false # Ansible 2.19 prints task failures to stderr, so both streams are checked. - name: The CI guard fired for every variable ansible.builtin.assert: that: - item.rc != 0 - "'refuses to run under CI' in (item.stdout ~ item.stderr)" - "'mint is opt-in' not in (item.stdout ~ item.stderr)" quiet: true loop: "{{ cloudflared_test_mint_ci.results }}" loop_control: label: "{{ item.item }}" - name: Run the mint cases outside CI, each as a child process delegate_to: localhost run_once: true ansible.builtin.command: argv: >- {{ ['ansible-playbook', '-i', 'localhost,', playbook_dir ~ '/mint.yml', '-e', cloudflared_test_mint_inputs | combine(item.vars) | to_json] + item.args | default([]) }} environment: "{{ cloudflared_test_no_ci }}" loop: # A tunnel that is already set is never replaced. - {name: already-set, vars: {cloudflared_tunnel_id: "{{ cloudflared_test_tunnel_id }}"}} - {name: account-missing, vars: {cloudflared_account_id: ""}} # An empty id counts as missing; in check mode the inputs are asserted and nothing is created. - {name: check-mode, vars: {cloudflared_tunnel_id: ""}, args: [--check]} - {name: minted, vars: {cloudflared_test_expect_minted: true}} loop_control: label: "{{ item.name }}" register: cloudflared_test_mint_cases # Some children are expected to fail. Their rc and output are asserted next. failed_when: false changed_when: false - name: Read the requests the API stub received delegate_to: localhost ansible.builtin.slurp: src: "{{ cloudflared_test_stub_dir }}/requests.log" register: cloudflared_test_requests - name: Each mint case ended as expected, and only the last one created a tunnel vars: cloudflared_test_out: >- {{ cloudflared_test_mint_cases.results | map(attribute='stdout') | zip(cloudflared_test_mint_cases.results | map(attribute='stderr')) | map('join', ' ') | list }} cloudflared_test_rc: "{{ cloudflared_test_mint_cases.results | map(attribute='rc') | list }}" cloudflared_test_posts: >- {{ cloudflared_test_requests.content | b64decode | split('\n') | select('search', '"POST", "path": "/client/v4/accounts/molecule-account/cfd_tunnel"') | list }} ansible.builtin.assert: that: - cloudflared_test_rc[0] == 0 - "'nothing to mint' in cloudflared_test_out[0]" - cloudflared_test_rc[1] != 0 - "'must be set and non-empty to mint' in cloudflared_test_out[1]" - cloudflared_test_rc[2] == 0 - "'nothing to mint' not in cloudflared_test_out[2]" - cloudflared_test_rc[3] == 0 - cloudflared_test_posts | length == 1 - cloudflared_test_posts[0] is search('"status": 200') fail_msg: "Return codes {{ cloudflared_test_rc }}, outputs: {{ cloudflared_test_out }}" quiet: true # The remove path. Last on purpose: it runs once, outside the idempotence step, and nothing runs # after verify except molecule's cleanup. cloudflared_manage_dns stays false, so the removal # deletes the tunnel through the API stub and makes no DNS call. - name: Verify the remove path hosts: all gather_facts: false tasks: - name: Run the role with the remove flags ansible.builtin.include_role: name: mortis.kamino.cloudflared vars: cloudflared_remove: true cloudflared_remove_data: true - name: Stat what the removal deletes ansible.builtin.stat: path: "{{ cloudflared_test_path }}" loop: - /etc/systemd/system/cloudflared.service - /usr/local/bin/cloudflared - /etc/cloudflared - /etc/systemd/system/multi-user.target.wants/cloudflared.service loop_control: loop_var: cloudflared_test_path register: cloudflared_test_removed - name: Look up the cloudflared user ansible.builtin.getent: database: passwd key: cloudflared fail_key: false register: cloudflared_test_user - name: Ask systemd whether the service is still active ansible.builtin.systemd: name: cloudflared register: cloudflared_test_active failed_when: false - name: Read the requests the API stub received delegate_to: localhost ansible.builtin.slurp: src: "{{ cloudflared_test_stub_dir }}/requests.log" register: cloudflared_test_requests - name: Everything is gone, nothing was recreated, and the tunnel was deleted on Cloudflare ansible.builtin.assert: that: - cloudflared_test_removed.results | map(attribute='stat') | map(attribute='exists') | select | list | length == 0 # prereqs, which create the account, do not run after a removal - cloudflared_test_user.ansible_facts.getent_passwd.cloudflared is none # the module errors for a unit that no longer exists, which is also a pass - (cloudflared_test_active.status | default({})).ActiveState | default('inactive') != 'active' - >- cloudflared_test_requests.content | b64decode is search('"DELETE", "path": "/client/v4/accounts/molecule-account/cfd_tunnel/' ~ cloudflared_test_tunnel_id ~ '", "status": 200') quiet: true