xoxys.lego/tasks/main.yml

75 lines
2.2 KiB
YAML
Raw Normal View History

2024-09-27 18:40:51 +00:00
---
- name: Install lego
ansible.legacy.unarchive:
src: https://github.com/go-acme/lego/releases/download/v{{ lego_version }}/lego_v{{ lego_version }}_linux_amd64.tar.gz
dest: "{{ __lego_bin_dir }}"
remote_src: True
extra_opts:
- "{{ __lego_bin_name }}"
mode: "0750"
- name: Create lego base dir
ansible.builtin.file:
path: "{{ __lego_base_dir }}/bin"
state: directory
owner: root
group: root
mode: "0750"
- name: Create LetsEncrypt certificates directory
ansible.builtin.file:
path: "{{ __lego_base_dir }}/.lego/certificates"
state: directory
owner: root
group: root
mode: "0700"
recurse: True
2024-09-27 20:04:16 +00:00
- name: Create hook scripts
ansible.builtin.copy:
content: "{{ item.hook }}"
dest: "{{ __lego_base_dir }}/bin/hook-{{ item.name }}.sh"
owner: root
group: root
mode: "0600"
when: item.hook is defined
loop: "{{ lego_certificates }}"
loop_control:
label: "{{ item.name }}"
2024-09-27 18:40:51 +00:00
- name: Obtain certificates for domains
2024-09-27 19:37:17 +00:00
ansible.builtin.command: >-
2024-09-27 20:04:16 +00:00
{{ __lego_bin_file }}
2024-09-27 19:37:17 +00:00
--email="{{ lego_acme_account_email }}"
--domains {{ " --domains ".join(item.domains) }}
--key-type="{{ lego_key_type }}"
--dns="cloudflare"
2024-09-27 20:04:16 +00:00
run
{{ '--run-hook="{{ __lego_base_dir }}/bin/hook-{{ item.name }}.sh"' if item.hook is defined else '' }}
2024-09-27 18:40:51 +00:00
args:
creates: "{{ __lego_base_dir }}/.lego/certificates/{{ item.domains[0] }}.crt"
environment:
2024-09-27 19:18:56 +00:00
LEGO_SERVER: "{{ lego_acme_server }}/directory"
2024-09-27 18:40:51 +00:00
LEGO_PATH: "{{ __lego_base_dir }}/.lego"
2024-09-27 19:18:56 +00:00
CLOUDFLARE_API_TOKEN: "{{ lego_cloudflare_api_token }}"
when: not item.skip_create | default(False) | bool
2024-09-27 18:40:51 +00:00
loop: "{{ lego_certificates }}"
loop_control:
2024-09-27 20:04:16 +00:00
label: "{{ item.name }}"
2024-09-27 18:40:51 +00:00
- name: Add cron scipt to renew certificates
ansible.builtin.template:
2024-09-27 20:04:16 +00:00
dest: "{{ __lego_base_dir }}/bin/cron-lego-renew.sh"
2024-09-27 18:40:51 +00:00
mode: "0755"
2024-09-27 20:04:16 +00:00
src: cron-lego-renew.sh.j2
2024-09-27 18:40:51 +00:00
- name: Add cron job to renew certificates
ansible.builtin.cron:
name: "lego-renew"
cron_file: "lego-renew"
2024-09-27 20:04:16 +00:00
job: "{{ __lego_base_dir }}/bin/cron-lego-renew.sh >> {{ __lego_base_dir }}/cron_lego_renew.log 2>&1"
2024-09-27 19:37:17 +00:00
hour: "{{ lego_cron_hour }}"
minute: "{{ lego_cron_minute }}"
2024-09-27 18:40:51 +00:00
user: root
2024-09-27 19:37:17 +00:00
state: "{{ 'present' if lego_cron_enabled | bool else 'absent' }}"