Skip to content

Commit 927130d

Browse files
committed
Allow users to set 'stream' vhosts via a new nginx_vhosts_stream variable
This because the expected behavior is quite different, and stream vhosts require additional behavior (e.g. on Debian/Ubuntu)
1 parent c38b9f9 commit 927130d

File tree

7 files changed

+95
-41
lines changed

7 files changed

+95
-41
lines changed

README.md

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ An example of a fully-populated nginx_vhosts entry, using a `|` to declare a blo
5454

5555
Please take note of the indentation in the above block. The first line should be a normal 2-space indent. All other lines should be indented normally relative to that line. In the generated file, the entire block will be 4-space indented. This style will ensure the config file is indented correctly.
5656

57+
5758
- listen: "80"
5859
server_name: "example.com www.example.com"
5960
return: "301 https://example.com$request_uri"
@@ -63,6 +64,25 @@ An example of a secondary vhost which will redirect to the one shown above.
6364

6465
*Note: The `filename` defaults to the first domain in `server_name`, if you have two vhosts with the same domain, eg. a redirect, you need to manually set the `filename` so the second one doesn't override the first one*
6566

67+
nginx_vhosts_stream: []
68+
69+
Define stream server entries here. The formatting is comparable with `nginx_vhosts`.
70+
71+
nginx_vhosts_stream:
72+
- listen: "636 ssl"
73+
filename: "ldap.example.com.stream"
74+
state: "present"
75+
vhost_parameters: |
76+
ssl_certificate /etc/letsencrypt/live/ldap.example.com/fullchain.pem;
77+
ssl_certificate_key /etc/letsencrypt/live/ldap.example.com/privkey.pem;
78+
79+
proxy_pass ldap-vm1.internal:389;
80+
81+
An example of a fully-populated nginx_vhosts_stream entry. The formatting is comparable with `nginx_vhosts`.
82+
**NOTE**: Ensure that the stream module is loaded. Enabling this differs per distibution, but should look like
83+
`load_module modules/ngx_stream_module.so;` (defined via e.g. `nginx_extra_conf_options`). On some distributions
84+
(e.g. RedHat based ones), the stream module is enabled automatically.
85+
6686
nginx_remove_default_vhost: false
6787

6888
Whether to remove the 'default' virtualhost configuration supplied by Nginx. Useful if you want the base `/` URL to be directed at one of your own virtual hosts configured in a separate .conf file.
@@ -231,46 +251,6 @@ Create the child template in the path you configured above and extend `geerlingg
231251
{% endblock %}
232252
```
233253
234-
### Example: LDAP stream via Nginx
235-
236-
This example describes how to setup a secure LDAP stream via Nginx.
237-
238-
Create the following file in your playbook directory (e.g. `templates/nginx/vhost-stream.j2`):
239-
240-
```
241-
server {
242-
listen {{ item.listen }};
243-
244-
{% if item.server_name is defined and item.server_name|length > 0 %}
245-
server_name {{ item.server_name }};
246-
{% endif %}
247-
248-
{% if item.vhost_parameters is defined %}
249-
{{ item.vhost_parameters|indent(8) }}
250-
{% endif %}
251-
252-
}
253-
```
254-
255-
Then, in your Ansible vars, specify e.g. the following configuration:
256-
257-
```yaml
258-
nginx_vhosts:
259-
# Configuration for other hosts
260-
# <snip>
261-
262-
# LDAPS
263-
- listen: "636 ssl"
264-
server_name: "ldap.example.com"
265-
template: "{{ playbook_dir }}/templates/nginx/vhost-stream.j2"
266-
filename: "ldap.example.com.636.stream"
267-
vhost_parameters: |
268-
ssl_certificate /etc/letsencrypt/live/ldap.example.com/fullchain.pem;
269-
ssl_certificate_key /etc/letsencrypt/live/ldap.example.com/privkey.pem;
270-
271-
proxy_pass ldap-vm1.internal:389;
272-
```
273-
274254
## Dependencies
275255
276256
None.

defaults/main.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Used only for Debian/Ubuntu installation, as the -t option for apt.
33
nginx_default_release: ""
44

5+
# Used only for Debian installation to install the Nginx stream module.
6+
nginx_install_stream_module: true
7+
58
# Used only for Redhat installation, enables source Nginx repo.
69
nginx_yum_repo_enabled: true
710

@@ -20,6 +23,7 @@ nginx_service_enabled: true
2023

2124
nginx_conf_template: "nginx.conf.j2"
2225
nginx_vhost_template: "vhost.j2"
26+
nginx_vhost_stream_template: "vhost-stream.j2"
2327

2428
nginx_worker_processes: >-
2529
"{{ ansible_processor_vcpus | default(ansible_processor_count) }}"
@@ -81,6 +85,17 @@ nginx_vhosts: []
8185
# template: "" # Can be used to override the `nginx_vhost_template` per host.
8286
# state: "absent" # To remove the vhost configuration.
8387

88+
nginx_vhosts_stream: []
89+
# Example vhost below, showing all available options:
90+
# - listen: "80" # default: N/A
91+
# server_name: "example.com" # default: N/A
92+
# filename: "example.com.stream" # Can be used to set the vhost filename.
93+
# vhost_parameters: "" # Must be used to add vhost config blocks (multiline).
94+
#
95+
# # Properties that are only added if defined:
96+
# template: "" # Can be used to override the `nginx_vhost_template` per host.
97+
# state: "absent" # To remove the vhost configuration.
98+
8499
nginx_upstreams: []
85100
# - name: myapp1
86101
# strategy: "ip_hash" # "least_conn", etc.

molecule/default/converge.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@
44

55
vars:
66
nginx_use_ppa: true
7+
8+
# Test HTTP vhost
79
nginx_remove_default_vhost: true
810
nginx_vhosts:
911
- server_name: "test.dev"
1012
root: "/var/www/test"
1113

14+
# Test stream vhost
15+
nginx_vhosts_stream:
16+
- listen: 8080
17+
filename: "stream.test.dev.stream"
18+
vhost_parameters:
19+
proxy_pass 127.0.0.1:80;
20+
1221
pre_tasks:
1322
- name: Update apt cache.
1423
apt: update_cache=yes cache_valid_time=600
1524
when: ansible_os_family == 'Debian'
1625
changed_when: false
1726

27+
- name: Enable Nginx stream module.
28+
set_fact:
29+
nginx_extra_conf_options: |
30+
load_module modules/ngx_stream_module.so;
31+
when: ansible_os_family == 'Debian'
32+
changed_when: false
33+
1834
roles:
1935
- role: geerlingguy.nginx

tasks/setup-Debian.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88
name: "{{ nginx_package_name }}"
99
state: present
1010
default_release: "{{ nginx_default_release }}"
11+
12+
- name: Ensure nginx stream module is installed.
13+
apt:
14+
name: libnginx-mod-stream
15+
state: present
16+
default_release: "{{ nginx_default_release }}"
17+
when: nginx_install_stream_module and nginx_vhosts_stream|length > 0

tasks/vhosts.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@
3737
tags:
3838
- skip_ansible_lint
3939

40+
- name: Add managed vhost stream config files.
41+
template:
42+
src: "{{ item.template|default(nginx_vhost_stream_template) }}"
43+
dest: "{{ nginx_vhost_path }}/{{ item.filename }}"
44+
force: true
45+
owner: root
46+
group: "{{ root_group }}"
47+
mode: 0644
48+
when: item.state|default('present') != 'absent'
49+
with_items: "{{ nginx_vhosts_stream }}"
50+
notify: reload nginx
51+
tags:
52+
- skip_ansible_lint
53+
54+
- name: Remove managed vhost stream config files.
55+
file:
56+
path: "{{ nginx_vhost_path }}/{{ item.filename }}"
57+
state: absent
58+
when: item.state|default('present') == 'absent'
59+
with_items: "{{ nginx_vhosts_stream }}"
60+
notify: reload nginx
61+
tags:
62+
- skip_ansible_lint
63+
4064
- name: Remove legacy vhosts.conf file.
4165
file:
4266
path: "{{ nginx_vhost_path }}/vhosts.conf"

templates/nginx.conf.j2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ http {
7373
{% block http_includes %}
7474
include {{ nginx_conf_path }}/*.conf;
7575
{% if nginx_conf_path != nginx_vhost_path %}
76-
include {{ nginx_vhost_path }}/*;
76+
include {{ nginx_vhost_path }}/*.conf;
7777
{% endif %}
7878
{% endblock %}
7979

8080
{% block http_end %}{% endblock %}
8181
}
8282

83+
{% if nginx_vhosts_stream|length > 0 %}
8384
stream {
8485
include {{ nginx_conf_path }}/*.stream;
86+
{% if nginx_conf_path != nginx_vhost_path %}
87+
include {{ nginx_vhost_path }}/*.stream;
88+
{% endif %}
8589
}
90+
{% endif %}

templates/vhost-stream.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
server {
2+
listen {{ item.listen }};
3+
4+
{% if item.vhost_parameters is defined %}
5+
{{ item.vhost_parameters|indent(4) }}
6+
{% endif %}
7+
}

0 commit comments

Comments
 (0)