--- - name: install certbot and dnsutils ansible.builtin.apt: name: - certbot - dnsutils state: present update_cache: true - name: install certbot cloudflare plugin ansible.builtin.apt: name: python3-certbot-dns-cloudflare state: present when: >- dnsmasq | selectattr('cert_method', 'defined') | selectattr('cert_method', 'equalto', 'cloudflare_dns') | list | length > 0 - name: resolve certificate domain ansible.builtin.command: cmd: "dig +short A {{ item.name }}" loop: "{{ dnsmasq }}" loop_control: label: "{{ item.name }}" register: dns_check changed_when: false check_mode: false when: item.cert | default(false) - name: fail if a domain does not point at this server ansible.builtin.fail: msg: "{{ item.item.name }} does not resolve to {{ ip }}" loop: "{{ dns_check.results }}" loop_control: label: "{{ item.item.name }}" when: - item.item.cert | default(false) - item.item.cert_method | default('standalone') != 'cloudflare_dns' - (ip | default(ansible_host)) not in item.stdout_lines - name: create cloudflare secret ansible.builtin.copy: content: "dns_cloudflare_api_token = {{ item.cloudflare_token }}\n" dest: "/etc/letsencrypt/cloudflare-{{ item.name }}.ini" mode: '0600' loop: "{{ dnsmasq }}" loop_control: label: "{{ item.name }}" when: - item.cert | default(false) - item.cert_method | default('standalone') == 'cloudflare_dns' no_log: true - name: issue certificate if missing ansible.builtin.command: cmd: >- certbot certonly --non-interactive --agree-tos --register-unsafely-without-email {% if item.cert_method | default('standalone') == 'cloudflare_dns' %} --dns-cloudflare --dns-cloudflare-credentials /etc/letsencrypt/cloudflare-{{ item.name }}.ini --dns-cloudflare-propagation-seconds 60 {% else %} --standalone {% if item.pre_hook is defined %}--pre-hook "{{ item.pre_hook }}"{% endif %} {% if item.post_hook is defined %}--post-hook "{{ item.post_hook }}"{% endif %} {% endif %} {% if item.deploy_hook is defined %}--deploy-hook "{{ item.deploy_hook }}"{% endif %} -d {{ item.name }} {% if item.cert_wildcard | default(false) %}-d '*.{{ item.name }}'{% endif %} creates: "/etc/letsencrypt/live/{{ item.name }}/fullchain.pem" loop: "{{ dnsmasq }}" loop_control: label: "{{ item.name }}" when: item.cert | default(false)