Skip to content

Commit 53a5c95

Browse files
authored
Merge pull request #67 from nginxinc/(feature)/refactor-templating
Refactor templating
2 parents 44a74aa + 13a8472 commit 53a5c95

17 files changed

+405
-121
lines changed

defaults/main.yml

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# Default is true.
44
nginx_enable: true
55

6+
# Print NGINX configuration file to terminal after executing playbook.
7+
nginx_debug_output: false
8+
69
# Specify which version of NGINX you want to install.
710
# Options are 'opensource' or 'plus'.
811
# Default is 'opensource'.
@@ -78,34 +81,108 @@ nginx_unit_modules: null
7881
# Will enable 'stub_status' in NGINX Open Source and 'status' in NGINX Plus.
7982
# Default is false.
8083
nginx_status_enable: false
84+
nginx_status_port: 8080
8185

8286
# Enable NGINX Plus REST API, write access to the REST API, and NGINX Plus dashboard.
8387
# Requires NGINX Plus.
8488
# Default is false.
8589
nginx_rest_api_enable: false
90+
nginx_rest_api_location: /etc/nginx/conf.d/api.conf
91+
nginx_rest_api_port: 8080
8692
nginx_rest_api_write: false
8793
nginx_rest_api_dashboard: false
8894

8995
# Enable uploading NGINX configuration files to your system.
9096
# Default for uploading files is false.
9197
# Default location of files is the files folder within the NGINX Ansible role.
92-
nginx_main_push_enable: false
93-
nginx_main_push_location: conf/nginx.conf
94-
nginx_http_push_enable: false
95-
nginx_http_push_location: conf/http/*.conf
96-
nginx_stream_push_enable: false
97-
nginx_stream_push_location: conf/stream/*.conf
98-
99-
# Configuration variables to create a templated NGINX configuration.
98+
# Upload the main NGINX configuration file.
99+
nginx_main_upload_enable: false
100+
nginx_main_upload_src: conf/nginx.conf
101+
nginx_main_upload_dest: /etc/nginx
102+
# Upload HTTP NGINX configuration files.
103+
nginx_http_upload_enable: false
104+
nginx_http_upload_src: conf/http/*.conf
105+
nginx_http_upload_dest: /etc/nginx/conf.d
106+
# Upload Stream NGINX configuration files.
107+
nginx_stream_upload_enable: false
108+
nginx_stream_upload_src: conf/stream/*.conf
109+
nginx_stream_upload_dest: /etc/nginx/conf.d
110+
# Upload HTML files.
111+
nginx_html_upload_enable: false
112+
nginx_html_upload_src: www/*
113+
nginx_html_upload_dest: /usr/share/nginx/html
114+
# Upload SSL certificates and keys.
115+
nginx_ssl_upload_enable: false
116+
nginx_ssl_crt_upload_src: ssl/*.crt
117+
nginx_ssl_crt_upload_dest: /etc/ssl/certs/
118+
nginx_ssl_key_upload_src: ssl/*.key
119+
nginx_ssl_key_upload_dest: /etc/ssl/private/
120+
121+
# Enable crating dynamic templated NGINX HTMK demo websites.
122+
nginx_html_demo_template_enable: false
123+
nginx_html_demo_template:
124+
default:
125+
template_file: www/index.html.j2
126+
html_file_name: index.html
127+
html_file_location: /usr/share/nginx/html
128+
app_name: default
129+
130+
# Enable creating dynamic templated NGINX configuration files.
100131
# Defaults are the values found in a fresh NGINX installation.
101132
nginx_main_template_enable: false
102-
nginx_main_template_user: nginx
103-
nginx_main_template_worker_processes: auto
104-
nginx_main_template_error_level: warn
105-
nginx_main_template_worker_connections: 1024
133+
nginx_main_template:
134+
template_file: nginx.conf.j2
135+
conf_file_name: nginx.conf
136+
conf_file_location: /etc/nginx/
137+
user: nginx
138+
worker_processes: auto
139+
error_level: warn
140+
worker_connections: 1024
141+
http_enable: true
142+
http_settings:
143+
keepalive_timeout: 65
144+
cache: false
145+
rate_limit: false
146+
keyval: false
147+
stream_enable: false
148+
149+
# Enable creating dynamic templated NGINX HTTP configuration files.
150+
# Defaults will not produce a valid configuration. Instead they are meant to showcase
151+
# the options available for templating. Each key represents a new configuration file.
152+
# Comment out load_balancer or web_server depending on whether you wish to create a web server
153+
# or load balancer configuration file.
106154
nginx_http_template_enable: false
107-
nginx_http_template_keepalive_timeout: 65
108-
nginx_http_template_listen: 80
109-
nginx_http_template_server_name: localhost
155+
nginx_http_template:
156+
default:
157+
template_file: http/default.conf.j2
158+
conf_file_name: default.conf
159+
conf_file_location: /etc/nginx/conf.d/
160+
port: 8081
161+
server_name: localhost
162+
error_page: /usr/share/nginx/html
163+
ssl:
164+
cert: ssl/default.crt
165+
key: ssl/default.key
166+
web_server:
167+
html_file_location: /usr/share/nginx/html
168+
html_file_name: index.html
169+
http_demo_conf: false
170+
load_balancer:
171+
proxy_pass: backend
172+
health_check_plus: false
173+
upstreams:
174+
upstream1:
175+
name: backend
176+
lb_method: least_conn
177+
zone_name: backend
178+
zone_size: 64k
179+
sticky_cookie: false
180+
servers:
181+
server1:
182+
address: localhost
183+
port: 8081
184+
weight: 1
185+
186+
# Enable creating dynamic templated NGINX stream configuration files.
110187
nginx_stream_template_enable: false
111188
nginx_stream_template_listen: 12345

files/ssl/.gitkeep

Whitespace-only changes.

files/www/.gitkeep

Whitespace-only changes.

tasks/conf/debug-output.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: "(Setup: All OSs) Register NGINX configuration"
3+
command: nginx -T
4+
changed_when: false
5+
register: nginx_configuration
6+
7+
- name: "(Setup: All OSs) Print NGINX configuration"
8+
debug:
9+
var: nginx_configuration.stdout_lines

tasks/conf/push-config.yml

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

tasks/conf/setup-rest-api.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
- name: "(Setup: NGINX Plus) Setup NGINX Plus API"
33
blockinfile:
4-
path: "{{ (nginx_http_template_enable) | ternary('/etc/nginx/conf.d/http/api.conf','/etc/nginx/conf.d/api.conf') }}"
4+
path: "{{ nginx_rest_api_location }}"
55
create: yes
66
block: |
77
server {
8-
listen 8080;
8+
listen {{ nginx_rest_api_port }};
99
location /api {
1010
{% if nginx_rest_api_write %}
1111
api write=on;

tasks/conf/setup-status.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
create: yes
66
block: |
77
server {
8-
listen 127.0.0.1:80;
8+
listen 127.0.0.1:{{ nginx_status_port }};
99
location /nginx_status {
1010
stub_status on;
1111
allow 127.0.0.1;
@@ -21,7 +21,7 @@
2121
create: yes
2222
block: |
2323
server {
24-
listen 127.0.0.1:80;
24+
listen 127.0.0.1:{{ nginx_status_port }};
2525
location /status {
2626
status;
2727
allow 127.0.0.1;

tasks/conf/template-config.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
---
2+
- name: "(Setup: All NGINX) Ensure HTML Directory Exists"
3+
file:
4+
path: "{{ item.value.html_file_location }}"
5+
state: directory
6+
with_dict: "{{ nginx_html_demo_template }}"
7+
when: nginx_html_demo_template_enable
8+
9+
- name: "(Setup: All NGINX) Dynamically Generate HTML Files"
10+
template:
11+
src: "{{ item.value.template_file }}"
12+
dest: "{{ item.value.html_file_location }}/{{ item.value.html_file_name }}"
13+
with_dict: "{{ nginx_html_demo_template }}"
14+
when: nginx_html_demo_template_enable
15+
216
- name: "(Setup: All NGINX) Dynamically Generate NGINX Main Configuration File"
317
template:
4-
src: nginx.conf.j2
5-
dest: /etc/nginx/nginx.conf
18+
src: "{{ nginx_main_template.template_file }}"
19+
dest: "{{ nginx_main_template.conf_file_location }}/{{ nginx_main_template.conf_file_name }}"
620
backup: yes
721
when: nginx_main_template_enable
822
notify: "(Handler: All OSs) Reload NGINX"
923

1024
- name: "(Setup: All NGINX) Ensure NGINX HTTP Directory Exists"
1125
file:
12-
path: /etc/nginx/conf.d/http
26+
path: "{{ item.value.conf_file_location }}"
1327
state: directory
28+
with_dict: "{{ nginx_http_template }}"
1429
when: nginx_http_template_enable
1530

1631
- name: "(Setup: All NGINX) Dynamically Generate NGINX HTTP Configuration Files"
1732
template:
18-
src: "{{ item }}"
19-
dest: /etc/nginx/conf.d/http/{{ item | basename | regex_replace('\.j2','') }}
33+
src: "{{ item.value.template_file }}"
34+
dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}"
2035
backup: yes
21-
with_fileglob:
22-
- "../templates/http/*.j2"
36+
with_dict: "{{ nginx_http_template }}"
2337
when: nginx_http_template_enable
2438
notify: "(Handler: All OSs) Reload NGINX"
2539

tasks/conf/upload-config.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
- name: "(Setup: All NGINX) Upload NGINX Main Configuration File"
3+
copy:
4+
src: "{{ nginx_main_upload_src }}"
5+
dest: "{{ nginx_main_upload_dest }}"
6+
backup: yes
7+
notify: "(Handler: All OSs) Reload NGINX"
8+
when: nginx_main_upload_enable
9+
10+
- name: "(Setup: All NGINX) Ensure NGINX HTTP Directory Exists"
11+
file:
12+
path: "{{ nginx_http_upload_dest }}"
13+
state: directory
14+
when: nginx_http_upload_enable
15+
16+
- name: "(Setup: All NGINX) Upload NGINX HTTP Configuration Files"
17+
copy:
18+
src: "{{ item }}"
19+
dest: "{{ nginx_http_upload_dest }}"
20+
backup: yes
21+
with_fileglob: "{{ nginx_http_upload_src }}"
22+
notify: "(Handler: All OSs) Reload NGINX"
23+
when: nginx_http_upload_enable
24+
25+
- name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists"
26+
file:
27+
path: "{{ nginx_stream_upload_dest }}"
28+
state: directory
29+
when: nginx_stream_upload_enable
30+
31+
- name: "(Setup: All NGINX) Upload NGINX Stream Configuration Files"
32+
copy:
33+
src: "{{ item }}"
34+
dest: "{{ nginx_stream_upload_dest }}"
35+
backup: yes
36+
with_fileglob: "{{ nginx_stream_upload_src }}"
37+
notify: "(Handler: All OSs) Reload NGINX"
38+
when: nginx_stream_upload_enable
39+
40+
- name: "(Setup: All NGINX) Ensure NGINX HTML Directory Exists"
41+
file:
42+
path: "{{ nginx_html_upload_dest }}"
43+
state: directory
44+
when: nginx_html_upload_enable
45+
46+
- name: "(Setup: All NGINX) Upload NGINX HTML Files"
47+
copy:
48+
src: "{{ item }}"
49+
dest: "{{ nginx_html_upload_dest }}"
50+
backup: yes
51+
with_fileglob: "{{ nginx_html_upload_src }}"
52+
notify: "(Handler: All OSs) Reload NGINX"
53+
when: nginx_html_upload_enable
54+
55+
- name: "(Setup: All NGINX) Ensure SSL Certificate Directory Exists"
56+
file:
57+
path: "{{ nginx_ssl_crt_upload_dest }}"
58+
state: directory
59+
when: nginx_ssl_upload_enable
60+
61+
- name: "(Setup: All NGINX) Ensure SSL Key Directory Exists"
62+
file:
63+
path: "{{ nginx_ssl_key_upload_dest }}"
64+
state: directory
65+
when: nginx_ssl_upload_enable
66+
67+
- name: "(Setup: All NGINX) Upload NGINX SSL Certificates"
68+
copy:
69+
src: "{{ item }}"
70+
dest: "{{ nginx_ssl_crt_upload_dest }}"
71+
backup: yes
72+
with_fileglob: "{{ nginx_ssl_crt_upload_src }}"
73+
when: nginx_ssl_upload_enable
74+
75+
- name: "(Setup: All NGINX) Upload NGINX SSL Keys"
76+
copy:
77+
src: "{{ item }}"
78+
dest: "{{ nginx_ssl_key_upload_dest }}"
79+
backup: yes
80+
with_fileglob: "{{ nginx_ssl_key_upload_src }}"
81+
when: nginx_ssl_upload_enable

tasks/main.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
- import_tasks: modules/install-modules.yml
2020
when: true in nginx_modules.values()
2121

22-
- import_tasks: conf/push-config.yml
23-
when: nginx_main_push_enable or nginx_http_push_enable or nginx_stream_push_enable
22+
- import_tasks: conf/upload-config.yml
23+
when: nginx_main_upload_enable or nginx_http_upload_enable or nginx_stream_upload_enable or nginx_html_upload_enable or nginx_ssl_upload_enable
2424

2525
- import_tasks: conf/template-config.yml
2626
when: nginx_main_template_enable or nginx_http_template_enable or nginx_stream_template_enable
@@ -29,7 +29,10 @@
2929
when: nginx_status_enable
3030

3131
- import_tasks: conf/setup-rest-api.yml
32-
when: nginx_rest_api_enable and nginx_type == "plus"
32+
when: nginx_rest_api_enable
33+
34+
- import_tasks: conf/debug-output.yml
35+
when: nginx_debug_output
3336

3437
when: nginx_enable
3538

tasks/plus/setup-license.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
src: "{{ item }}"
1010
dest: /etc/ssl/nginx
1111
with_items:
12-
- "{{ license.certificate }}"
13-
- "{{ license.key }}"
12+
- "{{ nginx_license.certificate }}"
13+
- "{{ nginx_license.key }}"

0 commit comments

Comments
 (0)