diff --git a/defaults/main.yml b/defaults/main.yml index 99e9f20..f55f25c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -6,6 +6,7 @@ certbot_auto_renew_hour: "3" certbot_auto_renew_minute: "30" certbot_auto_renew_options: "--quiet" +certbot_expand: true certbot_testmode: false certbot_hsts: false @@ -14,13 +15,13 @@ certbot_hsts: false certbot_create_if_missing: false certbot_create_method: standalone certbot_admin_email: email@example.com -certbot_expand: false # Default webroot, overwritten by individual per-cert webroot directories certbot_webroot: /var/www/letsencrypt certbot_certs: [] -# - email: janedoe@example.com +# - name: example.com +# email: janedoe@example.com # webroot: "/var/www/html/" # domains: # - example1.com @@ -40,8 +41,9 @@ certbot_create_command: >- {{ '--webroot-path ' if certbot_create_method == 'webroot' else '' }} {{ cert_item.webroot | default(certbot_webroot) if certbot_create_method == 'webroot' else '' }} {{ certbot_create_extra_args }} - --cert-name {{ cert_item.name | default(cert_item.domains | first | replace('*.', '') ) }} + --cert-name {{ cert_item_name }} -d {{ cert_item.domains | join(',') }} + {{ '--expand' if certbot_expand else '' }} {{ '--pre-hook /etc/letsencrypt/renewal-hooks/pre/stop_services' if certbot_create_standalone_stop_services and certbot_create_method == 'standalone' else '' }} diff --git a/tasks/create-cert-standalone.yml b/tasks/create-cert-standalone.yml index 0fb9348..4198d35 100644 --- a/tasks/create-cert-standalone.yml +++ b/tasks/create-cert-standalone.yml @@ -1,4 +1,13 @@ --- +- name: Determine certificate name + set_fact: + cert_item_name: "{{ cert_item.name | default(cert_item.domains | first | replace('*.', '')) }}" + +- name: Check if certificate already exists. + stat: + path: /etc/letsencrypt/live/{{ cert_item_name }}/cert.pem + register: letsencrypt_cert + - name: Ensure pre and post hook folders exist. file: path: /etc/letsencrypt/renewal-hooks/{{ item }} @@ -32,7 +41,23 @@ - certbot_create_standalone_stop_services is defined - certbot_create_standalone_stop_services +- name: Check if domains have changed + block: + - name: Register certificate domains + shell: "{{ certbot_script }} certificates --cert-name {{ cert_item_name }} | grep Domains | cut -d':' -f2" + changed_when: false + register: letsencrypt_cert_domains_dirty + + - name: Cleanup domain list + set_fact: + letsencrypt_cert_domains: "{{ letsencrypt_cert_domains_dirty.stdout | trim | split(' ') | map('trim') | select('!=', '') | list | sort }}" + + - name: Determine if domains have changed + set_fact: + letsencrypt_cert_domains_changed: "{{ letsencrypt_cert_domains != (cert_item.domains | map('trim') | select('!=', '') | list | sort) }}" + + when: letsencrypt_cert.stat.exists + - name: Generate new certificate if one doesn't exist. command: "{{ certbot_create_command }}" - register: certbot_create - changed_when: "'no action taken' not in certbot_create.stdout" + when: not letsencrypt_cert.stat.exists or letsencrypt_cert_domains_changed | default(false) diff --git a/tasks/create-cert-webroot.yml b/tasks/create-cert-webroot.yml index 6fca89b..b379577 100644 --- a/tasks/create-cert-webroot.yml +++ b/tasks/create-cert-webroot.yml @@ -1,10 +1,35 @@ --- +- name: Determine certificate name + set_fact: + cert_item_name: "{{ cert_item.name | default(cert_item.domains | first | replace('*.', '')) }}" + +- name: Check if certificate already exists. + stat: + path: /etc/letsencrypt/live/{{ cert_item_name }}/cert.pem + register: letsencrypt_cert + - name: Create webroot directory if it doesn't exist yet file: path: "{{ cert_item.webroot | default(certbot_webroot) }}" state: directory +- name: Check if domains have changed + block: + - name: Register certificate domains + shell: "{{ certbot_script }} certificates --cert-name {{ cert_item_name }} | grep Domains | cut -d':' -f2" + changed_when: false + register: letsencrypt_cert_domains_dirty + + - name: Cleanup domain list + set_fact: + letsencrypt_cert_domains: "{{ letsencrypt_cert_domains_dirty.stdout | trim | split(' ') | map('trim') | select('!=', '') | list | sort }}" + + - name: Determine if domains have changed + set_fact: + letsencrypt_cert_domains_changed: "{{ letsencrypt_cert_domains != (cert_item.domains | map('trim') | select('!=', '') | list | sort) }}" + + when: letsencrypt_cert.stat.exists + - name: Generate new certificate if one doesn't exist. command: "{{ certbot_create_command }}" - register: certbot_create - changed_when: "'no action taken' not in certbot_create.stdout" + when: not letsencrypt_cert.stat.exists or letsencrypt_cert_domains_changed | default(false)