From b686c6ae9ff18ca1ce8b8e602dc03548cbfa7b31 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 17 Oct 2026 21:23:25 -0700 Subject: Add better encapsulation to ansible roles - Replace a few of the global variables in the host inventory to explicit variables that we pass into the particular roles that need it. - Move slowcgi setup to cgit since that is where it is needed (instead of obsd_httpd) - Rename `acme_client` to `obsd_ssl` to better emphasize it is related to generating ssl certificates on openbsd. - Add support for multiple ssl certificates into `obsd_ssl` by looping over the new role variable it has --- inventory/git.yml | 4 -- roles/acme_client/defaults/main.yml | 1 - roles/acme_client/tasks/main.yml | 45 --------------------- roles/acme_client/templates/acme-client.conf.j2 | 15 ------- roles/acme_client/templates/httpd.d/acme.conf | 11 ------ roles/obsd_cgit/handlers/main.yml | 4 -- roles/obsd_cgit/tasks/main.yml | 7 +++- roles/obsd_git/tasks/main.yml | 2 +- roles/obsd_httpd/defaults/main.yml | 1 + roles/obsd_httpd/handlers/main.yml | 4 ++ roles/obsd_httpd/tasks/main.yml | 10 ++--- roles/obsd_httpd/templates/httpd.conf.j2 | 2 +- roles/obsd_ssl/tasks/main.yml | 52 +++++++++++++++++++++++++ roles/obsd_ssl/templates/acme-client.conf.j2 | 17 ++++++++ roles/obsd_ssl/templates/httpd.d/acme.conf | 12 ++++++ site.yml | 16 ++++++-- 16 files changed, 109 insertions(+), 94 deletions(-) delete mode 100644 roles/acme_client/defaults/main.yml delete mode 100644 roles/acme_client/tasks/main.yml delete mode 100644 roles/acme_client/templates/acme-client.conf.j2 delete mode 100644 roles/acme_client/templates/httpd.d/acme.conf delete mode 100644 roles/obsd_cgit/handlers/main.yml create mode 100644 roles/obsd_httpd/handlers/main.yml create mode 100644 roles/obsd_ssl/tasks/main.yml create mode 100644 roles/obsd_ssl/templates/acme-client.conf.j2 create mode 100644 roles/obsd_ssl/templates/httpd.d/acme.conf diff --git a/inventory/git.yml b/inventory/git.yml index 7a5c789..473bb85 100644 --- a/inventory/git.yml +++ b/inventory/git.yml @@ -1,10 +1,6 @@ all: hosts: git.eugenetriguba.com: - acme_domain: git.eugenetriguba.com - cgit_domain: git.eugenetriguba.com vars: ansible_user: admin ansible_python_interpreter: /usr/local/bin/python3 - git_authorized_keys: - - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC31SiCeUZSJN3CueH6/+7UQvxwxkT/QkaDK6ZGdnkdpCWxRVp+0CTO4WG3u0PVEVd9DvSd81CEqrFaKxtKyiSh+4Va0A/mqY27tX7NZzEQLM/zLQrdESmaHY6EyQM0wlCtBKfNzuQqCa9QEw4TwkyMLOJzyI+bRsfeSHNCfxqnqGIMKKFiaF0ygakvQz57nWuKLOgqWsWkzFTuJKGI0OhefFBcZ4VzoCLV8i1zbB2AebhsmbyMza9OUF3px44atSM3o33Jto3gy7LTWtxU83NZU+wLEqY6IvcWx9BbSQZxAwEwXxUFmnzcX+FTkTiOs/Q2nXiLl2ggsGJffAq9Z1SGMCNGxT+Syyta5UtfBVVQdn6xtUxpppHwrdGC+TR8C5F9U12gsZRzvKkz0SSt5M4WHG2gna5rDgTisDbrEAsrqpmUqY9iSBzgAyk5xOi6fdI+r67gpjRSs+PNvDle3gImfIkPnEQg4eWJ54iKjfH+yd8vBk25T/ahu3J9rGgxp18= eugene@laptop" # noqa: yaml[line-length] diff --git a/roles/acme_client/defaults/main.yml b/roles/acme_client/defaults/main.yml deleted file mode 100644 index 07c2a4a..0000000 --- a/roles/acme_client/defaults/main.yml +++ /dev/null @@ -1 +0,0 @@ -acme_client_domain: "{{ acme_domain }}" diff --git a/roles/acme_client/tasks/main.yml b/roles/acme_client/tasks/main.yml deleted file mode 100644 index 8b2e95f..0000000 --- a/roles/acme_client/tasks/main.yml +++ /dev/null @@ -1,45 +0,0 @@ -- name: Deploy acme challenge vhost fragment - ansible.builtin.template: - src: httpd.d/acme.conf - dest: "{{ obsd_httpd_conf_d | default('/etc/httpd.d') }}/acme.conf" - owner: root - group: wheel - mode: "0644" - register: acme_client_vhost_config - -- name: Deploy acme-client.conf - ansible.builtin.template: - src: acme-client.conf.j2 - dest: /etc/acme-client.conf - owner: root - group: wheel - mode: "0600" - -- name: Start httpd - ansible.builtin.service: - name: httpd - state: started - when: acme_client_vhost_config is changed # noqa: no-handler - -- name: Reload httpd - ansible.builtin.service: - name: httpd - state: reloaded - when: acme_client_vhost_config is changed # noqa: no-handler - -- name: Generate certificate - ansible.builtin.command: acme-client {{ acme_client_domain }} - args: - creates: "/etc/ssl/{{ acme_client_domain }}.fullchain.pem" - -- name: Add certificate renewal crontab entry - # Implements the example from `man acme-client` - # > A cron(8) job can renew the certificate as necessary. On renewal, - # > httpd(8) is reloaded: - # > - # > ~ * * * * acme-client example.com && rcctl reload httpd - ansible.builtin.cron: - name: "renew {{ acme_client_domain }} certificate" - minute: "~" - user: root - job: "acme-client {{ acme_client_domain }} && rcctl reload httpd" diff --git a/roles/acme_client/templates/acme-client.conf.j2 b/roles/acme_client/templates/acme-client.conf.j2 deleted file mode 100644 index 54036b9..0000000 --- a/roles/acme_client/templates/acme-client.conf.j2 +++ /dev/null @@ -1,15 +0,0 @@ -authority letsencrypt { - api url "https://acme-v02.api.letsencrypt.org/directory" - account key "/etc/acme/letsencrypt-privkey.pem" -} - -authority letsencrypt-staging { - api url "https://acme-staging-v02.api.letsencrypt.org/directory" - account key "/etc/acme/letsencrypt-staging-privkey.pem" -} - -domain "{{ acme_client_domain }}" { - domain key "/etc/ssl/private/{{ acme_client_domain }}.key" - domain full chain certificate "/etc/ssl/{{ acme_client_domain }}.fullchain.pem" - sign with letsencrypt -} diff --git a/roles/acme_client/templates/httpd.d/acme.conf b/roles/acme_client/templates/httpd.d/acme.conf deleted file mode 100644 index 905da28..0000000 --- a/roles/acme_client/templates/httpd.d/acme.conf +++ /dev/null @@ -1,11 +0,0 @@ -server "{{ acme_client_domain }}" { - listen on * port 80 - location "/.well-known/acme-challenge/*" { - root "/acme" - request strip 2 - } - location * { - block return 302 "https://$HTTP_HOST$REQUEST_URI" - } -} - diff --git a/roles/obsd_cgit/handlers/main.yml b/roles/obsd_cgit/handlers/main.yml deleted file mode 100644 index 356bdcb..0000000 --- a/roles/obsd_cgit/handlers/main.yml +++ /dev/null @@ -1,4 +0,0 @@ -- name: Reload httpd - ansible.builtin.service: - name: httpd - state: reloaded diff --git a/roles/obsd_cgit/tasks/main.yml b/roles/obsd_cgit/tasks/main.yml index 1d19d18..59b0e62 100644 --- a/roles/obsd_cgit/tasks/main.yml +++ b/roles/obsd_cgit/tasks/main.yml @@ -77,10 +77,15 @@ - name: Compile markdown renderer into chroot ansible.builtin.include_tasks: markdown.yml +- name: Enable slowcgi + ansible.builtin.service: + name: slowcgi + enabled: true + - name: Deploy cgit vhost fragment ansible.builtin.template: src: httpd.d/cgit.conf.j2 - dest: "{{ obsd_httpd_conf_d | default('/etc/httpd.d') }}/cgit.conf" + dest: "{{ obsd_httpd_conf_d }}/cgit.conf" owner: root group: wheel mode: "0644" diff --git a/roles/obsd_git/tasks/main.yml b/roles/obsd_git/tasks/main.yml index 4d200f0..91388cf 100644 --- a/roles/obsd_git/tasks/main.yml +++ b/roles/obsd_git/tasks/main.yml @@ -53,7 +53,7 @@ - name: Deploy authorized_keys ansible.builtin.copy: - content: "{{ git_authorized_keys | join('\n') }}\n" + content: "{{ obsd_git_authorized_keys | join('\n') }}\n" dest: "{{ obsd_git_home }}/.ssh/authorized_keys" owner: "{{ obsd_git_user }}" group: "{{ obsd_git_user }}" diff --git a/roles/obsd_httpd/defaults/main.yml b/roles/obsd_httpd/defaults/main.yml index f7b6b57..8dda1e9 100644 --- a/roles/obsd_httpd/defaults/main.yml +++ b/roles/obsd_httpd/defaults/main.yml @@ -1 +1,2 @@ obsd_httpd_conf_d: /etc/httpd.d +obsd_httpd_fragments: diff --git a/roles/obsd_httpd/handlers/main.yml b/roles/obsd_httpd/handlers/main.yml new file mode 100644 index 0000000..356bdcb --- /dev/null +++ b/roles/obsd_httpd/handlers/main.yml @@ -0,0 +1,4 @@ +- name: Reload httpd + ansible.builtin.service: + name: httpd + state: reloaded diff --git a/roles/obsd_httpd/tasks/main.yml b/roles/obsd_httpd/tasks/main.yml index 2dd1b30..ef0fd41 100644 --- a/roles/obsd_httpd/tasks/main.yml +++ b/roles/obsd_httpd/tasks/main.yml @@ -13,7 +13,6 @@ owner: root group: wheel mode: "0600" - notify: Reload httpd - name: Ensure all registered httpd fragment files exist ansible.builtin.copy: @@ -23,12 +22,9 @@ owner: root group: wheel mode: "0644" - loop: "{{ httpd_fragments }}" + loop: "{{ obsd_httpd_fragments }}" -- name: Enable httpd and slowcgi +- name: Enable httpd ansible.builtin.service: - name: "{{ item }}" + name: httpd enabled: true - loop: - - httpd - - slowcgi diff --git a/roles/obsd_httpd/templates/httpd.conf.j2 b/roles/obsd_httpd/templates/httpd.conf.j2 index 2825853..2cd2e4a 100644 --- a/roles/obsd_httpd/templates/httpd.conf.j2 +++ b/roles/obsd_httpd/templates/httpd.conf.j2 @@ -1,5 +1,5 @@ types { include "/usr/share/misc/mime.types" } -{% for fragment in httpd_fragments %} +{% for fragment in obsd_httpd_fragments %} include "{{ obsd_httpd_conf_d }}/{{ fragment }}" {% endfor %} diff --git a/roles/obsd_ssl/tasks/main.yml b/roles/obsd_ssl/tasks/main.yml new file mode 100644 index 0000000..ff8cadc --- /dev/null +++ b/roles/obsd_ssl/tasks/main.yml @@ -0,0 +1,52 @@ +- name: Check mandatory variables are defined + assert: + that: + - obsd_ssl_domains is defined + +- name: Deploy acme challenge vhost fragment + ansible.builtin.template: + src: httpd.d/acme.conf + dest: "{{ obsd_httpd_conf_d }}/acme.conf" + owner: root + group: wheel + mode: "0644" + register: acme_client_vhost_config + +- name: Deploy acme-client.conf + ansible.builtin.template: + src: acme-client.conf.j2 + dest: /etc/acme-client.conf + owner: root + group: wheel + mode: "0600" + +- name: Start httpd + ansible.builtin.service: + name: httpd + state: started + when: acme_client_vhost_config is changed # noqa: no-handler + +- name: Reload httpd + ansible.builtin.service: + name: httpd + state: reloaded + when: acme_client_vhost_config is changed # noqa: no-handler + +- name: Generate certificate + ansible.builtin.command: acme-client {{ item }} + args: + creates: "/etc/ssl/{{ item }}.fullchain.pem" + loop: "{{ obsd_ssl_domains }}" + +- name: Add certificate renewal crontab entry + # Implements the example from `man acme-client` + # > A cron(8) job can renew the certificate as necessary. On renewal, + # > httpd(8) is reloaded: + # > + # > ~ * * * * acme-client example.com && rcctl reload httpd + ansible.builtin.cron: + name: "renew {{ item }} certificate" + minute: "~" + user: root + job: "acme-client {{ item }} && rcctl reload httpd" + loop: "{{ obsd_ssl_domains }}" diff --git a/roles/obsd_ssl/templates/acme-client.conf.j2 b/roles/obsd_ssl/templates/acme-client.conf.j2 new file mode 100644 index 0000000..cb53f34 --- /dev/null +++ b/roles/obsd_ssl/templates/acme-client.conf.j2 @@ -0,0 +1,17 @@ +authority letsencrypt { + api url "https://acme-v02.api.letsencrypt.org/directory" + account key "/etc/acme/letsencrypt-privkey.pem" +} + +authority letsencrypt-staging { + api url "https://acme-staging-v02.api.letsencrypt.org/directory" + account key "/etc/acme/letsencrypt-staging-privkey.pem" +} + +{% for domain in obsd_ssl_domains %} +domain "{{ domain }}" { + domain key "/etc/ssl/private/{{ domain }}.key" + domain full chain certificate "/etc/ssl/{{ domain }}.fullchain.pem" + sign with letsencrypt +} +{% endfor %} diff --git a/roles/obsd_ssl/templates/httpd.d/acme.conf b/roles/obsd_ssl/templates/httpd.d/acme.conf new file mode 100644 index 0000000..357a294 --- /dev/null +++ b/roles/obsd_ssl/templates/httpd.d/acme.conf @@ -0,0 +1,12 @@ +{% for domain in obsd_ssl_domains %} +server "{{ domain }}" { + listen on * port 80 + location "/.well-known/acme-challenge/*" { + root "/acme" + request strip 2 + } + location * { + block return 302 "https://$HTTP_HOST$REQUEST_URI" + } +} +{% endfor %} diff --git a/site.yml b/site.yml index b68598d..b857e42 100644 --- a/site.yml +++ b/site.yml @@ -4,9 +4,17 @@ roles: - role: obsd_httpd vars: - httpd_fragments: + obsd_httpd_fragments: - acme.conf - cgit.conf - - acme_client - - obsd_git - - obsd_cgit + - role: obsd_ssl + vars: + obsd_ssl_domains: + - git.eugenetriguba.com + - role: obsd_git + vars: + obsd_git_authorized_keys: + - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC31SiCeUZSJN3CueH6/+7UQvxwxkT/QkaDK6ZGdnkdpCWxRVp+0CTO4WG3u0PVEVd9DvSd81CEqrFaKxtKyiSh+4Va0A/mqY27tX7NZzEQLM/zLQrdESmaHY6EyQM0wlCtBKfNzuQqCa9QEw4TwkyMLOJzyI+bRsfeSHNCfxqnqGIMKKFiaF0ygakvQz57nWuKLOgqWsWkzFTuJKGI0OhefFBcZ4VzoCLV8i1zbB2AebhsmbyMza9OUF3px44atSM3o33Jto3gy7LTWtxU83NZU+wLEqY6IvcWx9BbSQZxAwEwXxUFmnzcX+FTkTiOs/Q2nXiLl2ggsGJffAq9Z1SGMCNGxT+Syyta5UtfBVVQdn6xtUxpppHwrdGC+TR8C5F9U12gsZRzvKkz0SSt5M4WHG2gna5rDgTisDbrEAsrqpmUqY9iSBzgAyk5xOi6fdI+r67gpjRSs+PNvDle3gImfIkPnEQg4eWJ54iKjfH+yd8vBk25T/ahu3J9rGgxp18= eugene@laptop" # noqa: yaml[line-length] + - role: obsd_cgit + vars: + obsd_cgit_domain: git.eugenetriguba.com -- cgit v1.2.3