Skip to content

Commit 8d53955

Browse files
committed
♻️ refactor: Refactored the Ansible role to use the new nginx acme-module for automatic certificate management, and updated the Nginx configuration template to support Let's Encrypt.
1 parent 6ea76de commit 8d53955

File tree

8 files changed

+125
-112
lines changed

8 files changed

+125
-112
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ nginx_proxy: |
2424
}
2525
```
2626
27-
### TLS Certificates
27+
### TLS Certificates with ACME
2828
29-
The default configuration provides simple, self-signed certificates if none exist.
30-
Please make sure to replace them with your own certificates.
31-
Simply overwrite the following files:
29+
This role uses the [nginx-acme-module](https://github.com/nginx/nginx-acme) to automatically manage TLS-certificates.
3230
33-
- `/etc/nginx/tls/certificate.key;`
34-
- `/etc/nginx/tls/certificate.crt;`
31+
You can modify the url to the acme issuer in `nginx_acme_issuer_uri`.
32+
If you need to provide multiple server names, you can list them in `nginx_server_names`.
33+
34+
⚠️ You should check it the specified `nginx_resolver` is suitable for you.
3535

3636
### Advanced Configuration
3737

defaults/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ configure_for_firewalld: false
88
configure_for_ufw: false
99
configure_for_selinux: false
1010

11+
nginx_server_names: ["{{ inventory_hostname }}"]
12+
nginx_acme_issuer_uri: "https://acme-v02.api.letsencrypt.org/directory"
13+
# Specify a suitable DNS resolver
14+
nginx_resolver: 1.1.1.1
15+
1116
nginx_proxy: |
1217
location / {
1318
proxy_set_header Host $host;

files/dummy-tls-crt.pem

Lines changed: 0 additions & 33 deletions
This file was deleted.

files/dummy-tls-key.pem

Lines changed: 0 additions & 52 deletions
This file was deleted.

tasks/install_debian.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
- name: Ensure prerequisites
3+
ansible.builtin.apt:
4+
name:
5+
- curl
6+
- gnupg2
7+
- ca-certificates
8+
- lsb-release
9+
- debian-archive-keyring
10+
11+
- name: Create directory for keyrings used by apt
12+
ansible.builtin.file:
13+
path: /etc/apt/keyrings
14+
state: directory
15+
owner: root
16+
group: root
17+
mode: '0755'
18+
19+
- name: Install the nginx package repository key
20+
ansible.builtin.get_url:
21+
url: https://nginx.org/keys/nginx_signing.key
22+
dest: /etc/apt/keyrings/nginx.asc
23+
force: false
24+
owner: root
25+
group: root
26+
mode: '0644'
27+
28+
- name: Add nginx repository
29+
ansible.builtin.apt_repository:
30+
# yamllint disable-line rule:line-length
31+
repo: "deb [signed-by=/etc/apt/keyrings/nginx.asc] http://nginx.org/packages/mainline/debian {{ ansible_distribution_release }} nginx"
32+
filename: nginx
33+
34+
- name: Add nginx repository pinning
35+
ansible.builtin.copy:
36+
dest: /etc/apt/preferences.d/99nginx
37+
content: |
38+
Package: *
39+
Pin: origin nginx.org
40+
Pin: release o=nginx
41+
Pin-Priority: 900
42+
owner: root
43+
group: root
44+
mode: '0644'
45+
46+
- name: Install nginx
47+
ansible.builtin.apt:
48+
name:
49+
- nginx
50+
- nginx-module-acme
51+
update_cache: true

tasks/install_redhat.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
3+
- name: Ensure yum-utils are present
4+
ansible.builtin.dnf:
5+
name: yum-utils
6+
7+
- name: Create nginx repository file
8+
ansible.builtin.copy:
9+
dest: /etc/yum.repos.d/nginx.repo
10+
owner: root
11+
group: root
12+
mode: '0644'
13+
content: |
14+
[nginx-stable]
15+
name=nginx stable repo
16+
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
17+
gpgcheck=1
18+
enabled=1
19+
gpgkey=https://nginx.org/keys/nginx_signing.key
20+
module_hotfixes=true
21+
22+
[nginx-mainline]
23+
name=nginx mainline repo
24+
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
25+
gpgcheck=1
26+
enabled=1
27+
gpgkey=https://nginx.org/keys/nginx_signing.key
28+
module_hotfixes=true
29+
30+
- name: Verify and import nginx GPG key
31+
ansible.builtin.rpm_key:
32+
key: https://nginx.org/keys/nginx_signing.key
33+
fingerprint: 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
34+
35+
- name: Enable nginx-mainline repo
36+
community.general.dnf_config_manager:
37+
name: nginx-mainline
38+
39+
- name: Install nginx and its acme module
40+
ansible.builtin.dnf:
41+
name:
42+
- nginx
43+
- nginx-module-acme

tasks/main.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22

3-
- name: Install nginx
4-
ansible.builtin.package:
5-
name: nginx
6-
state: present
3+
- name: Include OS-specific install tasks
4+
ansible.builtin.include_tasks: "install_{{ ansible_os_family | lower }}.yml"
75

86
- name: Create configuration directories
97
ansible.builtin.file:
@@ -35,19 +33,6 @@
3533
loop: '{{ nginx_config }}'
3634
notify: Reload nginx
3735

38-
- name: Install dummy TLS certificate
39-
ansible.builtin.copy:
40-
src: dummy-tls-{{ item }}.pem
41-
dest: /etc/nginx/tls/certificate.{{ item }}
42-
owner: root
43-
group: root
44-
mode: '0400'
45-
force: false
46-
notify: Reload nginx
47-
loop:
48-
- key
49-
- crt
50-
5136
- name: SELinux settings
5237
when: configure_for_selinux
5338
block:

templates/nginx.conf

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ user www-data;
77
user nginx;
88
{% endif %}
99

10+
# Load the ACME module for automatic certificate management
11+
load_module modules/ngx_http_acme_module.so;
12+
1013
# Defines the number of worker processes. Setting it to the number of
1114
# available CPU cores should be a good start. The value `auto` will try to
1215
# autodetect that.
@@ -33,6 +36,15 @@ events {
3336
}
3437

3538
http {
39+
# ACME configuration
40+
acme_issuer letsencrypt {
41+
uri {{ nginx_acme_issuer_uri }};
42+
state_path /var/cache/nginx/acme-letsencrypt;
43+
accept_terms_of_service;
44+
}
45+
46+
resolver {{ nginx_resolver }} valid=300s;
47+
3648
# Include mime types for different file extensions.
3749
include /etc/nginx/mime.types;
3850

@@ -87,18 +99,20 @@ http {
8799

88100
# Enforce encrypted connections for everything else
89101
location / {
90-
return 301 https://{{ inventory_hostname }}$request_uri;
102+
return 301 https://{{ nginx_server_names | first }}$request_uri;
91103
}
92104
}
93105

94106
server {
95107
listen 443 ssl http2;
96108
listen [::]:443 ssl http2;
97-
server_name _;
109+
server_name {{ nginx_server_names | join(" ") }};
98110

99-
ssl_certificate_key /etc/nginx/tls/certificate.key;
100-
ssl_certificate /etc/nginx/tls/certificate.crt;
111+
acme_certificate letsencrypt;
101112

113+
ssl_certificate $acme_certificate;
114+
ssl_certificate_key $acme_certificate_key;
115+
ssl_certificate_cache max=2;
102116
# Additional TLS related Nginx options
103117
include /etc/nginx/tls/tls.conf;
104118

0 commit comments

Comments
 (0)