Skip to content

Commit 66ce501

Browse files
✨ Add certificate validity check and renewal logic
Add task to check existing certificate validity period and update certificate request logic to handle renewal based on profile: - shortlived profile: renew if validity > 30 days - default profile: renew if validity <= 30 days Add force-renewal flag for existing certificates
1 parent f14ffe4 commit 66ce501

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

roles/acme_certificates/tasks/main.yaml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,35 @@
6363
register: acme_certificates_cert_status
6464
loop_control:
6565
label: "{{ item.domain }}"
66-
- name: Request certificates for new domains from Let's Encrypt
67-
throttle: 1
68-
when: not item.stat.exists
66+
- name: Get certificate validity period for existing certs
67+
when: item.stat.exists
68+
ansible.builtin.shell:
69+
cmd: |
70+
start=$(openssl x509 -startdate -noout -in /etc/letsencrypt/live/{{ item.item.domain }}/cert.pem | cut -d= -f2)
71+
end=$(openssl x509 -enddate -noout -in /etc/letsencrypt/live/{{ item.item.domain }}/cert.pem | cut -d= -f2)
72+
echo $(( ($(date -d "$end" +%s) - $(date -d "$start" +%s)) / 86400 ))
6973
loop: "{{ acme_certificates_cert_status.results }}"
7074
loop_control:
7175
label: "{{ item.item.domain }}"
76+
register: acme_certificates_cert_validity
77+
changed_when: false
78+
- name: Request certificates for new domains from Let's Encrypt
79+
throttle: 1
80+
when: >-
81+
not item.0.stat.exists or
82+
(item.0.stat.exists and acme_certificates_profile == 'shortlived' and (item.1.stdout | default('0') | int) > 30) or
83+
(item.0.stat.exists and acme_certificates_profile == 'default' and (item.1.stdout | default('90') | int) <= 30)
84+
loop: "{{ acme_certificates_cert_status.results | zip(acme_certificates_cert_validity.results | default([])) | list }}"
85+
loop_control:
86+
label: "{{ item.0.item.domain }}"
7287
ansible.builtin.command:
7388
cmd: >-
7489
certbot certonly -n -m '{{ acme_certificates_cert_email }}' --agree-tos --manual
7590
--manual-auth-hook '/usr/local/bin/acme-dns-certbot-hook -config /etc/acme-dns.cfg'
7691
--preferred-challenges dns
92+
{% if item.0.stat.exists %}--force-renewal{% endif %}
7793
{% if acme_certificates_profile == 'shortlived' %}--preferred-profile shortlived{% endif %}
78-
-d {{ item.item.domain }} -d '*.{{ item.item.domain }}'
94+
-d {{ item.0.item.domain }} -d '*.{{ item.0.item.domain }}'
7995
- name: Prepare haproxy certs
8096
ansible.builtin.command:
8197
cmd: /usr/local/bin/prepare-certs.sh

0 commit comments

Comments
 (0)