--- # Opt-in entry point (tasks_from: mint): mints the admin API token and registers it as # gitea_minted_api_token. Persisting it is the calling play's job (for example, vault-encrypt # it into inventory); this role writes no vault files. - name: Guard the Gitea mint entry point ansible.builtin.assert: that: - gitea_mint_confirm | default(false) | bool fail_msg: "gitea mint is opt-in: pass -e gitea_mint_confirm=true" quiet: true tags: [gitea, always] - name: Refuse to mint the Gitea token under CI ansible.builtin.assert: that: - lookup('ansible.builtin.env', 'GITHUB_ACTIONS') | length == 0 - lookup('ansible.builtin.env', 'GITEA_ACTIONS') | length == 0 - lookup('ansible.builtin.env', 'CI') | length == 0 fail_msg: "gitea mint refuses to run under CI: run the mint play by hand" quiet: true tags: [gitea, always] # Defined and empty (an `gitea_api_token: ""` or null in the inventory) counts as missing: # configure.yml asserts a non-empty token, so skipping the mint on an empty one would deadlock. - name: Skip minting, Gitea admin API token is already set ansible.builtin.debug: msg: "gitea_api_token is already set to a non-empty value in inventory, nothing to mint." when: (gitea_api_token | default('', true) | string | length) > 0 tags: [gitea, mint] # The token is shown once: it is only registered as a no_log fact for the # operator playbook to persist, never printed. - name: Create Gitea admin API token when: (gitea_api_token | default('', true) | string | length) == 0 and not ansible_check_mode become: true tags: [gitea, mint] block: - name: Generate Gitea admin API token no_log: true ansible.builtin.command: argv: - sudo - -u - "{{ gitea_user }}" - "{{ gitea_install_dir }}/gitea" - admin - user - generate-access-token - --username - "{{ gitea_admin_user }}" - --token-name - ansible-provisioning - --scopes - all - --config - "{{ gitea_app_dir }}/app.ini" register: gitea_new_token failed_when: - gitea_new_token.rc != 0 - gitea_new_token.stdout is not search('successfully created') changed_when: "'successfully created' in gitea_new_token.stdout" - name: Extract token from CLI output no_log: true ansible.builtin.set_fact: gitea_raw_token: "{{ gitea_new_token.stdout | regex_search('Access token was successfully created: (\\S+)', '\\1') | first }}" - name: Fail if token extraction failed ansible.builtin.fail: msg: "Failed to extract Gitea API token" when: gitea_raw_token is not defined or gitea_raw_token | length == 0 - name: Register the minted Gitea API token no_log: true ansible.builtin.set_fact: gitea_minted_api_token: "{{ gitea_raw_token }}" # The registered command result and the extracted token would otherwise stay in # the host's vars for the rest of the play; only gitea_minted_api_token is kept. - name: Clear the token intermediates no_log: true ansible.builtin.set_fact: gitea_new_token: "" gitea_raw_token: ""