Skip to content

Commit feda384

Browse files
alessfggdzien
authored andcommitted
Improve role handling (#128)
* Improve role handling * Add defaults to some tasks * Improve template conditionals
1 parent f3ec78e commit feda384

13 files changed

+113
-104
lines changed

README.md

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ RedHat:
8686
- 6.5+
8787
- 7.4+
8888
- 8
89-
SUSE/SLES:
90-
versions:
91-
- 12
92-
- 15
89+
SUSE/SLES:
90+
versions:
91+
- 12
92+
- 15
9393
Ubuntu:
9494
versions:
9595
- trusty
@@ -115,7 +115,7 @@ Ubuntu:
115115
versions:
116116
- trusty
117117
- xenial
118-
- bionic
118+
- bionic
119119
RedHat:
120120
versions:
121121
- 6
@@ -211,23 +211,33 @@ nginx_install_from: nginx_repository
211211

212212
# Choose where to fetch the NGINX signing key from.
213213
# Default is the official NGINX signing key host.
214-
nginx_signing_key: http://nginx.org/keys/nginx_signing.key
214+
# nginx_signing_key: http://nginx.org/keys/nginx_signing.key
215215

216216
# Specify source repository for NGINX Open Source.
217-
# Only works if 'nginx_install_from' is set to 'nginx_repository'.
217+
# Only works if 'install_from' is set to 'nginx_repository'.
218218
# Defaults are the official NGINX repositories.
219219
nginx_repository:
220+
alpine: >-
221+
https://nginx.org/packages/{{ (nginx_branch == 'mainline')
222+
| ternary('mainline/', '') }}alpine/v{{ ansible_distribution_version | regex_search('^[0-9]+\\.[0-9]+') }}/main
220223
debian:
221-
- deb https://nginx.org/packages/{{ (nginx_branch == 'mainline') | ternary('mainline/', '') }}{{ ansible_distribution|lower }}/ {{ ansible_distribution_release }} nginx
222-
- deb-src https://nginx.org/packages/{{ (nginx_branch == 'mainline') | ternary('mainline/', '') }}{{ ansible_distribution|lower }}/ {{ ansible_distribution_release }} nginx
223-
redhat:
224-
- https://nginx.org/packages/{{ (nginx_branch == 'mainline') | ternary('mainline/', '') }}{{ (ansible_distribution == "RedHat") | ternary('rhel/', 'centos/') }}{{ ansible_distribution_major_version|int }}/$basearch/
225-
suse:
226-
- https://nginx.org/packages/{{ (nginx_branch == 'mainline') | ternary('mainline/', '') }}sles/12
224+
- >-
225+
deb https://nginx.org/packages/{{ (nginx_branch == 'mainline')
226+
| ternary('mainline/', '') }}{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} nginx
227+
- >-
228+
deb-src https://nginx.org/packages/{{ (nginx_branch == 'mainline')
229+
| ternary('mainline/', '') }}{{ ansible_distribution | lower }}/ {{ ansible_distribution_release }} nginx
230+
redhat: >-
231+
https://nginx.org/packages/{{ (nginx_branch == 'mainline')
232+
| ternary('mainline/', '') }}{{ (ansible_distribution == "RedHat")
233+
| ternary('rhel', 'centos') }}/{{ ansible_distribution_major_version }}/$basearch/
234+
suse: >-
235+
https://nginx.org/packages/{{ (nginx_branch == 'mainline')
236+
| ternary('mainline/', '') }}sles/{{ ansible_distribution_major_version }}
227237
228238
# Specify which branch of NGINX Open Source you want to install.
229239
# Options are 'mainline' or 'stable'.
230-
# Only works if 'nginx_install_from' is set to 'nginx_repository'.
240+
# Only works if 'install_from' is set to 'nginx_repository'.
231241
# Default is mainline.
232242
nginx_branch: mainline
233243

@@ -353,10 +363,12 @@ nginx_http_template:
353363
autoindex: false
354364
auth_basic: null
355365
auth_basic_user_file: null
366+
try_files: $uri $uri/index.html $uri.html =404
356367
#auth_request: /auth
357368
ssl:
358369
cert: /etc/ssl/certs/default.crt
359370
key: /etc/ssl/private/default.key
371+
dhparam: /etc/ssl/private/dh_param.pem
360372
protocols: TLSv1 TLSv1.1 TLSv1.2
361373
ciphers: HIGH:!aNULL:!MD5
362374
session_cache: none
@@ -370,7 +382,8 @@ nginx_http_template:
370382
autoindex: false
371383
auth_basic: null
372384
auth_basic_user_file: null
373-
#auth_req: /auth
385+
try_files: $uri $uri/index.html $uri.html =404
386+
#auth_request: /auth
374387
#returns:
375388
#return302:
376389
#code: 302
@@ -432,9 +445,6 @@ nginx_http_template:
432445
cert: /etc/ssl/certs/proxy_default.crt
433446
key: /etc/ssl/private/proxy_default.key
434447
trusted_cert: /etc/ssl/certs/proxy_ca.crt
435-
dhparam: /etc/ssl/private/dh_param.pem
436-
server_name: false
437-
name: server_name
438448
protocols: TLSv1 TLSv1.1 TLSv1.2
439449
ciphers: HIGH:!aNULL:!MD5
440450
verify: false
@@ -452,10 +462,10 @@ nginx_http_template:
452462
proxy_ignore_headers:
453463
- Vary
454464
- Cache-Control
455-
proxy_redirect: false
456465
websocket: false
457466
auth_basic: null
458467
auth_basic_user_file: null
468+
try_files: $uri $uri/index.html $uri.html =404
459469
#auth_req: /auth
460470
#returns:
461471
#return302:
@@ -494,7 +504,7 @@ nginx_status_port: 8080
494504
# Requires NGINX Plus.
495505
# Default is false.
496506
nginx_rest_api_enable: false
497-
nginx_rest_api_src: api.conf.j2
507+
nginx_rest_api_src: http/api.conf.j2
498508
nginx_rest_api_location: /etc/nginx/conf.d/api.conf
499509
nginx_rest_api_port: 8080
500510
nginx_rest_api_write: false
@@ -522,8 +532,6 @@ nginx_stream_template:
522532
cert: /etc/ssl/certs/proxy_default.crt
523533
key: /etc/ssl/private/proxy_default.key
524534
trusted_cert: /etc/ssl/certs/proxy_ca.crt
525-
server_name: false
526-
name: server_name
527535
protocols: TLSv1 TLSv1.1 TLSv1.2
528536
ciphers: HIGH:!aNULL:!MD5
529537
verify: false
@@ -713,6 +721,8 @@ License
713721
Author Information
714722
------------------
715723

716-
Alessandro Fael Garcia
724+
[Alessandro Fael Garcia](https://github.com/alessfg)
725+
726+
[Grzegorz Dzien](https://github.com/gdzien)
717727

718-
[NGINX Inc](https://www.nginx.com/)
728+
© [NGINX, Inc.](https://www.nginx.com/) 2018 - 2019

defaults/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
nginx_enable: true
55

66
# Start NGINX service.
7-
# Default is true
7+
# Default is true.
88
nginx_start: true
99

1010
# Print NGINX configuration file to terminal after executing playbook.
@@ -316,7 +316,7 @@ nginx_status_port: 8080
316316
# Requires NGINX Plus.
317317
# Default is false.
318318
nginx_rest_api_enable: false
319-
nginx_rest_api_src: api.conf.j2
319+
nginx_rest_api_src: http/api.conf.j2
320320
nginx_rest_api_location: /etc/nginx/conf.d/api.conf
321321
nginx_rest_api_port: 8080
322322
nginx_rest_api_write: false

tasks/conf/debug-output.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
- name: "(Setup: All OSs) Print NGINX configuration"
88
debug:
9-
var: nginx_configuration.stdout_lines
9+
var: nginx_configuration.stdout_lines

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:{{ nginx_status_port }};
8+
listen 127.0.0.1:{{ nginx_status_port | default('8080') }};
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:{{ nginx_status_port }};
24+
listen 127.0.0.1:{{ nginx_status_port | default('8080') }};
2525
location /status {
2626
status;
2727
allow 127.0.0.1;

tasks/conf/template-config.yml

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
---
22
- name: "(Setup: All NGINX) Ensure HTML Directory Exists"
33
file:
4-
path: "{{ item.value.html_file_location }}"
4+
path: "{{ item.value.html_file_location | default('/usr/share/nginx/html') }}"
55
state: directory
66
with_dict: "{{ nginx_html_demo_template }}"
77
when: nginx_html_demo_template_enable
88

99
- name: "(Setup: All NGINX) Dynamically Generate HTML Files"
1010
template:
11-
src: "{{ item.value.template_file }}"
12-
dest: "{{ item.value.html_file_location }}/{{ item.value.html_file_name }}"
11+
src: "{{ item.value.template_file | default('www/index.html.j2') }}"
12+
dest: "{{ item.value.html_file_location | default('/usr/share/nginx/html') }}/{{ item.value.html_file_name | default('index.html') }}"
1313
backup: yes
1414
with_dict: "{{ nginx_html_demo_template }}"
1515
when: nginx_html_demo_template_enable
1616

17+
- name: "(Setup: All NGINX) Ensure NGINX Main Directory Exists"
18+
file:
19+
path: "{{ nginx_main_template.conf_file_location | default('/etc/nginx') }}"
20+
state: directory
21+
when: nginx_main_template_enable
22+
1723
- name: "(Setup: All NGINX) Dynamically Generate NGINX Main Configuration File"
1824
template:
19-
src: "{{ nginx_main_template.template_file }}"
20-
dest: "{{ nginx_main_template.conf_file_location }}/{{ nginx_main_template.conf_file_name }}"
25+
src: "{{ nginx_main_template.template_file | default('nginx.conf.j2') }}"
26+
dest: "{{ nginx_main_template.conf_file_location | default('/etc/nginx') }}/{{ nginx_main_template.conf_file_name | default('nginx.conf') }}"
2127
backup: yes
2228
when: nginx_main_template_enable
2329
notify: "(Handler: All OSs) Reload NGINX"
2430

2531
- name: "(Setup: All NGINX) Ensure NGINX HTTP Directory Exists"
2632
file:
27-
path: "{{ item.value.conf_file_location }}"
33+
path: "{{ item.value.conf_file_location | default('/etc/nginx/conf.d/') }}"
2834
state: directory
2935
with_dict: "{{ nginx_http_template }}"
3036
when: nginx_http_template_enable
3137

32-
- name: "(Setup: All NGINX) Ensure NGINX Proxy Cache Directories Exists"
38+
- name: "(Setup: All NGINX) Ensure NGINX Proxy Cache Directories Exist"
3339
file:
3440
path: "{{ item.1.path }}"
3541
state: directory
36-
owner: "{{ nginx_main_template.user }}"
42+
owner: "{{ nginx_main_template.user | default('nginx') }}"
3743
with_subelements:
3844
- "{{ nginx_http_template }}"
3945
- reverse_proxy.proxy_cache_path
@@ -42,33 +48,33 @@
4248

4349
- name: "(Setup: All NGINX) Dynamically Generate NGINX HTTP Configuration Files"
4450
template:
45-
src: "{{ item.value.template_file }}"
46-
dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}"
51+
src: "{{ item.value.template_file | default('http/default.conf.j2') }}"
52+
dest: "{{ item.value.conf_file_location | default('/etc/nginx/conf.d/') }}/{{ item.value.conf_file_name | default('default.conf') }}"
4753
backup: yes
4854
with_dict: "{{ nginx_http_template }}"
4955
when: nginx_http_template_enable
5056
notify: "(Handler: All OSs) Reload NGINX"
5157

58+
- name: "(Setup: All NGINX) Dynamically Generate NGINX API Configuration File"
59+
template:
60+
src: "{{ nginx_rest_api_src | default('http/api.conf.j2') }}"
61+
dest: "{{ nginx_rest_api_location | default('/etc/nginx/conf.d/api.conf') }}"
62+
backup: yes
63+
notify: "(Handler: All OSs) Reload NGINX"
64+
when: nginx_rest_api_enable
65+
5266
- name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists"
5367
file:
54-
path: "{{ item.value.conf_file_location }}"
68+
path: "{{ item.value.conf_file_location | default('/etc/nginx/conf.d/stream/') }}"
5569
state: directory
5670
with_dict: "{{ nginx_stream_template }}"
5771
when: nginx_stream_template_enable
5872

5973
- name: "(Setup: All NGINX) Dynamically Generate NGINX Stream Configuration Files"
6074
template:
61-
src: "{{ item.value.template_file }}"
62-
dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}"
75+
src: "{{ item.value.template_file | default('stream/default.conf.j2') }}"
76+
dest: "{{ item.value.conf_file_location | default('/etc/nginx/conf.d/stream/') }}/{{ item.value.conf_file_name | default('default.conf') }}"
6377
backup: yes
6478
with_dict: "{{ nginx_stream_template }}"
6579
when: nginx_stream_template_enable
6680
notify: "(Handler: All OSs) Reload NGINX"
67-
68-
- name: "(Setup: All NGINX) Dynamically Generate NGINX API Configuration File"
69-
template:
70-
src: "{{ nginx_rest_api_src }}"
71-
dest: "{{ nginx_rest_api_location }}"
72-
backup: yes
73-
notify: "(Handler: All OSs) Reload NGINX"
74-
when: nginx_rest_api_enable

tasks/conf/upload-config.yml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,79 @@
11
---
2+
- name: "(Setup: All NGINX) Ensure NGINX Main Directory Exists"
3+
file:
4+
path: "{{ nginx_main_upload_dest | default('/etc/nginx/') }}"
5+
state: directory
6+
when: nginx_main_upload_enable
7+
28
- name: "(Setup: All NGINX) Upload NGINX Main Configuration File"
39
copy:
4-
src: "{{ nginx_main_upload_src }}"
5-
dest: "{{ nginx_main_upload_dest }}"
10+
src: "{{ nginx_main_upload_src | default('conf/nginx.conf') }}"
11+
dest: "{{ nginx_main_upload_dest | default('/etc/nginx/') }}"
612
backup: yes
713
notify: "(Handler: All OSs) Reload NGINX"
814
when: nginx_main_upload_enable
915

1016
- name: "(Setup: All NGINX) Ensure NGINX HTTP Directory Exists"
1117
file:
12-
path: "{{ nginx_http_upload_dest }}"
18+
path: "{{ nginx_http_upload_dest | default('/etc/nginx/conf.d/') }}"
1319
state: directory
1420
when: nginx_http_upload_enable
1521

1622
- name: "(Setup: All NGINX) Upload NGINX HTTP Configuration Files"
1723
copy:
1824
src: "{{ item }}"
19-
dest: "{{ nginx_http_upload_dest }}"
25+
dest: "{{ nginx_http_upload_dest | default('/etc/nginx/conf.d/') }}"
2026
backup: yes
2127
with_fileglob: "{{ nginx_http_upload_src }}"
2228
notify: "(Handler: All OSs) Reload NGINX"
2329
when: nginx_http_upload_enable
2430

2531
- name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists"
2632
file:
27-
path: "{{ nginx_stream_upload_dest }}"
33+
path: "{{ nginx_stream_upload_dest | default('/etc/nginx/conf.d/') }}"
2834
state: directory
2935
when: nginx_stream_upload_enable
3036

3137
- name: "(Setup: All NGINX) Upload NGINX Stream Configuration Files"
3238
copy:
3339
src: "{{ item }}"
34-
dest: "{{ nginx_stream_upload_dest }}"
40+
dest: "{{ nginx_stream_upload_dest | default('/etc/nginx/conf.d/') }}"
3541
backup: yes
3642
with_fileglob: "{{ nginx_stream_upload_src }}"
3743
notify: "(Handler: All OSs) Reload NGINX"
3844
when: nginx_stream_upload_enable
3945

4046
- name: "(Setup: All NGINX) Ensure NGINX HTML Directory Exists"
4147
file:
42-
path: "{{ nginx_html_upload_dest }}"
48+
path: "{{ nginx_html_upload_dest | default('/usr/share/nginx/html') }}"
4349
state: directory
4450
when: nginx_html_upload_enable
4551

4652
- name: "(Setup: All NGINX) Upload NGINX HTML Files"
4753
copy:
4854
src: "{{ item }}"
49-
dest: "{{ nginx_html_upload_dest }}"
55+
dest: "{{ nginx_html_upload_dest | default('/usr/share/nginx/html') }}"
5056
backup: yes
5157
with_fileglob: "{{ nginx_html_upload_src }}"
5258
notify: "(Handler: All OSs) Reload NGINX"
5359
when: nginx_html_upload_enable
5460

5561
- name: "(Setup: All NGINX) Ensure SSL Certificate Directory Exists"
5662
file:
57-
path: "{{ nginx_ssl_crt_upload_dest }}"
63+
path: "{{ nginx_ssl_crt_upload_dest | default('/etc/ssl/certs/') }}"
5864
state: directory
5965
when: nginx_ssl_upload_enable
6066

6167
- name: "(Setup: All NGINX) Ensure SSL Key Directory Exists"
6268
file:
63-
path: "{{ nginx_ssl_key_upload_dest }}"
69+
path: "{{ nginx_ssl_key_upload_dest | default('/etc/ssl/private/') }}"
6470
state: directory
6571
when: nginx_ssl_upload_enable
6672

6773
- name: "(Setup: All NGINX) Upload NGINX SSL Certificates"
6874
copy:
6975
src: "{{ item }}"
70-
dest: "{{ nginx_ssl_crt_upload_dest }}"
76+
dest: "{{ nginx_ssl_crt_upload_dest | default('/etc/ssl/certs/') }}"
7177
mode: 0640
7278
decrypt: yes
7379
backup: yes
@@ -77,7 +83,7 @@
7783
- name: "(Setup: All NGINX) Upload NGINX SSL Keys"
7884
copy:
7985
src: "{{ item }}"
80-
dest: "{{ nginx_ssl_key_upload_dest }}"
86+
dest: "{{ nginx_ssl_key_upload_dest | default('/etc/ssl/private/') }}"
8187
mode: 0640
8288
decrypt: yes
8389
backup: yes

tasks/controller/setup-debian.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
- name: "(Install: Debian/Ubuntu) Add NGINX Controller Agent Repository"
33
apt_repository:
44
filename: nginx-controller
5-
repo: deb http://packages.nginx.org/controller/{{ ansible_distribution|lower }}/ {{ ansible_distribution_release|lower }} controller
5+
repo: deb http://packages.nginx.org/controller/{{ ansible_distribution | lower }}/ {{ ansible_distribution_release | lower }} controller

tasks/plus/setup-debian.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: "(Install: Debian/Ubuntu) Add NGINX Plus Repository"
33
apt_repository:
4-
repo: deb https://plus-pkgs.nginx.com/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} nginx-plus
4+
repo: deb https://plus-pkgs.nginx.com/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} nginx-plus
55
filename: nginx-plus
66
update_cache: no
77

0 commit comments

Comments
 (0)