Files
Kamino/roles/gitea/tasks/configure.yml
T
Defame1297 cf2286b7a7 test(roles): run molecule's check step before and after converge
Every role scenario with a converge now runs check right before it (a
fresh host) and right after it (a configured host), and a script test
fails a scenario without both. pihole and pve_lxc move their second
check from after idempotence to after converge to match.

The roles that failed under --check on a fresh host get the two allowed
guards: cloudflared, docker, gitea, gitea_runner, traefik and
user_management. ufw and fail2ban preview only a host where their
package is installed, as their READMEs now say.

Closes #134

Assisted-by: Claude Code
2026-11-26 10:11:54 +00:00

259 lines
8.3 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
# Under --check on a fresh host there is no binary to run.
when: not ansible_check_mode or gitea_installed
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"
when: not ansible_check_mode or gitea_installed
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"
register: __gitea_unit_file
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
# Under --check a new unit is only simulated, so systemd would not find it.
when: not ansible_check_mode or __gitea_unit_file is not changed
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"
# The start above is skipped under --check for a new unit.
- not ansible_check_mode or __gitea_unit_file | default({}) is not changed
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]