From f8a04dd544630f3574f13fb117dbe223027406fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 25 Mar 2017 13:15:04 -0500 Subject: [PATCH 1/6] Write vhost configurations to separate config files --- tasks/vhosts.yml | 19 ++++++++++++------- templates/vhost.j2 | 31 +++++++++++++++++++++++++++++++ templates/vhosts.j2 | 33 --------------------------------- 3 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 templates/vhost.j2 delete mode 100644 templates/vhosts.j2 diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index cf27c14..9c4ef43 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -12,17 +12,22 @@ state: directory notify: reload nginx -- name: Add managed vhost config file (if any vhosts are configured). +- name: Add managed vhost config files. template: - src: vhosts.j2 - dest: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}" + src: vhost.j2 + dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" + force: yes + owner: root + group: root mode: 0644 - when: nginx_vhosts|length > 0 + when: "{{ item.state|default('present') != 'absent' }}" + with_items: "{{ nginx_vhosts }}" notify: reload nginx -- name: Remove managed vhost config file (if no vhosts are configured). +- name: Remove managed vhost config files. file: - path: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}" + path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" state: absent - when: nginx_vhosts|length == 0 + when: "{{ item.state|default('present') == 'absent' }}" + with_items: "{{ nginx_vhosts }}" notify: reload nginx diff --git a/templates/vhost.j2 b/templates/vhost.j2 new file mode 100644 index 0000000..80aa6ad --- /dev/null +++ b/templates/vhost.j2 @@ -0,0 +1,31 @@ +server { + listen {{ item.listen | default('80 default_server') }}; + +{% if item.server_name is defined %} + server_name {{ item.server_name }}; +{% endif %} + +{% if item.root is defined %} + root {{ item.root }}; +{% endif %} + + index {{ item.index | default('index.html index.htm') }}; + +{% if item.error_page is defined %} + error_page {{ item.error_page }}; +{% endif %} +{% if item.access_log is defined %} + access_log {{ item.access_log }}; +{% endif %} +{% if item.error_log is defined %} + error_log {{ item.error_log }} error; +{% endif %} + +{% if item.return is defined %} + return {{ item.return }}; +{% endif %} + +{% if item.extra_parameters is defined %} + {{ item.extra_parameters|indent(4) }} +{% endif %} +} diff --git a/templates/vhosts.j2 b/templates/vhosts.j2 deleted file mode 100644 index e4e8c05..0000000 --- a/templates/vhosts.j2 +++ /dev/null @@ -1,33 +0,0 @@ -{% for vhost in nginx_vhosts %} -server { - listen {{ vhost.listen | default('80 default_server') }}; - -{% if vhost.server_name is defined %} - server_name {{ vhost.server_name }}; -{% endif %} - -{% if vhost.root is defined %} - root {{ vhost.root }}; -{% endif %} - - index {{ vhost.index | default('index.html index.htm') }}; - -{% if vhost.error_page is defined %} - error_page {{ vhost.error_page }}; -{% endif %} -{% if vhost.access_log is defined %} - access_log {{ vhost.access_log }}; -{% endif %} -{% if vhost.error_log is defined %} - error_log {{ vhost.error_log }} error; -{% endif %} - -{% if vhost.return is defined %} - return {{ vhost.return }}; -{% endif %} - -{% if vhost.extra_parameters is defined %} - {{ vhost.extra_parameters|indent(4) }} -{% endif %} -} -{% endfor %} From 3f0442fe637dfa4be903c433a7e7859daf105308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 25 Mar 2017 15:24:34 -0500 Subject: [PATCH 2/6] Set default listen value to 80 rather than 80 default_server --- templates/vhost.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/vhost.j2 b/templates/vhost.j2 index 80aa6ad..35e3de1 100644 --- a/templates/vhost.j2 +++ b/templates/vhost.j2 @@ -1,5 +1,5 @@ server { - listen {{ item.listen | default('80 default_server') }}; + listen {{ item.listen | default('80') }}; {% if item.server_name is defined %} server_name {{ item.server_name }}; From f319f9d1957916f38deeaf54110f4725d8d15a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 25 Mar 2017 15:29:15 -0500 Subject: [PATCH 3/6] Update docs --- README.md | 7 ++----- defaults/main.yml | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c6375cb..d76df5b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Available variables are listed below, along with default values (see `defaults/m nginx_vhosts: [] -A list of vhost definitions (server blocks) for Nginx virtual hosts. If left empty, you will need to supply your own virtual host configuration. See the commented example in `defaults/main.yml` for available server options. If you have a large number of customizations required for your server definition(s), you're likely better off managing the vhost configuration file yourself, leaving this variable set to `[]`. +A list of vhost definitions (server blocks) for Nginx virtual hosts. Each entry will create a separate config file named by `server_name`. If left empty, you will need to supply your own virtual host configuration. See the commented example in `defaults/main.yml` for available server options. If you have a large number of customizations required for your server definition(s), you're likely better off managing the vhost configuration file yourself, leaving this variable set to `[]`. nginx_vhosts: - listen: "80 default_server" @@ -26,6 +26,7 @@ A list of vhost definitions (server blocks) for Nginx virtual hosts. If left emp error_page: "" access_log: "" error_log: "" + state: "present" extra_parameters: | location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; @@ -43,10 +44,6 @@ Please take note of the indentation in the above block. The first line should be 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. - nginx_vhosts_filename: "vhosts.conf" - -The filename to use to store vhosts configuration. If you run the role multiple times (e.g. include the role with `with_items`), you can change the name for each run, effectively creating a separate vhosts file per vhost configuration. - nginx_upstreams: [] If you are configuring Nginx as a load balancer, you can define one or more upstream sets using this variable. In addition to defining at least one upstream, you would need to configure one of your server blocks to proxy requests through the defined upstream (e.g. `proxy_pass http://myapp1;`). See the commented example in `defaults/main.yml` for more information. diff --git a/defaults/main.yml b/defaults/main.yml index cd139d9..eb0d3b5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -50,7 +50,6 @@ nginx_extra_http_options: "" # proxy_set_header Host $http_host; nginx_remove_default_vhost: false -nginx_vhosts_filename: "vhosts.conf" nginx_vhosts: [] # Example vhost below, showing all available options: # - listen: "80 default_server" # default: "80 default_server" @@ -63,6 +62,7 @@ nginx_vhosts: [] # access_log: "" # error_log: "" # extra_parameters: "" # Can be used to add extra config blocks (multiline). +# state: "absent" # To remove the vhost configuration. nginx_upstreams: [] # - name: myapp1 From b7b4a1cb4b574554b8f2b6abe2a3c1ce569cd9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 25 Mar 2017 14:19:08 -0500 Subject: [PATCH 4/6] Add integration test --- .travis.yml | 11 +++++++++++ tests/test.yml | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/.travis.yml b/.travis.yml index 85eea53..352227a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,10 @@ env: - distro: ubuntu1204 script: + # Configure test script so we can run extra tests after playbook is run. + - export container_id=$(date +%s) + - export cleanup=false + # Download test shim. - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ - chmod +x ${PWD}/tests/test.sh @@ -15,5 +19,12 @@ script: # Run tests. - ${PWD}/tests/test.sh + # Setup test site. + - 'docker exec ${container_id} mkdir -p /var/www/test' + - 'docker exec ${container_id} bash -c "echo Success >| /var/www/test/index.html"' + + # Make sure virtualhost exists. + - 'docker exec --tty ${container_id} env TERM=xterm curl http://test.dev/ | grep "Success"' + notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/tests/test.yml b/tests/test.yml index 13426a9..32b4e09 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -3,6 +3,18 @@ vars: nginx_use_ppa: true + nginx_remove_default_vhost: true + nginx_vhosts: + - server_name: "test.dev" + root: "/var/www/test" + + pre_tasks: + - name: Update apt cache. + apt: update_cache=yes cache_valid_time=86400 + when: ansible_os_family == 'Debian' + + - name: Install dependencies. + package: name=curl roles: - role_under_test From 9cf49ac6c2fdf0b2da99483be848e361b687d7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 21 Apr 2017 07:46:45 -0500 Subject: [PATCH 5/6] when statements should not include jinja2 templating delimiters --- tasks/vhosts.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 9c4ef43..f89b8f2 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -20,7 +20,7 @@ owner: root group: root mode: 0644 - when: "{{ item.state|default('present') != 'absent' }}" + when: item.state|default('present') != 'absent' with_items: "{{ nginx_vhosts }}" notify: reload nginx @@ -28,6 +28,6 @@ file: path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" state: absent - when: "{{ item.state|default('present') == 'absent' }}" + when: item.state|default('present') == 'absent' with_items: "{{ nginx_vhosts }}" notify: reload nginx From 0d931c53c31652eb277f76e37ad57ba40da44abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 22 Apr 2017 10:03:18 -0500 Subject: [PATCH 6/6] Remove legacy vhosts.conf file --- tasks/vhosts.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index f89b8f2..8448503 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -31,3 +31,9 @@ when: item.state|default('present') == 'absent' with_items: "{{ nginx_vhosts }}" notify: reload nginx + +- name: Remove legacy vhosts.conf file. + file: + path: "{{ nginx_vhost_path }}/vhosts.conf" + state: absent + notify: reload nginx