|
63 | 63 | register: acme_certificates_cert_status |
64 | 64 | loop_control: |
65 | 65 | 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 )) |
69 | 73 | loop: "{{ acme_certificates_cert_status.results }}" |
70 | 74 | loop_control: |
71 | 75 | 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 }}" |
72 | 87 | ansible.builtin.command: |
73 | 88 | cmd: >- |
74 | 89 | certbot certonly -n -m '{{ acme_certificates_cert_email }}' --agree-tos --manual |
75 | 90 | --manual-auth-hook '/usr/local/bin/acme-dns-certbot-hook -config /etc/acme-dns.cfg' |
76 | 91 | --preferred-challenges dns |
| 92 | + {% if item.0.stat.exists %}--force-renewal{% endif %} |
77 | 93 | {% 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 }}' |
79 | 95 | - name: Prepare haproxy certs |
80 | 96 | ansible.builtin.command: |
81 | 97 | cmd: /usr/local/bin/prepare-certs.sh |
|
0 commit comments