Files
Kamino/roles/gitea/README.md
T
Defame1297andClaude Opus 5.5 f060461152
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Failing after 43s
Molecule / molecule (pull_request) Successful in 1m20s
feat(gitea): add the gitea role
Moves the gitea role from the environment repository, the second wave 2 role and the
second caller of the internal _binary_update role.

Against the source:
- The update path stages the downloaded, signature-verified binary in a directory next to
  the live one and hands the stop, backup, swap, restart, version check and rollback to
  mortis.kamino._binary_update (include_role with apply tags, so --tags update reaches
  it). A download or signature failure still leaves the running service untouched and
  says so; a failure in the swap keeps the rollback message of _binary_update. Its default
  version regex is the one the source used.
- The same two small behaviour changes as for traefik come with _binary_update: the backup
  is taken whenever a live binary exists, and a failure before the stop no longer
  restarts the running service.
- The update no longer removes a PID file between the stop and the start, and
  gitea_pid_file is gone: the unit runs `gitea web` without --pid and has no PIDFile=,
  so Gitea writes no PID file and systemd reads none.
- The database-migration warning of the source's rollback message (the previous binary
  may refuse a migrated schema) moves to the gitea_update docs in the README and the
  argument_specs, since _binary_update owns the failure message.
- The legacy name guard (pre-rename variables of the environment) is dropped, as in the
  wave 1 roles, and the argument_specs and README no longer mention it. The jwt secret,
  mailer and e-mail confirmation asserts stay.
- Environment references are gone from comments, app.ini.j2, the README and the
  argument_specs: issue numbers, decision records, host names, inventory paths,
  playbooks, the network and proxy layout and other roles' copies of the paths. The
  Renovate comment on gitea_version is dropped; the README and argument_specs recommend
  pinning the version in inventory. The mint messages no longer name a playbook.
- On a fresh host the token assert and the SSH-key upload ran before the unit was
  written and Gitea started, so a first install could never complete. Both now run after
  the service check, and the upload first waits (up to 60 s, not under --check) for the
  local listener; the listener address is resolved once for both.
- REVERSE_PROXY_LIMIT, hard-coded to 2, is now gitea_reverse_proxy_limit with default 1:
  a limit above the real number of proxies lets a client forge the address Gitea logs and
  Fail2Ban bans. A setup with two proxies sets it to 2.
- The gitea commands with user-supplied values (migrate, admin user list and create, and
  the token mint) take argv instead of a command string; still run through sudo -u.
- gitea_version is asserted at role start to be a plain X.Y.Z, with no leading v.
- meta namespace mortis; the scenario follows the Kamino conventions (header,
  test_sequence, no provisioner env) and calls the role by FQCN, also in the child
  ansible-playbook of the mint CI-guard cases. Coverage is unchanged.

No other default changed; DISABLE_REGISTRATION stays hard-coded to true.

Changelog fragment, README Roadmap and the CLAUDE.md role list updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-11-21 21:53:20 +00:00

14 KiB

gitea

Installs the Gitea git service as a systemd-managed binary with a GPG-verified download, renders app.ini (SQLite, HTTP on gitea_http_addr:gitea_http_port, default 127.0.0.1:3000), creates the admin user and loads SSH keys through the API, opens the port through the mortis.kamino.ufw role, installs a Fail2Ban jail through the mortis.kamino.fail2ban role, and optionally updates or removes the service (removal also deletes its ufw rules). Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.

Supported platforms

Ubuntu 24.04 (noble), the image the molecule scenario runs on. The role uses ansible.builtin.apt, so Debian-family hosts only, and needs outbound access to dl.gitea.com for the binary and signature and to gitea_gpg_key_url (keys.openpgp.org over HTTPS by default) for the signing key. A molecule scenario covers the guard and no-op paths of the mint entry point, the remove path and the signature check of the download (nothing is installed; see molecule/default).

Requirements and run order

  • Use it by its fully qualified name, mortis.kamino.gitea.
  • Run the system_baseline and ufw roles of this Collection first. The configure tasks pass gitea_ufw_service_rules (allow from 127.0.0.1 and ::1, then deny, on gitea_http_port) to the service_rules entry point of mortis.kamino.ufw, owner gitea, which needs ufw installed and active and the SSH rule in place. See the README of mortis.kamino.ufw.
  • Coupling with mortis.kamino.fail2ban: the same file runs its service_jail entry point, passing fail2ban_service_name: gitea, fail2ban_service_jail: "{{ gitea_fail2ban_jail }}" and fail2ban_service_filter: "{{ gitea_fail2ban_filter }}". The jail reads journald (journalmatch), because Gitea logs to the console. See the README of mortis.kamino.fail2ban.
  • Secrets have no role defaults: gitea_admin_email, gitea_admin_password, gitea_secret_key, gitea_internal_token, gitea_jwt_secret and gitea_api_token. Map them from vaulted variables in inventory; the role never holds the values itself.
  • The argument spec marks gitea_domain, gitea_admin_user, gitea_admin_email, gitea_admin_password, gitea_secret_key, gitea_internal_token and gitea_ssh_keys required, so a missing one fails the role at its start, before the remove path: gitea_remove=true needs them too. gitea_jwt_secret is asserted at role start (defined and non-empty) by the tasks, also before the remove path, so --tags remove needs it as well.
  • gitea_version is asserted at role start to be a plain X.Y.Z release number, with no leading v.
  • gitea_domain, gitea_admin_user and gitea_ssh_keys have no role default and must be set in inventory.
  • Token: tasks/configure.yml asserts gitea_api_token after the service has started and before the SSH-key API call, and never mints it. See Minting secrets.
  • A reverse proxy in front terminates TLS: app.ini serves plain HTTP on the listener and renders ROOT_URL as https://<gitea_domain>/.
  • Updates: the update path stages the verified binary and hands the swap to mortis.kamino._binary_update, an internal role of this Collection.
  • Pin gitea_version in inventory to move Gitea independently of Collection releases; the role default changes only with a release.

Role variables

Inputs with no role default (all required, supplied by inventory):

Variable Description
gitea_domain Public host name, rendered into DOMAIN and ROOT_URL.
gitea_admin_user Admin username: the account created by the admin CLI, which receives the SSH keys and the access token.
gitea_admin_email Admin e-mail (vaulted, no_log).
gitea_admin_password Admin password, only evaluated when the admin user does not exist (vaulted, no_log).
gitea_secret_key SECRET_KEY in app.ini (vaulted, no_log).
gitea_internal_token INTERNAL_TOKEN in app.ini (vaulted, no_log).
gitea_jwt_secret JWT_SECRET in app.ini (vaulted, no_log; required, asserted at role start).
gitea_ssh_keys Public keys added to the admin user.

Inputs with no role default that the normal flow asserts:

Variable Description
gitea_api_token Admin access token for the key API calls, asserted defined and non-empty (vaulted, no_log). Created by the mint entry point.

Inputs with role defaults:

Variable Default Description
gitea_update false Run the update tasks when the installed version differs from gitea_version: download and verify the signature into a staging directory before anything is stopped, then mortis.kamino._binary_update stops, backs up, swaps, restarts and checks the version, and rolls back the binary on failure. The database is not rolled back, see Notes.
gitea_remove false Stop and disable the service, remove unit and binary, then delete the role's ufw rules (owner gitea, ufw_service_state: absent). The last remove task deletes the role's ufw rules, so ufw must be active on the host or that task fails. Prerequisites, install, configure and update are skipped, so nothing reinstalls the service or recreates gitea_user.
gitea_remove_data false With gitea_remove, also delete gitea_app_dir, gitea_data_dirs and the gitea_user account.
gitea_version pinned in defaults/main.yml Release to install, X.Y.Z with no leading v (asserted); pin it in inventory.
gitea_download_url dl.gitea.com, linux amd64 (templated) Binary URL.
gitea_signature_url gitea_download_url plus .asc Signature URL.
gitea_gpg_key_id Gitea release key fingerprint, see defaults Primary key whose signature (or a subkey's) is required. The check runs in a throwaway keyring and rejects a signature by any other key, a bad signature and one by an expired or revoked key.
gitea_gpg_key_url keys.openpgp.org lookup of gitea_gpg_key_id Where the release key is fetched from on every download.
gitea_user / gitea_group git / git Service account.
gitea_home /home/git Home of gitea_user.
gitea_install_dir /usr/local/bin Binary directory.
gitea_app_dir /etc/gitea Directory of app.ini.
gitea_data_dirs /var/lib/gitea and custom, data, log below it Order matters: item 0 is the work path and database directory, item 2 the repository parent.
gitea_service_unit /etc/systemd/system/gitea.service Unit path.
gitea_fail2ban_jail journald jail on http and https, 5 retries, 1h ban, 10m window Jail mapping for the fail2ban role; overriding replaces the whole mapping.
gitea_fail2ban_filter failregex for failed authentication attempts Filter mapping for the fail2ban role; overriding replaces the whole mapping.
gitea_http_port 3000 Listener port: HTTP_PORT in app.ini, the admin key API URL and the default gitea_ufw_service_rules.
gitea_http_addr 127.0.0.1 Listener address: HTTP_ADDR, the host of the admin key API URL (wildcard addresses 0.0.0.0, ::, ::0 and empty become loopback; IPv6 is bracketed, existing brackets are accepted). The default ufw rules do not follow it: they stay pinned to 127.0.0.1 and ::1, so a non-loopback bind needs your own gitea_ufw_service_rules.
gitea_reverse_proxy_limit 1 REVERSE_PROXY_LIMIT: the number of trusted (loopback) proxy hops in X-Forwarded-For. Set it to the real number of proxies in front of Gitea; a higher value lets a client forge the address Gitea logs and Fail2Ban bans.
gitea_bash_completion_path /usr/share/bash-completion/completions/gitea Where the generated bash completion is written.
gitea_ufw_service_rules allow gitea_http_port from 127.0.0.1 and ::1, deny otherwise (in that order) Rules passed to ufw service_rules (owner gitea).
gitea_mailer_enabled false Render a [mailer] section and set ENABLE_NOTIFY_MAIL = true. The settings below are asserted at role start when true; when false no [mailer] section is rendered.
gitea_mailer_protocol smtp+starttls PROTOCOL: smtp, smtps or smtp+starttls.
gitea_mailer_smtp_addr / gitea_mailer_smtp_port "" / 587 SMTP server.
gitea_mailer_from "" FROM, sender with optional display name.
gitea_mailer_user / gitea_mailer_passwd "" / "" SMTP login (vaulted, no_log). PASSWD is backtick-quoted, so it must not contain a backtick.
gitea_mailer_subject_prefix "" SUBJECT_PREFIX, omitted when empty.
gitea_default_email_notifications enabled DEFAULT_EMAIL_NOTIFICATIONS in [admin]: the notification mail setting new users start with (enabled, onmention, disabled). Rendered only with the mailer on.
gitea_register_email_confirm false REGISTER_EMAIL_CONFIRM: new accounts and added e-mail addresses must be confirmed by mail. Requires gitea_mailer_enabled (asserted).

Minting secrets

The normal flow never creates a credential. tasks/configure.yml asserts gitea_api_token right before the first task that uses it, after the service has started. On a fresh host the first run installs Gitea, creates the admin user and starts the service, then fails at that assert with what to do; the operator mints the token with the entry point below, stores it in inventory and runs the role again. gitea_jwt_secret is likewise assert-only.

The token is created by the opt-in mint entry point (tasks_from: mint), run by hand from an operator play, never from CI:

- name: Mint the Gitea admin API token
  hosts: gitea
  tasks:
    - name: Mint
      ansible.builtin.include_role:
        name: mortis.kamino.gitea
        tasks_from: mint
    # Then vault-encrypt gitea_minted_api_token (when defined) into inventory as gitea_api_token.

Run it with -e gitea_mint_confirm=true.

  • Guard variable: gitea_mint_confirm must be true (default false). The entry point also fails when GITHUB_ACTIONS, GITEA_ACTIONS or CI is set in the controller environment. This guards against accidents; it is not a security boundary.
  • If gitea_api_token is already defined and non-empty (undefined, null and "" count as missing) the entry point only reports that and creates nothing, so a re-run is safe.
  • Otherwise it runs gitea admin user generate-access-token (name ansible-provisioning, scope all) and registers the token as the no_log fact gitea_minted_api_token. The fact is not cacheable, never printed and only defined when a token was created.
  • The role writes no vault file. The calling play encrypts the fact and stores it in inventory.
  • Gitea shows the token once. If the play fails after the token was created and before it was persisted, the token is lost: delete ansible-provisioning for the admin user in the Gitea UI (a second token with that name cannot be told apart) and mint again.

Tags

gitea (everything), mint (carried by the mint entry point's tasks, but not selectable on its own: its guards are tagged gitea, always, so select the mint play with --tags gitea), remove, prereqs, install, configure, health, update, ufw, fail2ban. prereqs tasks and the input asserts are also tagged always. ufw and fail2ban only take effect together with configure, health or gitea: their include_role tasks live in tasks/configure.yml, which main.yml includes only under those tags, so on their own they select nothing. The ufw rule removal in tasks/remove.yml also carries ufw and remove, but main.yml includes remove.yml only under gitea and remove.

Example play

- name: Install Gitea
  hosts: gitea
  roles:
    - role: mortis.kamino.gitea
      vars:
        gitea_domain: git.example.com
        gitea_admin_user: admin
        gitea_admin_email: "{{ vault_gitea_admin_email }}"
        gitea_admin_password: "{{ vault_gitea_admin_password }}"
        gitea_secret_key: "{{ vault_gitea_secret_key }}"
        gitea_internal_token: "{{ vault_gitea_internal_token }}"
        gitea_jwt_secret: "{{ vault_gitea_jwt_secret }}"
        gitea_api_token: "{{ vault_gitea_api_token }}"
        gitea_ssh_keys:
          - "ssh-ed25519 AAAA... user@host"

Update or remove: pass -e gitea_update=true, or -e "gitea_remove=true gitea_remove_data=true" --tags remove.

Notes

  • The role is not a state selector. main.yml runs prerequisites and configure unless gitea_remove is true; install runs only when the binary is absent; update only when gitea_update is true; remove only when gitea_remove is true, and then every other task in main.yml is skipped.
  • Updates roll back the binary, not the database. Gitea migrates its database when the new version starts, so after a failure past that point the previous binary may refuse the newer schema and the service may not come back up (the failure message of _binary_update then says it did not). Back up the database before an update across releases; to recover, restore it or move forward to gitea_version.
  • app.ini is written with no_log: true and diff: false because it holds SECRET_KEY, INTERNAL_TOKEN, JWT_SECRET and, with the mailer on, the SMTP password. The admin-create, token-mint and key-upload tasks are no_log too.
  • Gitea accepts the admin password only through --password, so it is in the command's argv: visible in ps for the life of the create command, and logged in full by sudo (COMMAND=...) to syslog, auth.log or journald, where it persists in the host logs. This happens only on the first run, when the user is missing. Run the role on a single-purpose host and restrict access to its logs.
  • app.ini renders the listener from gitea_http_addr and gitea_http_port; it hard-codes SQLite, DISABLE_REGISTRATION = true (accounts are created by an admin only), START_SSH_SERVER = false and loopback as the only trusted proxy. None of these is a variable; REVERSE_PROXY_LIMIT comes from gitea_reverse_proxy_limit. A reverse proxy terminates TLS in front of it.
  • The SSH-key API calls go to gitea_http_addr:gitea_http_port (wildcard becomes loopback, IPv6 is bracketed), not to gitea_domain.
  • Handlers: Reload Gitea restarts the service when app.ini changes, and main.yml flushes handlers at the end so a later failure in the same play does not drop the restart. The Reload Systemd handler is defined but no task in the role notifies it.
  • The play fails when the service is not active after start.