2023-04-25 13:49:45 +02:00
|
|
|
- name: Ensure certs directory exists
|
|
|
|
ansible.builtin.file:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: /etc/ansible_certs
|
2023-04-25 13:49:45 +02:00
|
|
|
state: directory
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "755"
|
|
|
|
- name: Ensure sub-directory for the certificate exists
|
|
|
|
ansible.builtin.file:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: "/etc/ansible_certs/{{ item }}"
|
2023-04-25 13:49:45 +02:00
|
|
|
state: directory
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "755"
|
|
|
|
- name: Ensure private key is generated
|
|
|
|
community.crypto.openssl_privatekey:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: "/etc/ansible_certs/{{ item }}/key.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
size: 4096
|
|
|
|
type: RSA
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0600"
|
|
|
|
- name: Ensure certificate signing request is created
|
|
|
|
community.crypto.openssl_csr:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: "/etc/ansible_certs/{{ item }}/csr.pem"
|
|
|
|
privatekey_path: "/etc/ansible_certs/{{ item }}/key.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
common_name: "{{ item }}"
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
|
|
|
register: cert__csr_result
|
|
|
|
- name: Check certificate status and create ACME challenge if needed
|
|
|
|
community.crypto.acme_certificate:
|
|
|
|
account_email: "{{ cert__acme_account.email }}"
|
|
|
|
account_key_content: "{{ cert__acme_account.key }}"
|
|
|
|
acme_directory: https://acme-v02.api.letsencrypt.org/directory
|
|
|
|
acme_version: 2
|
|
|
|
remaining_days: 28
|
|
|
|
terms_agreed: true
|
|
|
|
challenge: dns-01
|
2023-04-25 16:57:34 +02:00
|
|
|
csr: "/etc/ansible_certs/{{ item }}/csr.pem"
|
|
|
|
dest: "/etc/ansible_certs/{{ item }}/cert.pem"
|
|
|
|
fullchain_dest: "/etc/ansible_certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__acme_challenge
|
|
|
|
- name: Retrieve certificate and fulfill challenge if needed # noqa no-handler
|
|
|
|
when: cert__acme_challenge.changed # Can't be put in a handler, because then the block "always" tasks won't be executed for some reason
|
|
|
|
block:
|
|
|
|
- name: Set DNS record via Cloudflare API to fulfill the challenge
|
|
|
|
when: not cert__acme_challenge.authorizations[item].status == "valid"
|
|
|
|
community.general.cloudflare_dns:
|
|
|
|
api_token: "{{ cert__cloudflare_dns.api_token }}"
|
|
|
|
zone: "{{ cert__cloudflare_dns.zone }}"
|
|
|
|
record: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].record }}"
|
|
|
|
value: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].resource_value }}"
|
|
|
|
type: TXT
|
|
|
|
ttl: 60
|
|
|
|
solo: true
|
|
|
|
state: present
|
|
|
|
- name: Retrieve certificate
|
|
|
|
community.crypto.acme_certificate:
|
|
|
|
account_email: "{{ cert__acme_account.email }}"
|
|
|
|
account_key_content: "{{ cert__acme_account.key }}"
|
|
|
|
acme_directory: https://acme-v02.api.letsencrypt.org/directory
|
|
|
|
acme_version: 2
|
|
|
|
terms_agreed: true
|
|
|
|
remaining_days: 28
|
|
|
|
challenge: dns-01
|
2023-04-25 16:57:34 +02:00
|
|
|
csr: "/etc/ansible_certs/{{ item }}/csr.pem"
|
|
|
|
dest: "/etc/ansible_certs/{{ item }}/cert.pem"
|
|
|
|
fullchain_dest: "/etc/ansible_certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
data: "{{ cert__acme_challenge }}"
|
|
|
|
notify: "{{ cert__handlers }}"
|
|
|
|
always:
|
|
|
|
- name: Ensure DNS record is removed
|
|
|
|
when: not cert__acme_challenge.authorizations[item].status == "valid"
|
|
|
|
community.general.cloudflare_dns:
|
|
|
|
api_token: "{{ cert__cloudflare_dns.api_token }}"
|
|
|
|
zone: "{{ cert__cloudflare_dns.zone }}"
|
|
|
|
record: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].record }}"
|
|
|
|
value: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].resource_value }}"
|
|
|
|
type: TXT
|
|
|
|
ttl: 60
|
|
|
|
state: absent
|
|
|
|
- name: Ensure correct permissions for certificate are set
|
|
|
|
ansible.builtin.file:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: "/etc/ansible_certs/{{ item }}/cert.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
|
|
|
- name: Ensure correct permissions for fullchain cert are set
|
|
|
|
ansible.builtin.file:
|
2023-04-25 16:57:34 +02:00
|
|
|
path: "/etc/ansible_certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
|
|
|
- name: Get content of cert.pem
|
|
|
|
ansible.builtin.slurp:
|
2023-04-25 16:57:34 +02:00
|
|
|
src: "/etc/ansible_certs/{{ item }}/cert.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__cert_slurp
|
|
|
|
- name: Get content of fullchain.pem
|
|
|
|
ansible.builtin.slurp:
|
2023-04-25 16:57:34 +02:00
|
|
|
src: "/etc/ansible_certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__fullchain_slurp
|
|
|
|
- name: Ensure ca.pem is created
|
|
|
|
ansible.builtin.copy:
|
|
|
|
content: "{{ cert__fullchain_slurp.content | b64decode | replace(cert__cert_slurp.content | b64decode, '') }}"
|
2023-04-25 16:57:34 +02:00
|
|
|
dest: "/etc/ansible_certs/{{ item }}/ca.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|