feat: add renew hook script

This commit is contained in:
Adrien Waksberg 2018-07-07 19:24:56 +02:00
parent 8256e86007
commit 196240c95f
10 changed files with 91 additions and 11 deletions

View file

@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
Which is based on [Keep A Changelog](http://keepachangelog.com/) Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased] ## [Unreleased]
- add renew hook script
## [1.0.0] - 2018-06-10 ## [1.0.0] - 2018-06-10
- first version - first version

View file

@ -13,11 +13,11 @@ None
- `certbot_mail` - mail address used by let's encrypt to notify - `certbot_mail` - mail address used by let's encrypt to notify
- `certbot_key_size` - private key size (default: `4096`) - `certbot_key_size` - private key size (default: `4096`)
- `certbot_path` - path where certbot write temporary files(default: `/var/www/acme`) - `certbot_path` - path where certbot write temporary files(default: `/var/www/acme`)
- `certbot_domains` - array with the domain name - `certbot_domains` - array with the domain name and command
- `certbot_role` - string must be master or slave, if master generate the certificates
## How to use ## How to use
* Install:
``` ```
- hosts: git-server - hosts: git-server
roles: roles:

View file

@ -1,4 +1,5 @@
certbot_mail: ssl@host.local certbot_mail: ssl@host.local
certbot_key_size: 4096 certbot_key_size: 4096
certbot_path: /var/www/acme certbot_path: /var/www/acme
certbot_role: master
certbot_domains: [] certbot_domains: []

13
files/certbot-renew Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/python3
import configparser
import os
config = configparser.ConfigParser()
config.read('/etc/letsencrypt/renew.cfg')
for domain in os.environ['RENEWED_DOMAINS'].split(' '):
try:
os.system(config.get('default', domain))
except:
continue

View file

@ -1,8 +1,11 @@
- name: install certbot package - name: install certbot package
apt: apt:
name: certbot name: '{{ item }}'
default_release: '{{ certbot_distribution|default(ansible_distribution_release) }}' default_release: '{{ certbot_distribution|default(ansible_distribution_release) }}'
state: present state: present
with_items:
- certbot
- cron
tags: certbot tags: certbot
- name: create webroot path directory - name: create webroot path directory
@ -13,3 +16,31 @@
mode: 0755 mode: 0755
state: directory state: directory
tags: certbot tags: certbot
- name: install certbot-renew binary
copy:
src: certbot-renew
dest: /usr/local/bin/certbot-renew
owner: root
group: root
mode: 0755
tags: certbot
- name: install certbot renew configuration
template:
src: renew.cfg.j2
dest: /etc/letsencrypt/renew.cfg
owner: root
group: root
mode: 0644
tags: certbot
- name: add certbot renew cron
lineinfile:
path: /etc/cron.d/certbot
owner: root
group: root
mode: 0644
regexp: '^0 */12 * * * root'
line: "0 */12 * * * root perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook /usr/local/bin/certbot-renew"
tags: certbot

View file

@ -1,6 +1,6 @@
- name: check if certificate exist - name: check if certificate exist
stat: stat:
path: '/etc/letsencrypt/live/{{ item }}' path: '/etc/letsencrypt/live/{{ item.name }}'
with_items: '{{ certbot_domains }}' with_items: '{{ certbot_domains }}'
register: st register: st
tags: certbot tags: certbot
@ -12,13 +12,13 @@
tags: certbot tags: certbot
- name: create a new certificate - name: create a new certificate
shell: 'certbot certonly -n --agree-tos -d {{ item.item }} -m {{ certbot_mail }} --webroot --webroot-path {{ certbot_path }} --rsa-key-size {{ certbot_key_size }}' shell: 'certbot certonly -n --agree-tos -d {{ item.item.name }} -m {{ certbot_mail }} --webroot --webroot-path {{ certbot_path }} --rsa-key-size {{ certbot_key_size }}'
with_items: '{{ st.results }}' with_items: '{{ st.results }}'
when: (role == 'master' or item.item == ansible_fqdn) and not item.stat.exists and ng.stat.exists when: (certbot_role == 'master' or item.item.name == ansible_fqdn) and not item.stat.exists and ng.stat.exists
tags: certbot tags: certbot
- name: create a new certificate (standalone) - name: create a new certificate (standalone)
shell: 'certbot certonly -n --agree-tos -d {{ item.item }} -m {{ certbot_mail }} --standalone --rsa-key-size {{ certbot_key_size }}' shell: 'certbot certonly -n --agree-tos -d {{ item.item.name }} -m {{ certbot_mail }} --standalone --rsa-key-size {{ certbot_key_size }}'
with_items: '{{ st.results }}' with_items: '{{ st.results }}'
when: (role == 'master' or item.item == ansible_fqdn) and not item.stat.exists and not ng.stat.exists when: (certbot_role == 'master' or item.item.name == ansible_fqdn) and not item.stat.exists and not ng.stat.exists
tags: certbot tags: certbot

4
templates/renew.cfg.j2 Normal file
View file

@ -0,0 +1,4 @@
[default]
{% for domain in certbot_domains%}
{{ domain.name }} = {{ domain.command }}
{% endfor %}

View file

@ -1,6 +1,8 @@
- hosts: gitea - hosts: certbot
connection: local connection: local
vars: vars:
certbot_mail: test@local.com certbot_role: slave
certbot_domains:
- { name: test.local, command: 'echo OK > /tmp/test.txt'}
roles: roles:
- ansible-role-certbot - ansible-role-certbot

View file

@ -13,3 +13,31 @@ describe file('/var/www/acme') do
it { should be_owned_by 'root' } it { should be_owned_by 'root' }
it { should be_grouped_into 'root' } it { should be_grouped_into 'root' }
end end
describe file('/etc/cron.d/certbot') do
it { should exist }
it { should be_file }
it { should be_mode 644 }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
it { should contain '--renew-hook /usr/local/bin/certbot-renew' }
end
describe file('/etc/letsencrypt/renew.cfg') do
it { should exist }
it { should be_file }
it { should be_mode 644 }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
it { should contain 'test.local = echo OK > /tmp/test.txt' }
end
describe command('RENEWED_DOMAINS=test.local /usr/local/bin/certbot-renew') do
its(:exit_status) { should eq 0 }
end
describe file('/tmp/test.txt') do
it { should exist }
it { should be_file }
it { should contain 'OK' }
end

View file

@ -1,2 +1,2 @@
[gitea] [certbot]
localhost localhost