Files
ansible-playbooks/frontend.yaml
T
Matt SpencerandCopilot a409b985e8 Use vault secret for Cloudflare token and fail if empty
An unset env var led to an empty cloudflare.ini, breaking certbot renewal.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-12-02 12:13:53 +00:00

177 lines
6.0 KiB
YAML
Executable File

---
# - import_playbook: reverse-proxy.yaml
- name: Frontend setup
hosts: frontend
vars:
username: matt
certbot_dir: "{{ ansible_env.HOME }}/.local/share/certbot"
certbot_config_dir: "{{ ansible_env.HOME }}/.config/letsencrypt"
cloudflare_api_token: "{{ secrets.CLOUDFLARE_API_TOKEN | default(lookup('env', 'CLOUDFLARE_API_TOKEN')) }}"
certbot_admin_email: "{{ lookup('env', 'CERTBOT_ADMIN_EMAIL') }}"
roles:
- role: geerlingguy.git
become: true
tasks:
- name: Ensure SELinux configuration is set to enforcing
ansible.posix.selinux:
policy: targeted
state: enforcing
become: true
- name: Allow container runtimes to use NFS shares (virt_use_nfs)
ansible.posix.seboolean:
name: virt_use_nfs
state: true
persistent: true
become: true
- name: Ensure target directories exist
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: "0700"
loop:
- "{{ certbot_dir }}"
- "{{ certbot_config_dir }}"
- "{{ certbot_config_dir }}/work"
- "{{ certbot_config_dir }}/logs"
- name: Ensure Cloudflare API token is set
ansible.builtin.assert:
that: cloudflare_api_token | length > 0
fail_msg: CLOUDFLARE_API_TOKEN is empty; certbot renewal would fail. Pass -e @secrets.enc or set the env var.
- name: Deploy Cloudflare API credentials file
ansible.builtin.copy:
dest: "{{ certbot_config_dir }}/cloudflare.ini"
content: |
dns_cloudflare_api_token = {{ cloudflare_api_token }}
mode: "0600"
- name: Install Certbot and Cloudflare plugin via Pip inside virtualenv
ansible.builtin.pip:
name:
- certbot
- certbot-dns-cloudflare
virtualenv: "{{ certbot_dir }}"
virtualenv_command: "python3 -m venv" # Forces Ansible to use Python's built-in module
state: present
- name: Generate Certificate Bundle 1 (apptabulous.co.uk)
ansible.builtin.command:
cmd: >
{{ certbot_dir }}/bin/certbot certonly --non-interactive --agree-tos
--config-dir {{ certbot_config_dir }}
--work-dir {{ certbot_config_dir }}/work
--logs-dir {{ certbot_config_dir }}/logs
--email {{ certbot_admin_email }}
--dns-cloudflare
--dns-cloudflare-credentials {{ certbot_config_dir }}/cloudflare.ini
--dns-cloudflare-propagation-seconds 20
-d apptabulous.co.uk
-d www.apptabulous.co.uk
-d hub.apptabulous.co.uk
-d watchtower.apptabulous.co.uk
creates: "{{ certbot_config_dir }}/live/apptabulous.co.uk/fullchain.pem"
- name: Generate Certificate Bundle 2 (m5p3nc3r.co.uk)
ansible.builtin.command:
cmd: >
{{ certbot_dir }}/bin/certbot certonly --non-interactive --agree-tos
--config-dir {{ certbot_config_dir }}
--work-dir {{ certbot_config_dir }}/work
--logs-dir {{ certbot_config_dir }}/logs
--email {{ certbot_admin_email }}
--dns-cloudflare
--dns-cloudflare-credentials {{ certbot_config_dir }}/cloudflare.ini
--dns-cloudflare-propagation-seconds 60
-d m5p3nc3r.co.uk
-d www.m5p3nc3r.co.uk
-d gitea.m5p3nc3r.co.uk
creates: "{{ certbot_config_dir }}/live/m5p3nc3r.co.uk/fullchain.pem"
- name: Setup User-level Cron for automated renewal
ansible.builtin.cron:
name: "User-space Certbot Renewal"
minute: "30"
hour: "3"
job: >
{{ certbot_dir }}/bin/certbot renew --non-interactive
--config-dir {{ certbot_config_dir }}
--work-dir {{ certbot_config_dir }}/work
--logs-dir {{ certbot_config_dir }}/logs
--post-hook "podman unshare restorecon -R {{ certbot_config_dir }}/live {{ certbot_config_dir }}/archive && podman kill --signal HUP reverse_proxy"
- name: Copy reverse proxy configuration to host
become: true
ansible.builtin.copy:
src: apptabulous/reverseproxy
dest: /etc
- name: Create webservices network
containers.podman.podman_network:
name: webservices
state: present
- name: Ensure /etc/reverseproxy/certs directory exists
become: true
ansible.builtin.file:
path: /etc/reverseproxy/certs
state: directory
owner: "{{ username }}"
group: "{{ username }}"
mode: "0755"
- name: Start reverse proxy container
containers.podman.podman_container:
name: reverse_proxy
image: nginx:alpine
restart_policy: always
network: [webservices]
volumes:
- /etc/reverseproxy/nginx.conf:/etc/nginx/nginx.conf
- /etc/reverseproxy/conf.d:/etc/nginx/conf.d
- /etc/reverseproxy/stream.d:/etc/nginx/stream.d
- /home/matt/.config/letsencrypt:/etc/letsencrypt:ro,z
ports:
- "8080:80"
- "8443:443"
# No longer streaming ssh - so can remove this
# - "2222:2222"
state: started
- name: Log into ghcf.io registry
community.general.docker_login:
registry: ghcr.io
username: "{{ secrets.GITHUB_ACTOR }}"
password: "{{ secrets.GITHUB_TOKEN }}"
- name: Start m5p3nc3r website
containers.podman.podman_container:
name: m5p3nc3r
image: ghcr.io/m5p3nc3r/website:main
restart_policy: always
network: [webservices]
volumes:
- /home/matt/public/apps:/app/public/apps
ports:
- "3000:3000"
env:
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: "{{ secrets.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY }}"
state: started
- name: Start Apptabulous website
containers.podman.podman_container:
name: apptabulous
image: ghcr.io/m5p3nc3r/apptabulous_website:main
restart_policy: always
network: [webservices]
ports:
- "3001:3000"
state: started