2020-02-10 00:50:56 -06:00
|
|
|
#!/usr/bin/ansible-playbook
|
|
|
|
# vim:ft=ansible:
|
|
|
|
---
|
|
|
|
- name: "Register certificate for {{ website_url }}"
|
|
|
|
block:
|
2020-02-10 01:05:38 -06:00
|
|
|
- name: Install SSL-required packages
|
|
|
|
apt:
|
2020-04-26 02:14:02 -05:00
|
|
|
name: python3-openssl
|
2020-02-10 00:50:56 -06:00
|
|
|
- name: Set up PKI filesystem hierarchy
|
|
|
|
file:
|
|
|
|
path: "{{ item.dir }}"
|
|
|
|
mode: "{{ item.mode }}"
|
|
|
|
owner: root
|
|
|
|
group: www-data
|
|
|
|
state: directory
|
|
|
|
loop:
|
|
|
|
- { dir: "/etc/pki", mode: "0750" }
|
|
|
|
- { dir: "/etc/pki/cert", mode: "0750" }
|
|
|
|
- { dir: "/etc/pki/cert/crt", mode: "0750" }
|
|
|
|
- { dir: "/etc/pki/cert/csr", mode: "0750" }
|
|
|
|
- { dir: "/etc/pki/cert/private", mode: "0750" }
|
|
|
|
- name: Create ACME account key
|
|
|
|
openssl_privatekey:
|
|
|
|
path: "/etc/pki/cert/private/account.key"
|
|
|
|
- name: Create certificate key
|
|
|
|
openssl_privatekey:
|
|
|
|
path: "/etc/pki/cert/private/{{ website_url }}.key"
|
2020-02-20 03:56:51 -06:00
|
|
|
mode: "0750"
|
2020-02-10 00:50:56 -06:00
|
|
|
- name: Create CSR
|
|
|
|
openssl_csr:
|
|
|
|
path: "/etc/pki/cert/csr/{{ website_url }}.csr"
|
|
|
|
common_name: "{{ website_url }}"
|
|
|
|
privatekey_path: /etc/pki/cert/private/{{ website_url }}.key
|
|
|
|
email_address: "rehashedsalt@cock.li"
|
|
|
|
- name: Create challenge for CSR
|
|
|
|
acme_certificate:
|
2020-08-02 19:32:07 -05:00
|
|
|
acme.directory: "{{ acme.directory }}"
|
|
|
|
acme.version: 2
|
2020-02-10 00:50:56 -06:00
|
|
|
terms_agreed: yes
|
|
|
|
account_email: "rehashedsalt@cock.li"
|
|
|
|
account_key: "/etc/pki/cert/private/account.key"
|
|
|
|
csr: "/etc/pki/cert/csr/{{ website_url }}.csr"
|
|
|
|
dest: "/etc/pki/cert/crt/{{ website_url }}.crt"
|
|
|
|
fullchain_dest: "/etc/pki/cert/crt/{{ website_url }}-fullchain.crt"
|
|
|
|
register: com_challenge
|
2020-02-20 04:19:03 -06:00
|
|
|
- name: Create or renew certificate
|
2020-02-10 00:50:56 -06:00
|
|
|
block:
|
2020-02-20 05:25:46 -06:00
|
|
|
- name: Fulfill challenge
|
|
|
|
block:
|
2020-02-27 05:16:02 -06:00
|
|
|
- name: Disable website
|
|
|
|
command:
|
|
|
|
cmd: "a2dissite {{ website_url }}.conf"
|
|
|
|
removes: "/etc/apache2/sites-enabled/{{ website_url }}.conf"
|
2020-02-20 05:25:46 -06:00
|
|
|
- name: Create temporary config
|
|
|
|
template:
|
|
|
|
src: apache2-vhost.conf
|
|
|
|
dest: "/etc/apache2/sites-enabled/{{ website_url }}.conf"
|
|
|
|
- name: Reload Apache
|
|
|
|
service:
|
|
|
|
name: apache2
|
|
|
|
state: reloaded
|
|
|
|
- name: Create well-known directory
|
|
|
|
file:
|
2020-08-02 19:32:07 -05:00
|
|
|
path: "{{ acme.webroot }}/.well-known/acme-challenge"
|
2020-02-20 05:25:46 -06:00
|
|
|
mode: "0755"
|
|
|
|
recurse: yes
|
|
|
|
state: directory
|
|
|
|
- name: Copy challenge files
|
|
|
|
copy:
|
2020-08-02 19:32:07 -05:00
|
|
|
dest: "{{ acme.webroot }}/{{ com_challenge['challenge_data'][website_url]['http-01']['resource'] }}"
|
2020-02-20 05:25:46 -06:00
|
|
|
content: "{{ com_challenge['challenge_data'][website_url]['http-01']['resource_value'] }}"
|
|
|
|
when: com_challenge['challenge_data']|length > 0
|
2020-02-20 04:19:03 -06:00
|
|
|
- name: Create certificate
|
|
|
|
acme_certificate:
|
2020-08-02 19:32:07 -05:00
|
|
|
acme.directory: "{{ acme.directory }}"
|
|
|
|
acme.version: 2
|
2020-02-20 04:19:03 -06:00
|
|
|
account_key: /etc/pki/cert/private/account.key
|
|
|
|
csr: "/etc/pki/cert/csr/{{ website_url }}.csr"
|
|
|
|
dest: "/etc/pki/cert/crt/{{ website_url }}.crt"
|
|
|
|
fullchain_dest: "/etc/pki/cert/crt/{{ website_url }}-fullchain.crt"
|
|
|
|
chain_dest: "/etc/pki/cert/crt/{{ website_url }}-intermediate.crt"
|
|
|
|
data: "{{ com_challenge }}"
|
2020-02-20 05:25:46 -06:00
|
|
|
- name: Assign appropriate permissions to certificate
|
2020-02-20 04:19:03 -06:00
|
|
|
file:
|
2020-02-20 05:25:46 -06:00
|
|
|
path: "{{ item }}"
|
|
|
|
owner: root
|
|
|
|
group: www-data
|
|
|
|
mode: "0640"
|
|
|
|
loop:
|
|
|
|
- "/etc/pki/cert/crt/{{ website_url }}.crt"
|
|
|
|
- "/etc/pki/cert/crt/{{ website_url }}-fullchain.crt"
|
|
|
|
- "/etc/pki/cert/crt/{{ website_url }}-intermediate.crt"
|
|
|
|
- name: Clean up fulfillment
|
|
|
|
block:
|
|
|
|
- name: Remove webroot
|
|
|
|
file:
|
2020-08-02 19:32:07 -05:00
|
|
|
path: "{{ acme.webroot }}/.well-known/acme-challenge"
|
2020-02-20 05:25:46 -06:00
|
|
|
state: absent
|
|
|
|
- name: Remove temporary config
|
|
|
|
file:
|
|
|
|
path: "/etc/apache2/sites-enabled/{{ website_url }}.conf"
|
|
|
|
state: absent
|
|
|
|
- name: Restore original config
|
2020-02-27 05:16:02 -06:00
|
|
|
command:
|
|
|
|
cmd: "a2ensite {{ website_url }}.conf"
|
2020-02-20 05:25:46 -06:00
|
|
|
creates: "/etc/apache2/sites-enabled/{{ website_url }}.conf"
|
|
|
|
when: com_challenge['challenge_data']|length > 0
|
2020-02-20 04:19:03 -06:00
|
|
|
- name: Reload Apache
|
|
|
|
service:
|
|
|
|
name: apache2
|
|
|
|
state: reloaded
|
2020-02-10 00:50:56 -06:00
|
|
|
when: com_challenge is changed
|
|
|
|
become: yes
|