Skip to content

Commit

Permalink
Generalized fragment templates to location templates. (#5)
Browse files Browse the repository at this point in the history
* Generalized fragment templates to location templates.

* Skip task if variable is not defined.

* Fixed template overwriting common_config
  • Loading branch information
aleeriz authored Nov 14, 2017
1 parent 1a34bce commit fcb0157
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 80 deletions.
35 changes: 18 additions & 17 deletions tasks/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
with_items:
- "sites-available"
- "sites-enabled"
- "fragments"
- "upstreams.d"
- "include.d"

- name: common | Copy the nginx default configuration file
template:
Expand All @@ -37,6 +38,22 @@
notify:
- check reload nginx

- name: common | Create the new locations
template:
src=locations.j2
dest="{{ item.nginx_location_file }}"
with_items: "{{ locations_upstreams_conf | default([]) }}"
notify:
- check reload nginx

- name: common | Create the new upstreams
template:
src=upstreams.j2
dest="{{ item.nginx_upstream_file }}"
with_items: "{{ locations_upstreams_conf | default([]) }}"
notify:
- check reload nginx

## TODO: Add condition to disable/enable sites
- name: Create the link for site enabled specific configurations
file:
Expand All @@ -48,22 +65,6 @@
notify:
- check reload nginx

- name: common | Create the configuration files for fragments
template:
src=fragments.j2
dest="{{ nginx_etc }}/fragments/fragments.conf"
when: fragments is defined and fragments['list'] is defined
notify:
- check reload nginx

- name: common | Create the configuration files for fragment-upstreams
template:
src=fragment_upstreams.j2
dest="{{ nginx_etc }}/fragments/fragment.upstreams.conf"
when: fragments is defined and fragments['list'] is defined
notify:
- check reload nginx

- name: common | Copy the nginx main configuration file
template:
src=nginx.conf.j2
Expand Down
14 changes: 0 additions & 14 deletions templates/fragment_upstreams.j2

This file was deleted.

25 changes: 0 additions & 25 deletions templates/fragments.j2

This file was deleted.

27 changes: 27 additions & 0 deletions templates/locations.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##{{ ansible_managed }}
{% for config_block in item['list'] %}
location {{ config_block.path }} {
{% if 'proxy_pass' in config_block %}
proxy_pass http://{{ config_block.proxy_pass }};
{% endif %}

{% if 'common_config' in item %}
{% set combined_config = dict(item['common_config']) %}
{% set _ = combined_config.update(config_block) %}
{% else %}
{% set combined_config = config_block %}
{% endif %}
{% for key, value in combined_config.iteritems() %}
{% if key not in ['proxy_pass', 'path', 'server'] %}
{% if value is iterable and value is not string %}
{% for array_value in value %}
{{ key }} {{ array_value }};
{% endfor %}
{% else %}
{{ key }} {{ value }};
{% endif %}
{% endif %}
{% endfor %}
}

{% endfor %}
4 changes: 1 addition & 3 deletions templates/nginx.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ http {
gzip_disable "msie6";

include {{ nginx_etc }}/conf.d/*.conf;
{% if fragments is defined and 'list' in fragments %}
include {{ nginx_etc }}/fragments/fragment.upstreams.conf;
{% endif %}
include {{ nginx_etc }}/upstreams.d/*.conf;
include {{ nginx_etc }}/sites-enabled/*;
}
23 changes: 23 additions & 0 deletions templates/upstreams.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
##{{ ansible_managed }}
{% if 'create_upstreams' in item and item['create_upstreams'] == 'false' %}
{% else %}
{% set data = {} %}
{% for config_block in item['list'] %}
{% if 'proxy_pass' in config_block and not config_block.proxy_pass in data %}
{% set _ = data.update({config_block.proxy_pass: "1"}) %}
upstream {{ config_block.proxy_pass }} {
{% if 'proxy_pass' in config_block %}
{% if config_block.proxy_pass in consul_services%}
{% if 'local_port' in consul_services[config_block.proxy_pass] %}
server 127.0.0.1:{{ consul_services[config_block.proxy_pass]['local_port'] }};
{% else %}
server 127.0.0.1:{{ consul_services[config_block.proxy_pass]['port'] }};
{% endif %}
{% else %}
server {{ config_block.proxy_pass }};
{% endif %}
{% endif %}
}
{% endif %}
{% endfor %}
{% endif %}
37 changes: 27 additions & 10 deletions test/integration/simple/serverspec/nginx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,51 @@

end

describe 'Nginx fragments' do
describe file('/etc/nginx/fragments/') do
describe 'Nginx locations' do
describe file('/etc/nginx/include.d/') do
it { should be_directory }
end
describe file('/etc/nginx/fragments/fragment.upstreams.conf') do
it { should exist }
it { should contain 'upstream some-upstream {'}
it { should contain 'server localhost'}
end
describe file('/etc/nginx/fragments/fragments.conf') do
describe file('/etc/nginx/include.d/fragment_locations.conf') do
it { should exist }
it { should contain 'location ~* ^/fragment1($|/.*$) {'}
it { should contain 'proxy_pass http://some-upstream;'}
it { should contain 'proxy_set_header X-Forwarded-Proto https'}
it { should contain 'proxy_set_header Proxy \'\''}
end
describe file('/etc/nginx/fragments/fragments.conf') do
describe file('/etc/nginx/include.d/fragment_locations.conf') do
it { should exist }
it { should contain 'location ~* ^/fragment2($|/.*$) {'}
it { should contain 'proxy_pass http://some-upstream;'}
it { should contain 'proxy_set_header Proxy \'XYZZY\''}
end
describe file('/etc/nginx/include.d/other_locations.conf') do
it { should exist }
it { should contain 'location ~* ^/external($|/.*$) {'}
it { should contain 'proxy_pass http://some-upstream;'}
end
end

describe 'Nginx upstreams' do
describe file('/etc/nginx/upstreams.d/') do
it { should be_directory }
end
describe file('/etc/nginx/upstreams.d/fragments_upstreams.conf') do
it { should exist }
it { should contain 'upstream some-upstream {'}
it { should contain 'server 127.0.0.1:1337'}
it { should contain 'upstream example.com:8080 {'}
it { should contain 'server example.com:8080'}
end
describe file('/etc/nginx/upstreams.d/other_upstreams.conf') do
it { should exist }
its(:content) { is_expected.not_to include('upstream some-upstream {') }
its(:content) { is_expected.not_to include('server localhost')}
end
end

describe 'NGinx http content' do

describe command "curl -s -L http://127.0.0.1:1080" do
describe command "curl -s -L http://127.0.0.1:1080" do
its(:exit_status) { should eq 0 }
its(:stdout) { should match "Hello test1" }
end
Expand Down
42 changes: 31 additions & 11 deletions test/integration/simple/simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
become : True
connection : "{{ kitchen_connection | default('local') }}"
vars :
consul_services:
some-upstream:
port: 1337
other-upstream:
local_port: 8080
port: 1338

nginx_site :
- file_name : "test"
blocks :
Expand All @@ -19,23 +26,36 @@
try_files: "$uri $uri/ /index.html",
root: "/nginx/test1/"
}
fragments:
list:
locations_upstreams_conf:
- list:
- path: "~* ^/fragment1($|/.*$)"
proxy_pass: "some-upstream"
server: "localhost"

- path: "~* ^/fragment2($|/.*$)"
proxy_pass: "some-upstream"
server: "localhost"
proxy_pass: "example.com:8080"
proxy_set_header:
- "Proxy 'XYZZY'"

common_config:
# list of common nginx config options for all fragments
- path: "~* ^/fragment3($|/.*$)"
proxy_pass: "some-upstream"

- path: "~* ^/fragment4($|/.*$)"
proxy_pass: "other-upstream"

nginx_location_file: "{{ nginx_etc }}/include.d/fragment_locations.conf"
nginx_upstream_file: "{{ nginx_etc }}/upstreams.d/fragments_upstreams.conf"
common_config:
# list of common nginx config options for all fragments
proxy_set_header:
- "X-Forwarded-Proto https"
- "Proxy ''" #https://httpoxy.org/
- "X-Forwarded-Proto https"
- "Proxy ''" #https://httpoxy.org/
- list:
- path: "~* ^/external($|/.*$)"
proxy_pass: "some-upstream"
nginx_location_file: "{{ nginx_etc }}/include.d/other_locations.conf"
nginx_upstream_file: "{{ nginx_etc }}/upstreams.d/other_upstreams.conf"
create_upstreams: "false"
pre_tasks:
- name: update apt
apt: update_cache=yes cache_valid_time=3600
Expand All @@ -45,15 +65,15 @@
path="/nginx/{{ item }}"
state=directory
mode=0755
with_items:
with_items:
- test1

- name: Create content for test
- name: Create content for test
copy:
content="Hello {{ item }}"
dest="/nginx/{{ item }}/index.html"
mode=0644
with_items:
with_items:
- test1

roles :
Expand Down

0 comments on commit fcb0157

Please sign in to comment.