2023-04-25 18:03:59 +02:00
|
|
|
- name: Ensure `ansible_certs` directory exists
|
2023-04-25 13:49:45 +02:00
|
|
|
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"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 18:03:59 +02:00
|
|
|
- name: Ensure `certs` sub-directory exists
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: /etc/ansible_certs/certs
|
|
|
|
state: directory
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "755"
|
|
|
|
become: true
|
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Ensure sub-directory for the certificate exists
|
|
|
|
ansible.builtin.file:
|
2023-04-25 18:03:59 +02:00
|
|
|
path: "/etc/ansible_certs/certs/{{ item }}"
|
2023-04-25 13:49:45 +02:00
|
|
|
state: directory
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "755"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 18:03:59 +02:00
|
|
|
- name: Ensure private key for certificate exists
|
2023-04-25 13:49:45 +02:00
|
|
|
community.crypto.openssl_privatekey:
|
2023-05-01 00:18:59 +02:00
|
|
|
path: "/etc/ansible_certs/certs/{{ item }}/privkey.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
size: 4096
|
|
|
|
type: RSA
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0600"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Ensure certificate signing request is created
|
|
|
|
community.crypto.openssl_csr:
|
2023-04-25 18:03:59 +02:00
|
|
|
path: "/etc/ansible_certs/certs/{{ item }}/csr.pem"
|
2023-05-01 00:18:59 +02:00
|
|
|
privatekey_path: "/etc/ansible_certs/certs/{{ item }}/privkey.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
common_name: "{{ item }}"
|
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__csr_result
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 18:03:59 +02:00
|
|
|
- name: Ensure private key for ACME account exists
|
|
|
|
community.crypto.openssl_privatekey:
|
|
|
|
path: "/etc/ansible_certs/account_key.pem"
|
|
|
|
size: 4096
|
|
|
|
type: RSA
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0600"
|
|
|
|
become: true
|
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Check certificate status and create ACME challenge if needed
|
|
|
|
community.crypto.acme_certificate:
|
2023-04-25 18:03:59 +02:00
|
|
|
account_email: "{{ cert__acme_account_email }}"
|
|
|
|
account_key_src: "/etc/ansible_certs/account_key.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
acme_directory: https://acme-v02.api.letsencrypt.org/directory
|
|
|
|
acme_version: 2
|
|
|
|
remaining_days: 28
|
|
|
|
terms_agreed: true
|
|
|
|
challenge: dns-01
|
2023-04-25 18:03:59 +02:00
|
|
|
csr: "/etc/ansible_certs/certs/{{ item }}/csr.pem"
|
|
|
|
dest: "/etc/ansible_certs/certs/{{ item }}/cert.pem"
|
|
|
|
fullchain_dest: "/etc/ansible_certs/certs/{{ item }}/fullchain.pem"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__acme_challenge
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- 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:
|
2023-04-27 23:38:14 +02:00
|
|
|
- name: Add file containing nsupdate commands for adding TXT record for DNS-01 challenge
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: nsupdate_add_txt_record.j2
|
|
|
|
dest: /root/nsupdate_add_txt_record
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0600"
|
|
|
|
vars:
|
|
|
|
cert__nsupdate_domain: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].record }}"
|
|
|
|
cert__nsupdate_txt_data: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].resource_value }}"
|
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
|
|
|
|
|
|
|
- name: Add DNS record to BIND 9 server via nsupdate # noqa: no-changed-when
|
|
|
|
ansible.builtin.command: /usr/bin/nsupdate -l /root/nsupdate_add_txt_record
|
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Retrieve certificate
|
|
|
|
community.crypto.acme_certificate:
|
2023-04-25 18:03:59 +02:00
|
|
|
account_email: "{{ cert__acme_account_email }}"
|
|
|
|
account_key_src: "/etc/ansible_certs/account_key.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
acme_directory: https://acme-v02.api.letsencrypt.org/directory
|
|
|
|
acme_version: 2
|
|
|
|
terms_agreed: true
|
|
|
|
remaining_days: 28
|
|
|
|
challenge: dns-01
|
2023-04-25 18:03:59 +02:00
|
|
|
csr: "/etc/ansible_certs/certs/{{ item }}/csr.pem"
|
|
|
|
dest: "/etc/ansible_certs/certs/{{ item }}/cert.pem"
|
|
|
|
fullchain_dest: "/etc/ansible_certs/certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
data: "{{ cert__acme_challenge }}"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 13:49:45 +02:00
|
|
|
notify: "{{ cert__handlers }}"
|
|
|
|
always:
|
2023-04-27 23:38:14 +02:00
|
|
|
- name: Remove file containing nsupdate commands for adding TXT record again
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: /root/nsupdate_add_txt_record
|
2023-04-25 13:49:45 +02:00
|
|
|
state: absent
|
2023-04-27 23:38:14 +02:00
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
|
|
|
|
|
|
|
- name: Remove TXT record again
|
|
|
|
block:
|
|
|
|
- name: Add file containing nsupdate commands for deleting TXT record for DNS-01 challenge
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: nsupdate_delete_txt_record.j2
|
|
|
|
dest: /root/nsupdate_delete_txt_record
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0600"
|
|
|
|
vars:
|
|
|
|
cert__nsupdate_domain: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].record }}"
|
|
|
|
cert__nsupdate_txt_data: "{{ cert__acme_challenge.challenge_data[item]['dns-01'].resource_value }}"
|
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
|
|
|
|
|
|
|
- name: Remove DNS record from BIND 9 server via nsupdate # noqa: no-changed-when
|
|
|
|
ansible.builtin.command: /usr/bin/nsupdate -l /root/nsupdate_delete_txt_record
|
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
|
|
|
always:
|
|
|
|
- name: Remove file containing nsupdate commands for deleting TXT record again
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: /root/nsupdate_delete_txt_record
|
|
|
|
state: absent
|
|
|
|
delegate_to: "{{ cert__bind_9_host }}"
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Ensure correct permissions for certificate are set
|
|
|
|
ansible.builtin.file:
|
2023-04-25 18:03:59 +02:00
|
|
|
path: "/etc/ansible_certs/certs/{{ item }}/cert.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Ensure correct permissions for fullchain cert are set
|
|
|
|
ansible.builtin.file:
|
2023-04-25 18:03:59 +02:00
|
|
|
path: "/etc/ansible_certs/certs/{{ item }}/fullchain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Get content of cert.pem
|
|
|
|
ansible.builtin.slurp:
|
2023-04-25 18:03:59 +02:00
|
|
|
src: "/etc/ansible_certs/certs/{{ item }}/cert.pem"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__cert_slurp
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-04-25 13:49:45 +02:00
|
|
|
- name: Get content of fullchain.pem
|
|
|
|
ansible.builtin.slurp:
|
2023-04-25 18:03:59 +02:00
|
|
|
src: "/etc/ansible_certs/certs/{{ item }}/fullchain.pem"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|
2023-04-25 13:49:45 +02:00
|
|
|
register: cert__fullchain_slurp
|
2023-04-25 16:59:02 +02:00
|
|
|
|
2023-05-01 00:18:59 +02:00
|
|
|
- name: Ensure chain.pem is created
|
2023-04-25 13:49:45 +02:00
|
|
|
ansible.builtin.copy:
|
|
|
|
content: "{{ cert__fullchain_slurp.content | b64decode | replace(cert__cert_slurp.content | b64decode, '') }}"
|
2023-05-01 00:18:59 +02:00
|
|
|
dest: "/etc/ansible_certs/certs/{{ item }}/chain.pem"
|
2023-04-25 13:49:45 +02:00
|
|
|
owner: "{{ cert__owner }}"
|
|
|
|
group: "{{ cert__group }}"
|
|
|
|
mode: "0660"
|
2023-04-25 17:13:10 +02:00
|
|
|
become: true
|