Skip to content

Allow to generate custom locations in load_balancer mode #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,18 @@ nginx_http_template:
cert: ssl/default.crt
key: ssl/default.key
web_server:
html_file_location: /usr/share/nginx/html
html_file_name: index.html
locations:
default:
location: /
html_file_location: /usr/share/nginx/html
html_file_name: index.html
http_demo_conf: false
load_balancer:
proxy_pass: backend
health_check_plus: false
locations:
location1:
location: /
proxy_pass: backend
health_check_plus: false
upstreams:
upstream1:
name: backend
Expand Down Expand Up @@ -384,6 +390,79 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role to a
- role: nginxinc.nginx
```

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 simple web server.

```yaml
---
- hosts: localhost
become: true
roles:
- ansible-role-nginx
vars:
nginx_http_template_enable: true
nginx_http_template:
default:
template_file: http/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/
port: 80
server_name: localhost
error_page: /usr/share/nginx/html
web_server:
locations:
default:
location: /
html_file_location: /usr/share/nginx/html
html_file_name: index.html
```

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.

```yaml
---
- hosts: localhost
become: true
roles:
- nginxinc.nginx
vars:
nginx_http_template_enable: true
nginx_http_template:
load_balancer:
locations:
frontend:
location: /
proxy_pass: frontend_servers
backend:
location: /backend
proxy_pass: backend_servers
upstreams:
upstream_1:
name: frontend_servers
lb_method: least_conn
zone_name: frontend
zone_size: 64k
sticky_cookie: false
servers:
frontend_server_1:
address: localhost
port: 80
weight: 1
health_check: max_fails=3 fail_timeout=5s
upstream_2:
name: backend_servers
lb_method: least_conn
zone_name: backend
zone_size: 64k
sticky_cookie: false
servers:
backend_server_1:
address: localhost
port: 8080
weight: 1
health_check: max_fails=3 fail_timeout=5s
```


This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing NGINX Plus.

```yaml
Expand Down
12 changes: 9 additions & 3 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ nginx_http_template:
cert: ssl/default.crt
key: ssl/default.key
web_server:
html_file_location: /usr/share/nginx/html
html_file_name: index.html
locations:
default:
location: /
html_file_location: /usr/share/nginx/html
html_file_name: index.html
http_demo_conf: false
load_balancer:
proxy_pass: backend
locations:
backend:
location: /
proxy_pass: backend
health_check_plus: false
upstreams:
upstream1:
Expand Down
15 changes: 10 additions & 5 deletions templates/http/default.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ server {
{% endif %}
server_name {{ item.value.server_name }};
{% if item.value.load_balancer is defined %}
location / {
proxy_pass http://{{ item.value.load_balancer.proxy_pass }};
{% for location in item.value.load_balancer.locations %}
location {{ item.value.load_balancer.locations[location].location }} {
proxy_pass http://{{ item.value.load_balancer.locations[location].proxy_pass }};
{% if item.value.load_balancer.health_check_plus %}
health_check;
{% endif %}
Expand All @@ -34,12 +35,16 @@ server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

{% endfor %}
{% endif %}
{% if item.value.web_server is defined %}
location / {
root {{ item.value.web_server.html_file_location }};
index {{ item.value.web_server.html_file_name }};
{% for location in item.value.web_server.locations %}
location {{ item.value.web_server.locations[location].location }} {
root {{ item.value.web_server.locations[location].html_file_location }};
index {{ item.value.web_server.locations[location].html_file_name }};
}
{% endfor %}
{% if item.value.web_server.http_demo_conf %}
sub_filter_once off;
sub_filter 'server_hostname' '$hostname';
Expand Down
7 changes: 5 additions & 2 deletions tests/playbooks/nginx-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
server_name: localhost
error_page: /usr/share/nginx/html
web_server:
html_file_location: /usr/share/nginx/html
html_file_name: index.html
locations:
default:
location: /
html_file_location: /usr/share/nginx/html
html_file_name: index.html
http_demo_conf: false