I'm quite new in Ansible world and I just wrote an Ansible role and playbook to handle my Let's Encrypt SSL certificates.
---
# create/update SSL/TLS certificate using letsencrypt service
#
- name: check & install openssl package
apt:
update_cache: yes
cache_valid_time: 3600
name: openssl
- name: copy the accountkey
copy:
src: 'letsencrypt_account.key'
dest: '/tmp/letsencrypt_account.key'
owner: root
group: root
mode: 0400
delegate_to: 127.0.0.1
- name: check if the private key exists
stat:
path: "{{ ssl_cert_key }}"
register: sslcert_key_exists
- name: create RSA private key if not exist
command: "openssl genrsa -out {{ ssl_cert_key }} 2048"
when: sslcert_key_exists.stat.exists != True
- name: check if the CRT exists
stat:
path: "{{ ssl_cert_crt }}"
register: sslcert_crt_exists
- name: create an initial csr
shell: 'openssl req -key {{ ssl_cert_key }} -new -out {{ ssl_cert_csr }}
-subj "/C=FR/ST=Bouches du Rhone/L=Fuveau/O=Ricozome/OU=mailgate/CN={{ ansible_hostname }}.ricozome.net" -reqexts SAN
-config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName={{ ssl_cert_subjectAltName }}"))'
args:
executable: /bin/bash
when: sslcert_crt_exists.stat.exists != True
- name: create the CSR from the existing certificate
command: "openssl x509 -in {{ ssl_cert_crt }} -signkey {{ ssl_cert_key }} -x509toreq -out {{ ssl_cert_csr }}"
when: sslcert_crt_exists.stat.exists == True
- name: check SSL certificate
letsencrypt:
acme_directory: "{{ ssl_acme_directory }}"
challenge: 'dns-01'
account_key: '/tmp/letsencrypt_account.key'
account_email: "{{ ssl_cert_email }}"
csr: "{{ ssl_cert_csr }}"
dest: "{{ ssl_cert_crt }}"
remaining_days: 10
register: sslcert_challenge
- name: create nsupdate request
template:
src: nsupdate.j2
dest: "/tmp/nsupdate_{{ ansible_hostname }}.tmp"
delegate_to: 127.0.0.1
when: sslcert_challenge|changed
- name: add letsencrypt challenge DNS record
command: "nsupdate -k {{ ssl_nsupdate_key}} /tmp/nsupdate_{{ ansible_hostname }}.tmp"
delegate_to: 127.0.0.1
when: sslcert_challenge|changed
register: sslcert_challenge_replied
- name: reply to letsencrypt challenge
letsencrypt:
acme_directory: "{{ ssl_acme_directory }}"
challenge: 'dns-01'
account_key: '/tmp/letsencrypt_account.key'
csr: "{{ ssl_cert_csr }}"
dest: "{{ ssl_cert_crt }}"
data: "{{ sslcert_challenge }}"
when: sslcert_challenge_replied
register: sslcert_updated
As you can see, I declare variables used by my sslcert role in the playbook, then I call the role, and everything run smoothly.
But now I would like to use my role to generale more than once per server : for a given server, I have to generate *many* certificates because it run TLS/SMTP, and IMAP over SSL, and SASL LDAP, and I want one cert per service !