Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ No dependencies required.

## Role Variables

- `lego_version`:
- Version of lego to install (tag in its GitHub repo). Simple appended to `https://api.github.com/repos/go-acme/lego/releases/tags/`.
- Required

- `lego_email`:
- Domain administrators email address. It will be passed to the ACME provider (e.g. LetsEncrypt).
- Required
Expand All @@ -52,7 +56,7 @@ No dependencies required.
- Default: `[]`

- `lego_hook`:
- Lego run/renew certificate hook. The hook is executed only when the certificates are effectively obtained/renewed.
- Lego run/renew certificate hook. The hook is executed only when the certificates are effectively obtained/renewed. Can be a multi line string to include multiple commands.
- Default:

- `lego_link_certificate_path`:
Expand Down
10 changes: 3 additions & 7 deletions files/lego-renew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

CONF_DIR="/etc/lego"
LEGO_BIN="/opt/lego/lego"
HOOK_SCRIPT="/opt/lego/hook.sh"
DOMAIN="$1"
CERT_PATH="${CONF_DIR}/certificates/${DOMAIN}.crt"

Expand All @@ -12,10 +13,5 @@ test -f "${CONF_DIR}/config/${DOMAIN}" && . "${CONF_DIR}/config/${DOMAIN}"
set +a

set -e
if [ -f "${CERT_PATH}" ]; then
echo "Renew ${DOMAIN} certificate if needed"
"${LEGO_BIN}" --accept-tos --path "${CONF_DIR}" $ARGS $DOMAIN_ARGS renew --renew-hook "$HOOK"
else
echo "Obtain ${DOMAIN} certificate"
"${LEGO_BIN}" --accept-tos --path "${CONF_DIR}" $ARGS $DOMAIN_ARGS run --run-hook "$HOOK"
fi
echo "Obtaining/renewing ${DOMAIN} certificate"
"${LEGO_BIN}" run --accept-tos --path "${CONF_DIR}" $ARGS $DOMAIN_ARGS --deploy-hook "$HOOK_SCRIPT"
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- name: Converge
hosts: all
vars:
lego_version: v5.2.2
lego_email: lego@localhost
lego_domains:
- molecule.lego.elan
Expand Down
11 changes: 10 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

- name: Fetch download URL
ansible.builtin.uri:
url: https://api.github.com/repos/go-acme/lego/releases/latest
url: https://api.github.com/repos/go-acme/lego/releases/tags/{{ lego_version }}
return_content: true
register: lego_release_latest

Expand Down Expand Up @@ -100,6 +100,15 @@
mode: "0755"
notify: Renew certificate

- name: Install hook script
ansible.builtin.template:
src: hook.sh
dest: /opt/lego/
owner: root
group: root
mode: "0755"
notify: Renew certificate

- name: Create service and timer definitions
ansible.builtin.copy:
src: "{{ item }}"
Expand Down
1 change: 0 additions & 1 deletion templates/env_domain
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DOMAIN_ARGS="{{ lego_domain_extra_args | join(' ') }}{% for lego_domain in lego_domains %} --domains {{ lego_domain }}{% endfor %}"
HOOK="{{ lego_hook }}"

{% for lego_domain_env_var in lego_domain_env_vars %}
{{ lego_domain_env_var }}
Expand Down
3 changes: 3 additions & 0 deletions templates/hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

{{ lego_hook }}