Просмотр исходного кода

Merge pull request #91 from oxyc/separate-configs

Write vhost configurations to separate config files
Jeff Geerling 8 лет назад
Родитель
Сommit
c9046d9f3a
7 измененных файлов с 75 добавлено и 46 удалено
  1. 11 0
      .travis.yml
  2. 2 5
      README.md
  3. 1 1
      defaults/main.yml
  4. 18 7
      tasks/vhosts.yml
  5. 31 0
      templates/vhost.j2
  6. 0 33
      templates/vhosts.j2
  7. 12 0
      tests/test.yml

+ 11 - 0
.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/

+ 2 - 5
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.

+ 1 - 1
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

+ 18 - 7
tasks/vhosts.yml

@@ -12,17 +12,28 @@
     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: 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
-  when: nginx_vhosts|length == 0
   notify: reload nginx

+ 31 - 0
templates/vhost.j2

@@ -0,0 +1,31 @@
+server {
+    listen {{ item.listen | default('80') }};
+
+{% 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 %}
+}

+ 0 - 33
templates/vhosts.j2

@@ -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 %}

+ 12 - 0
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