ansible-nginx/tasks/ssl/acme.yml

102 lines
3.4 KiB
YAML
Raw Normal View History

2017-12-03 04:22:28 +07:00
---
- name: SET_FACT | Assign default...
2021-09-01 16:58:39 +07:00
ansible.builtin.set_fact:
2017-12-03 04:22:28 +07:00
acme_create: []
- name: STAT | Check if certificates are already installed
2021-09-01 16:58:39 +07:00
ansible.builtin.stat:
path: "{{ item | nginx_cert_path(nginx_ssl_dir) }}"
2019-02-05 03:25:25 +07:00
loop: "{{ nginx_ssl_pairs }}"
2017-12-03 04:22:28 +07:00
when: item.acme is defined and item.acme
register: acme_installed_certs
- name: SET_FACT | Assign var with certificates to create
2021-09-01 16:58:39 +07:00
ansible.builtin.set_fact:
2018-03-23 02:35:51 +07:00
acme_create: "{{ acme_create | default([]) + [ (item.item) ] }}"
2019-02-05 03:25:25 +07:00
loop: "{{ acme_installed_certs.results }}"
when: item.skipped is not defined and (not item.stat.exists or item.stat.size == 0)
2017-12-03 04:22:28 +07:00
- name: BLOCK | Start acme
block:
2021-09-01 16:21:12 +07:00
- name: TEMPLATE | Create fake site
2021-09-01 16:58:39 +07:00
ansible.builtin.template:
2021-09-01 16:21:12 +07:00
src: "etc/nginx/conf.d/FAKESITE.conf.j2"
dest: "{{ nginx_etc_dir }}/conf.d/FAKESITE_{{ item | nginx_site_name }}.conf"
2021-09-01 16:45:44 +07:00
mode: 0644
owner: root
group: root
2021-09-01 16:21:12 +07:00
loop: "{{ acme_create }}"
register: fake_site
- name: FILE | Delete current site if needed
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2021-09-01 16:21:12 +07:00
path: "{{ nginx_etc_dir }}/sites-enabled/{{ item | nginx_site_name }}"
state: absent
loop: "{{ acme_create }}"
when: fake_site.changed
- name: SERVICE | Restart nginx
2021-09-01 16:58:39 +07:00
ansible.builtin.service:
2021-09-01 16:21:12 +07:00
name: nginx
state: restarted
when: fake_site.changed and ansible_virtualization_type != 'docker'
- name: COMMAND | Restart nginx
2021-09-01 16:58:39 +07:00
ansible.builtin.command: service nginx restart
2021-09-01 16:21:12 +07:00
args:
warn: false
when: fake_site.changed and ansible_virtualization_type == 'docker'
- name: COMMAND | Get certificates
2021-09-01 16:58:39 +07:00
ansible.builtin.command: |
2021-09-01 16:21:12 +07:00
{{ nginx_acmesh_bin }}
--home {{ nginx_acmesh_dir }}
--issue{% for s in nginx_sites | nginx_search_by_ssl_name(item.name) | nginx_all_site_names %} -d {{ s }}{% endfor %}
--nginx
{% if nginx_acmesh_test %}--test --log{% endif %}
args:
creates: "{{ nginx_acmesh_dir }}/{{ item | nginx_site_name }}/fullchain.cer"
loop: "{{ acme_create }}"
register: acme_get
failed_when: acme_get.rc != 0 and acme_get.rc != 2
no_log: "{{ not nginx_debug_role }}"
- name: FILE | Create SSL dir per site
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2021-09-01 16:21:12 +07:00
path: "{{ nginx_ssl_dir }}/{{ item | nginx_site_name }}"
loop: "{{ acme_create }}"
- name: COMMAND | Install certificates
2021-09-01 16:58:39 +07:00
ansible.builtin.command: |
2021-09-01 16:21:12 +07:00
{{ nginx_acmesh_bin }}
--home {{ nginx_acmesh_dir }}
--install-cert -d {{ nginx_sites | nginx_search_by_ssl_name(item | nginx_site_name) | nginx_site_name }}
--fullchain-file {{ item | nginx_cert_path(nginx_ssl_dir) }}
--key-file {{ item | nginx_key_path(nginx_ssl_dir) }}
--reloadcmd "service nginx reload"
args:
creates: "{{ item | nginx_cert_path(nginx_ssl_dir) }}"
loop: "{{ nginx_ssl_pairs }}"
when: item.acme is defined and item.acme
notify: restart nginx
rescue:
2021-09-01 16:21:12 +07:00
- name: FAIL | Explicit
2021-09-01 16:58:39 +07:00
ansible.builtin.fail:
2021-09-01 16:21:12 +07:00
msg: "Something is bad... Auto crash!"
always:
2021-09-01 16:21:12 +07:00
- name: FILE | Delete fake sites
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2021-09-01 16:21:12 +07:00
path: "{{ nginx_etc_dir }}/conf.d/FAKESITE_{{ item | nginx_site_name }}.conf"
state: absent
loop: "{{ acme_create }}"
notify: restart nginx
2017-12-03 04:22:28 +07:00
2021-09-01 16:21:12 +07:00
- name: META | Flush handlers
2021-09-01 16:58:39 +07:00
ansible.builtin.meta: flush_handlers