Skip to content

Commit 3a66e10

Browse files
tallen116gdzien
authored andcommitted
Basic stream template (#92)
* Add Stream template defaults * Add stream default config template * Added stream template variables * Changed Ansible task to use new stream template
1 parent f79e6d0 commit 3a66e10

File tree

9 files changed

+231
-32
lines changed

9 files changed

+231
-32
lines changed

.travis.yml

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ matrix:
88
distribution: centos
99
version: 6
1010
playbook: basic
11+
- env:
12+
distribution: centos
13+
version: 6
14+
playbook: http-template
1115
- env:
1216
distribution: centos
1317
version: 6
@@ -23,7 +27,7 @@ matrix:
2327
- env:
2428
distribution: centos
2529
version: 6
26-
playbook: template
30+
playbook: stream-template
2731
- env:
2832
distribution: centos
2933
version: 6
@@ -32,6 +36,10 @@ matrix:
3236
distribution: centos
3337
version: 7
3438
playbook: basic
39+
- env:
40+
distribution: centos
41+
version: 7
42+
playbook: http-template
3543
- env:
3644
distribution: centos
3745
version: 7
@@ -43,11 +51,11 @@ matrix:
4351
- env:
4452
distribution: centos
4553
version: 7
46-
playbook: template
54+
playbook: stable
4755
- env:
4856
distribution: centos
4957
version: 7
50-
playbook: stable
58+
playbook: stream-template
5159
- env:
5260
distribution: centos
5361
version: 7
@@ -56,6 +64,10 @@ matrix:
5664
distribution: debian
5765
version: jessie
5866
playbook: basic
67+
- env:
68+
distribution: debian
69+
version: jessie
70+
playbook: http-template
5971
- env:
6072
distribution: debian
6173
version: jessie
@@ -67,11 +79,11 @@ matrix:
6779
- env:
6880
distribution: debian
6981
version: jessie
70-
playbook: template
82+
playbook: stable
7183
- env:
7284
distribution: debian
7385
version: jessie
74-
playbook: stable
86+
playbook: stream-template
7587
- env:
7688
distribution: debian
7789
version: jessie
@@ -80,6 +92,10 @@ matrix:
8092
distribution: debian
8193
version: stretch
8294
playbook: basic
95+
- env:
96+
distribution: debian
97+
version: stretch
98+
playbook: http-template
8399
- env:
84100
distribution: debian
85101
version: stretch
@@ -91,11 +107,11 @@ matrix:
91107
- env:
92108
distribution: debian
93109
version: stretch
94-
playbook: template
110+
playbook: stable
95111
- env:
96112
distribution: debian
97113
version: stretch
98-
playbook: stable
114+
playbook: stream-template
99115
- env:
100116
distribution: debian
101117
version: stretch
@@ -104,6 +120,10 @@ matrix:
104120
distribution: ubuntu
105121
version: trusty
106122
playbook: basic
123+
- env:
124+
distribution: ubuntu
125+
version: trusty
126+
playbook: http-template
107127
- env:
108128
distribution: ubuntu
109129
version: trusty
@@ -115,15 +135,19 @@ matrix:
115135
- env:
116136
distribution: ubuntu
117137
version: trusty
118-
playbook: template
138+
playbook: stable
119139
- env:
120140
distribution: ubuntu
121141
version: trusty
122-
playbook: stable
142+
playbook: stream-template
123143
- env:
124144
distribution: ubuntu
125145
version: xenial
126146
playbook: basic
147+
- env:
148+
distribution: ubuntu
149+
version: xenial
150+
playbook: http-template
127151
- env:
128152
distribution: ubuntu
129153
version: xenial
@@ -135,11 +159,11 @@ matrix:
135159
- env:
136160
distribution: ubuntu
137161
version: xenial
138-
playbook: template
162+
playbook: stable
139163
- env:
140164
distribution: ubuntu
141165
version: xenial
142-
playbook: stable
166+
playbook: stream-template
143167
- env:
144168
distribution: ubuntu
145169
version: xenial
@@ -148,6 +172,10 @@ matrix:
148172
distribution: ubuntu
149173
version: artful
150174
playbook: basic
175+
- env:
176+
distribution: ubuntu
177+
version: artful
178+
playbook: http-template
151179
- env:
152180
distribution: ubuntu
153181
version: artful
@@ -163,7 +191,7 @@ matrix:
163191
- env:
164192
distribution: ubuntu
165193
version: artful
166-
playbook: template
194+
playbook: stream-template
167195
- env:
168196
distribution: ubuntu
169197
version: artful
@@ -172,6 +200,10 @@ matrix:
172200
distribution: ubuntu
173201
version: bionic
174202
playbook: basic
203+
- env:
204+
distribution: ubuntu
205+
version: bionic
206+
playbook: http-template
175207
- env:
176208
distribution: ubuntu
177209
version: bionic
@@ -187,7 +219,7 @@ matrix:
187219
- env:
188220
distribution: ubuntu
189221
version: bionic
190-
playbook: template
222+
playbook: stream-template
191223
- env:
192224
distribution: ubuntu
193225
version: bionic

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,37 @@ nginx_http_template:
366366
health_check: max_fails=1 fail_timeout=10s
367367

368368
# Enable creating dynamic templated NGINX stream configuration files.
369+
# Defaults will not produce a valid configuration. Instead they are meant to showcase
370+
# the options available for templating. Each key represents a new configuration file.
369371
nginx_stream_template_enable: false
370-
nginx_stream_template_listen: 12345
372+
nginx_stream_template:
373+
default:
374+
template_file: stream/default.conf.j2
375+
conf_file_name: default.conf
376+
conf_file_location: /etc/nginx/conf.d/stream/
377+
network_streams:
378+
default:
379+
listen_address: localhost
380+
listen_port: 80
381+
udp_enable: false
382+
proxy_pass: backend
383+
proxy_timeout: 3s
384+
proxy_connect_timeout: 1s
385+
proxy_protocol: false
386+
health_check_plus: false
387+
upstreams:
388+
upstream1:
389+
name: backend
390+
lb_method: least_conn
391+
zone_name: backend
392+
zone_size: 64k
393+
sticky_cookie: false
394+
servers:
395+
server1:
396+
address: localhost
397+
port: 8080
398+
weight: 1
399+
health_check: max_fails=1 fail_timeout=10s
371400
```
372401
373402
Dependencies
@@ -405,11 +434,14 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
405434
- hosts: localhost
406435
become: true
407436
roles:
408-
- ansible-role-nginx
437+
- role: nginxinc.nginx
409438
vars:
410439
nginx_http_template_enable: true
411440
nginx_http_template:
412441
default:
442+
template_file: http/default.conf.j2
443+
conf_file_name: default.conf
444+
conf_file_location: /etc/nginx/conf.d/
413445
port: 80
414446
server_name: localhost
415447
error_page: /usr/share/nginx/html
@@ -420,6 +452,8 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
420452
location: /
421453
html_file_location: /usr/share/nginx/html
422454
html_file_name: index.html
455+
autoindex: false
456+
http_demo_conf: false
423457
```
424458

425459
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing the open source version of NGINX as a reverse proxy.
@@ -429,11 +463,18 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
429463
- hosts: localhost
430464
become: true
431465
roles:
432-
- nginxinc.nginx
466+
- role: nginxinc.nginx
433467
vars:
434468
nginx_http_template_enable: true
435469
nginx_http_template:
436470
default:
471+
template_file: http/default.conf.j2
472+
conf_file_name: default.conf
473+
conf_file_location: /etc/nginx/conf.d/
474+
port: 80
475+
server_name: localhost
476+
error_page: /usr/share/nginx/html
477+
autoindex: false
437478
reverse_proxy:
438479
locations:
439480
frontend:

defaults/main.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,34 @@ nginx_http_template:
197197
health_check: max_fails=1 fail_timeout=10s
198198

199199
# Enable creating dynamic templated NGINX stream configuration files.
200+
# Defaults will not produce a valid configuration. Instead they are meant to showcase
201+
# the options available for templating. Each key represents a new configuration file.
200202
nginx_stream_template_enable: false
201-
nginx_stream_template_listen: 12345
203+
nginx_stream_template:
204+
default:
205+
template_file: stream/default.conf.j2
206+
conf_file_name: default.conf
207+
conf_file_location: /etc/nginx/conf.d/stream/
208+
network_streams:
209+
default:
210+
listen_address: localhost
211+
listen_port: 80
212+
udp_enable: false
213+
proxy_pass: backend
214+
proxy_timeout: 3s
215+
proxy_connect_timeout: 1s
216+
proxy_protocol: false
217+
health_check_plus: false
218+
upstreams:
219+
upstream1:
220+
name: backend
221+
lb_method: least_conn
222+
zone_name: backend
223+
zone_size: 64k
224+
sticky_cookie: false
225+
servers:
226+
server1:
227+
address: localhost
228+
port: 8080
229+
weight: 1
230+
health_check: max_fails=1 fail_timeout=10s

tasks/conf/template-config.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939

4040
- name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists"
4141
file:
42-
path: /etc/nginx/conf.d/stream
42+
path: "{{ item.value.conf_file_location }}"
4343
state: directory
44+
with_dict: "{{ nginx_stream_template }}"
4445
when: nginx_stream_template_enable
4546

4647
- name: "(Setup: All NGINX) Dynamically Generate NGINX Stream Configuration Files"
4748
template:
48-
src: "{{ item }}"
49-
dest: /etc/nginx/conf.d/stream/{{ item | basename | regex_replace('\.j2','') }}
49+
src: "{{ item.value.template_file }}"
50+
dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}"
5051
backup: yes
51-
with_fileglob:
52-
- "../templates/stream/*.j2"
52+
with_dict: "{{ nginx_stream_template }}"
5353
when: nginx_stream_template_enable
5454
notify: "(Handler: All OSs) Reload NGINX"

templates/http/default.conf.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ upstream {{ item.value.upstreams[upstream].name }} {
1212
sticky cookie srv_id expires=1h path=/;
1313
{% endif %}
1414
}
15-
1615
{% endfor %}
1716
{% endif %}
1817

templates/stream/default.conf.j2

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# {{ ansible_managed }}
2+
{% if item.value.upstreams is defined %}
3+
{% for upstream in item.value.upstreams %}
4+
upstream {{ item.value.upstreams[upstream].name }} {
5+
{% if item.value.upstreams[upstream].lb_method is defined %}
6+
{{ item.value.upstreams[upstream].lb_method }};
7+
{% endif %}
8+
zone {{ item.value.upstreams[upstream].zone_name }} {{ item.value.upstreams[upstream].zone_size }};
9+
{% for server in item.value.upstreams[upstream].servers %}
10+
server {{ item.value.upstreams[upstream].servers[server].address }}:{{ item.value.upstreams[upstream].servers[server].port }} weight={{ item.value.upstreams[upstream].servers[server].weight|default("1") }} {{ item.value.upstreams[upstream].servers[server].health_check|default("") }};
11+
{% endfor %}
12+
{% if item.value.upstreams[upstream].sticky_cookie is defined %}
13+
{% if item.value.upstreams[upstream].sticky_cookie %}
14+
sticky cookie srv_id expires=1h path=/;
15+
{% endif %}
16+
{% endif %}
17+
}
18+
{% endfor %}
19+
{% endif %}
20+
21+
{% if item.value.network_streams is defined %}
22+
{% for stream in item.value.network_streams %}
23+
server {
24+
{% if item.value.network_streams[stream].listen_address is defined and item.value.network_streams[stream].listen_port is defined %}
25+
{% if item.value.network_streams[stream].udp_enable %}
26+
listen {{ item.value.network_streams[stream].listen_address }}:{{ item.value.network_streams[stream].listen_port }} udp;
27+
{% else %}
28+
listen {{ item.value.network_streams[stream].listen_address }}:{{ item.value.network_streams[stream].listen_port }};
29+
{% endif %}
30+
{% elif item.value.network_streams[stream].listen_port is defined %}
31+
{% if item.value.network_streams[stream].udp_enable %}
32+
listen {{ item.value.network_streams[stream].listen_port }} udp;
33+
{% else %}
34+
listen {{ item.value.network_streams[stream].listen_port }};
35+
{% endif %}
36+
{% endif %}
37+
proxy_pass {{ item.value.network_streams[stream].proxy_pass }};
38+
proxy_timeout {{ item.value.network_streams[stream].proxy_timeout }};
39+
proxy_connect_timeout {{ item.value.network_streams[stream].proxy_connect_timeout }};
40+
{% if item.value.network_streams[stream].proxy_protocol %}
41+
proxy_protocol on;
42+
{% else %}
43+
proxy_protocol off;
44+
{% endif %}
45+
{% if item.value.network_streams[stream].health_check_plus %}
46+
health_check;
47+
{% endif %}
48+
}
49+
{% endfor %}
50+
{% endif %}

templates/stream/stream.conf.j2

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

0 commit comments

Comments
 (0)