Files
Kamino/roles/gitea/tasks/configure.yml
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

250 lines
7.9 KiB
YAML

---
# no_log: this file holds SECRET_KEY/INTERNAL_TOKEN/JWT_SECRET in cleartext -
# without it, a --diff run prints the old and new file bodies to the console
# (and CI logs).
- name: Create Gitea configuration file
become: true
ansible.builtin.template:
src: app.ini.j2
dest: "{{ gitea_app_dir }}/app.ini"
owner: root
group: "{{ gitea_group }}"
mode: "0640"
no_log: true
diff: false
notify: Reload Gitea
tags: [gitea, configure]
- name: Initialize Gitea database
become: true
ansible.builtin.command:
argv:
- sudo
- -u
- "{{ gitea_user }}"
- "{{ gitea_install_dir }}/gitea"
- migrate
- --config
- "{{ gitea_app_dir }}/app.ini"
args:
creates: "{{ gitea_data_dirs[0] }}/gitea.db"
tags: [gitea, configure]
- name: Check if admin user exists
become: true
ansible.builtin.command:
argv:
- sudo
- -u
- "{{ gitea_user }}"
- "{{ gitea_install_dir }}/gitea"
- admin
- user
- list
- --config
- "{{ gitea_app_dir }}/app.ini"
register: gitea_users
changed_when: false
tags: [gitea, configure]
# no_log hides the password from Ansible output, but `gitea admin user create`
# only accepts it via --password (no env var, stdin or file option as of
# v28.0.0, per cmd/admin_user_create.go). The password therefore sits in argv:
# visible in `ps` for the life of the command, and in sudo's argv too, which
# sudo logs in full (COMMAND=...) to syslog/auth.log/journald, so it persists
# in the host logs. Run it on a single-purpose host and restrict log access.
# argv, not a command string: the values are passed as single arguments, never split or
# re-parsed, so a space or quote in them cannot change the command.
- name: Create admin user
no_log: true
become: true
ansible.builtin.command:
argv:
- sudo
- -u
- "{{ gitea_user }}"
- "{{ gitea_install_dir }}/gitea"
- admin
- user
- create
- --username
- "{{ gitea_admin_user }}"
- --password
- "{{ gitea_admin_password }}"
- --email
- "{{ gitea_admin_email }}"
- --admin
- --must-change-password=false
- --config
- "{{ gitea_app_dir }}/app.ini"
when: "(gitea_admin_user | string) not in gitea_users.stdout"
register: gitea_create_admin
failed_when:
- gitea_create_admin.rc != 0
- '"successfully created" not in gitea_create_admin.stdout'
changed_when: "'successfully created' in gitea_create_admin.stdout"
tags: [gitea, configure]
- name: Setup bash autocompletion
ansible.builtin.command: >
{{ gitea_install_dir }}/gitea completion bash
register: gitea_completion_script
changed_when: false
# Read-only: the script is only generated to be written by the next task, so it must also run in --check.
check_mode: false
tags: [gitea, configure]
- name: Write bash completion to system directory
become: true
ansible.builtin.copy:
content: "{{ gitea_completion_script.stdout }}"
dest: "{{ gitea_bash_completion_path }}"
owner: root
group: root
mode: "0644"
tags: [gitea, configure]
- name: Create systemd service file for Gitea
become: true
ansible.builtin.copy:
dest: "{{ gitea_service_unit }}"
content: |
[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target
[Service]
User={{ gitea_user }}
Group={{ gitea_group }}
WorkingDirectory={{ gitea_data_dirs[0] }}
ExecStart={{ gitea_install_dir }}/gitea web \
--config {{ gitea_app_dir }}/app.ini
Restart=always
Environment=USER={{ gitea_user }} HOME={{ gitea_home }} GITEA_WORK_DIR={{ gitea_data_dirs[0] }}
[Install]
WantedBy=multi-user.target
owner: root
group: root
mode: "0644"
tags: [gitea, configure]
- name: Reload systemd daemon
become: true
ansible.builtin.systemd:
daemon_reload: true
tags: [gitea, configure]
- name: Trigger UFW after installing Gitea
ansible.builtin.include_role:
name: mortis.kamino.ufw
tasks_from: service_rules
apply:
tags: [gitea, ufw, configure]
vars:
ufw_service_name: gitea
ufw_service_rules: "{{ gitea_ufw_service_rules }}"
tags: [gitea, ufw, configure]
- name: Deploy Gitea Fail2Ban jail
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
tasks_from: service_jail
apply:
tags: [gitea, fail2ban, configure]
vars:
fail2ban_service_name: gitea
fail2ban_service_jail: "{{ gitea_fail2ban_jail }}"
fail2ban_service_filter: "{{ gitea_fail2ban_filter }}"
tags: [gitea, fail2ban, configure]
- name: Enable and start Gitea
become: true
ansible.builtin.systemd:
name: gitea
enabled: true
state: started
tags: [gitea, configure]
- name: Check Gitea service status
become: true
ansible.builtin.systemd:
name: gitea
register: gitea_service_status
tags: [gitea, configure, health]
- name: Fail if Gitea service is not running
ansible.builtin.fail:
msg: "Gitea service is not running"
when: gitea_service_status.status.ActiveState != "active"
tags: [gitea, configure, health]
# The token is minted by the opt-in mint entry point, never here. This sits after the
# service has started, so a first run on a fresh host installs and starts Gitea, then fails
# here; the operator mints the token (it needs the database and the admin user this run
# created) and runs again.
- name: Require gitea_api_token
ansible.builtin.assert:
that:
- gitea_api_token is defined
- gitea_api_token | default('', true) | string | length > 0
fail_msg: >-
gitea_api_token is not defined or empty. Mint it once with this role's mint entry point
(tasks_from: mint, -e gitea_mint_confirm=true, run by hand, never from CI), store the
gitea_minted_api_token it registers as a vaulted gitea_api_token in inventory, and run again.
quiet: true
tags: [gitea, configure]
# Local listener (app.ini HTTP_ADDR/HTTP_PORT), not gitea_domain: the public name may not
# resolve or have a certificate yet. A wildcard bind is reached over loopback. gitea_api_addr is
# the bare address (wait_for takes IPv6 without brackets); the URL brackets an IPv6 address.
- name: Resolve the local Gitea API address
vars:
gitea_api_bind: "{{ gitea_http_addr | trim | regex_replace('^\\[(.*)\\]$', '\\1') }}"
ansible.builtin.set_fact:
gitea_api_addr: >-
{{ '127.0.0.1' if gitea_api_bind in ['', '0.0.0.0'] else
'::1' if gitea_api_bind in ['::', '::0'] else gitea_api_bind }}
tags: [gitea, configure]
# The unit reports active before the listener accepts connections.
- name: Wait for the Gitea listener
ansible.builtin.wait_for:
host: "{{ gitea_api_addr }}"
port: "{{ gitea_http_port }}"
timeout: 60
when:
- not ansible_check_mode
- gitea_ssh_keys | length > 0
tags: [gitea, configure]
- name: Add SSH keys to Gitea admin user via API
# checkov:skip=CKV2_ANSIBLE_1:loopback-only request to a listener that cannot serve TLS
no_log: true
vars:
gitea_api_host: "{{ ('[' ~ gitea_api_addr ~ ']') if ':' in gitea_api_addr else gitea_api_addr }}"
ansible.builtin.uri:
url: "http://{{ gitea_api_host }}:{{ gitea_http_port }}/api/v1/admin/users/{{ gitea_admin_user }}/keys"
method: POST
headers:
Authorization: "token {{ gitea_api_token }}"
Content-Type: "application/json"
body_format: json
body:
key: "{{ gitea_ssh_key }}"
title: "{{ (gitea_ssh_key.split() + ['provisioned-key'])[2] | regex_replace('[^a-zA-Z0-9@._-]', '_') }}"
read_only: false
status_code:
- 201
- 422
register: gitea_keys_response
failed_when: >
gitea_keys_response.status not in [201, 422] or
(gitea_keys_response.status == 422 and
gitea_keys_response.json.message is not search("Key content has been used"))
changed_when: gitea_keys_response.status == 201
loop: "{{ gitea_ssh_keys }}"
loop_control:
loop_var: gitea_ssh_key
tags: [gitea, configure]