|
| 1 | +--- |
| 2 | +- name: Install prerequisite packages |
| 3 | + become: true |
| 4 | + ansible.builtin.apt: |
| 5 | + pkg: |
| 6 | + - python3-venv |
| 7 | + - libaugeas0 |
| 8 | + update_cache: true |
| 9 | + |
| 10 | +- name: Install certbot |
| 11 | + become: true |
| 12 | + ansible.builtin.pip: |
| 13 | + name: certbot |
| 14 | + virtualenv: /opt/certbot |
| 15 | + virtualenv_command: python3 -m venv |
| 16 | + |
| 17 | +- name: Create a symbolic link for certbot |
| 18 | + become: true |
| 19 | + ansible.builtin.file: |
| 20 | + src: /opt/certbot/bin/certbot |
| 21 | + dest: /usr/bin/certbot |
| 22 | + owner: root |
| 23 | + group: root |
| 24 | + state: link |
| 25 | + |
| 26 | +- name: Stop HAProxy |
| 27 | + become: true |
| 28 | + ansible.builtin.systemd_service: |
| 29 | + state: stopped |
| 30 | + name: haproxy.service |
| 31 | + |
| 32 | +- name: Check if certificate exists |
| 33 | + become: true |
| 34 | + ansible.builtin.stat: |
| 35 | + path: /etc/haproxy/{{ domain }}.crt |
| 36 | + register: certificate_file |
| 37 | + |
| 38 | +- name: Generate the certificate for the first time |
| 39 | + become: true |
| 40 | + ansible.builtin.shell: "certbot certonly --standalone --non-interactive --agree-tos --domains {{ domain }} -m cloud-support@stfc.ac.uk" |
| 41 | + when: not certificate_file.stat.exists |
| 42 | + |
| 43 | +- name: Copy certificate for the first time |
| 44 | + become: true |
| 45 | + ansible.builtin.shell: "cat /etc/letsencrypt/live/{{ domain }}/privkey.pem /etc/letsencrypt/live/{{ domain }}/fullchain.pem > /etc/haproxy/{{ domain }}.crt" |
| 46 | + when: not certificate_file.stat.exists |
| 47 | + |
| 48 | +- name: Create a cron job for the renewal of certificates |
| 49 | + become: true |
| 50 | + become_user: root |
| 51 | + ansible.builtin.cron: |
| 52 | + name: "Renew Let's Encrypt certificates" |
| 53 | + minute: "0" |
| 54 | + hour: "0,12" |
| 55 | + day: "*" |
| 56 | + job: "/opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && sudo certbot renew -q" |
| 57 | + |
| 58 | +- name: Create a cron job for the upgrade of certbot |
| 59 | + become: true |
| 60 | + become_user: root |
| 61 | + ansible.builtin.cron: |
| 62 | + name: "Upgrade Certbot" |
| 63 | + month: "*" |
| 64 | + job: "sudo /opt/certbot/bin/pip install --upgrade certbot" |
| 65 | + |
| 66 | +- name: Create a cron job to copy certificate to haproxy directory |
| 67 | + become: true |
| 68 | + become_user: root |
| 69 | + ansible.builtin.cron: |
| 70 | + name: "Copy certificate" |
| 71 | + minute: "1" |
| 72 | + hour: "0,12" |
| 73 | + day: "*" |
| 74 | + job: "cat /etc/letsencrypt/live/{{ domain }}/privkey.pem /etc/letsencrypt/live/{{ domain }}/fullchain.pem > /etc/haproxy/{{ domain }}.crt" |
| 75 | + |
| 76 | +- name: Create a cron job to restart HAProxy to pick up new certificate |
| 77 | + become: true |
| 78 | + become_user: root |
| 79 | + ansible.builtin.cron: |
| 80 | + name: "Restart HAProxy to pick up new certificate" |
| 81 | + minute: "3" |
| 82 | + hour: "0,12" |
| 83 | + day: "*" |
| 84 | + job: "systemctl restart haproxy.service" |
| 85 | + |
| 86 | +- name: Restart HAProxy |
| 87 | + become: true |
| 88 | + ansible.builtin.systemd_service: |
| 89 | + state: restarted |
| 90 | + name: haproxy.service |
| 91 | + |
| 92 | + |
| 93 | + |
0 commit comments