Role Detail

jdauphant.nginx

Ansible role to install and manage nginx configuration
Downloads
33242
Type Ansible
Minimum Ansible Version 2.2
Installation $ ansible-galaxy install jdauphant.nginx
Tags
web
Last Commit 2017-10-27 15:57:03 PM UTC
Last Imported 2017-11-02 18:45:06 PM UTC
Version History
Version Release Date
v2.12 2017-10-27 15:57:03 PM
v2.11.1 2017-10-19 20:16:08 PM
v2.11 2017-09-29 09:14:09 AM
v2.10 2017-09-29 09:10:09 AM
v2.9.1 2017-09-11 08:52:08 AM
v2.9 2017-07-28 13:31:01 PM
v2.8.1 2017-06-14 15:24:03 PM
v2.8 2017-05-04 16:25:04 PM
v2.7.7 2017-03-19 22:13:10 PM
v2.7.6 2017-03-17 18:41:06 PM
v2.7.5 2017-03-17 13:43:01 PM
v2.7.4 2017-01-27 13:26:01 PM
v2.7.3 2017-01-27 13:22:01 PM
v2.7.2 2017-01-23 18:31:06 PM
v2.7.1 2017-01-20 16:22:04 PM
v2.7 2017-01-20 10:11:10 AM
v2.6.3 2017-01-19 18:14:06 PM
v2.6.2 2017-01-18 09:41:09 AM
v2.6.1 2017-01-06 13:18:01 PM
v2.6 2016-12-14 18:22:06 PM
v2.5.2 2016-11-23 13:37:01 PM
v2.5.1 2016-11-19 12:52:12 PM
v2.5 2016-11-11 13:19:01 PM
v2.4 2016-11-10 11:12:11 AM
v2.3.1 2016-06-18 09:57:09 AM
v2.3 2016-06-17 09:24:09 AM
v2.2.3 2016-06-03 15:54:03 PM
v2.2.2 2016-06-02 08:06:08 AM
v2.2.1 2016-05-20 15:51:03 PM
v2.2.0 2016-05-04 17:57:05 PM
v2.1.0 2016-03-30 21:41:09 PM
v2.0.2 2016-03-25 11:25:11 AM
v2.0.1 2016-03-15 21:46:09 PM
v2.0 2016-03-10 23:36:11 PM
v1.11.4 2016-03-09 10:09:10 AM
v1.11.3 2016-03-07 22:43:10 PM
v1.11.2 2016-02-29 23:50:11 PM
v1.11.1 2016-02-15 13:36:01 PM
v1.11.0 2016-02-10 14:02:02 PM
v1.10.0 2015-12-07 11:34:11 AM
v1.9.1 2015-11-18 10:53:10 AM
v1.9 2015-10-28 10:12:10 AM
v1.8.2 2015-10-19 16:32:04 PM
v1.8.1 2015-10-16 22:55:10 PM
v1.8.0 2015-10-16 22:33:10 PM
v1.7.1 2015-09-11 21:12:09 PM
v1.7 2015-09-07 21:47:09 PM
v1.6.2 2015-09-02 08:45:08 AM
v1.6.1 2015-09-01 08:29:08 AM
v1.6 2015-09-01 08:22:08 AM
v1.5.1 2015-05-20 15:07:03 PM
v1.5 2015-05-19 22:13:10 PM
v1.4.1 2015-05-18 13:43:01 PM
v1.4 2015-05-17 21:09:09 PM
v1.3.4 2015-04-17 12:07:12 PM
v1.3.3 2015-04-12 21:19:09 PM
v1.3.2 2015-04-12 21:12:09 PM
v1.3.1 2015-03-18 09:28:09 AM
v1.3 2015-03-12 22:46:10 PM
v1.2.1 2015-02-27 15:41:03 PM
v1.2 2015-02-11 10:18:10 AM
v1.1.2 2015-01-13 11:55:11 AM
v1.1.1 2014-12-04 13:58:01 PM
v1.1.0 2014-11-26 10:45:10 AM
v1.0.1 2014-11-04 23:13:11 PM
v1.0.0 2014-10-13 10:00:10 AM
Supported Platforms
Platform Version
Debian etch
Debian jessie
Debian lenny
Debian sid
Debian squeeze
Debian stretch
Debian wheezy
EL 5
EL 6
EL 7
Fedora 16
Fedora 17
Fedora 18
Fedora 19
Fedora 20
Fedora 21
Fedora 22
Fedora 23
Fedora 24
Fedora 25
Fedora 26
FreeBSD 10.0
FreeBSD 10.1
FreeBSD 10.2
FreeBSD 10.3
FreeBSD 11.0
opensuse 12.1
opensuse 12.2
opensuse 12.3
opensuse 13.1
opensuse 13.2
SmartOS any
Ubuntu artful
Ubuntu lucid
Ubuntu maverick
Ubuntu natty
Ubuntu oneiric
Ubuntu precise
Ubuntu quantal
Ubuntu raring
Ubuntu saucy
Ubuntu trusty
Ubuntu utopic
Ubuntu vivid
Ubuntu wily
Ubuntu xenial
Ubuntu yakkety
Ubuntu zesty
Last 10 Imports
Completed Status
2017-11-02 18:45:06 PM UTC FAILED
2017-10-27 16:00:04 PM UTC FAILED
2017-10-27 16:00:04 PM UTC FAILED
2017-10-27 15:13:03 PM UTC FAILED
2017-10-21 00:04:12 AM UTC FAILED
2017-10-20 13:51:01 PM UTC FAILED
2017-10-19 20:20:08 PM UTC FAILED
2017-10-19 20:19:08 PM UTC FAILED
2017-10-16 14:11:02 PM UTC FAILED
2017-10-13 12:51:12 PM UTC FAILED

nginx

This role installs and configures the nginx web server. The user can specify any http configuration parameters they wish to apply their site. Any number of sites can be added with configurations of your choice.

Build Status Ansible Galaxy

Requirements

This role requires Ansible 2.0 or higher and platform requirements are listed in the metadata file. (Some older version of the role support Ansible 1.4) For FreeBSD a working pkgng setup is required (see: https://www.freebsd.org/doc/handbook/pkgng-intro.html )

Install

ansible-galaxy install jdauphant.nginx

Role Variables

The variables that can be passed to this role and a brief description about them are as follows. (For all variables, take a look at defaults/main.yml)

# The user to run nginx
nginx_user: "www-data"

# A list of directives for the events section.
nginx_events_params:
 - worker_connections 512
 - debug_connection 127.0.0.1
 - use epoll
 - multi_accept on

# A list of hashes that define the servers for nginx,
# as with http parameters. Any valid server parameters
# can be defined here.
nginx_sites:
 default:
     - listen 80
     - server_name _
     - root "/usr/share/nginx/html"
     - index index.html
 foo:
     - listen 8080
     - server_name localhost
     - root "/tmp/site1"
     - location / { try_files $uri $uri/ /index.html; }
     - location /images/ { try_files $uri $uri/ /index.html; }
 bar:
     - listen 9090
     - server_name ansible
     - root "/tmp/site2"
     - location / { try_files $uri $uri/ /index.html; }
     - location /images/ {
         try_files $uri $uri/ /index.html;
         allow 127.0.0.1;
         deny all;
       }

# A list of hashes that define additional configuration
nginx_configs:
  proxy:
      - proxy_set_header X-Real-IP  $remote_addr
      - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
  upstream:
      - upstream foo { server 127.0.0.1:8080 weight=10; }
  geo:
      - geo $local {
          default 0;
          127.0.0.1 1;
        }
  gzip:
      - gzip on
      - gzip_disable msie6

# A list of hashes that define configuration snippets
nginx_snippets:
  error_pages:
    - error_page 500 /http_errors/500.html
    - error_page 502 /http_errors/502.html
    - error_page 503 /http_errors/503.html
    - error_page 504 /http_errors/504.html

# A list of hashes that define user/password files
nginx_auth_basic_files:
   demo:
     - foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo , generated by : htpasswd -nb foo demo
     - bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo , generated by : htpasswd -nb bar demo

Examples

1) Install nginx with HTTP directives of choice, but with no sites configured and no additional configuration:

- hosts: all
  roles:
  - {role: nginx,
     nginx_http_params: ["sendfile on", "access_log /var/log/nginx/access.log"]
                          }

2) Install nginx with different HTTP directives than in the previous example, but no

sites configured and no additional configuration.

- hosts: all
  roles:
  - {role: nginx,
     nginx_http_params: ["tcp_nodelay on", "error_log /var/log/nginx/error.log"]}

Note: Please make sure the HTTP directives passed are valid, as this role won't check for the validity of the directives. See the nginx documentation for details.

3) Install nginx and add a site to the configuration.

- hosts: all

  roles:
  - role: nginx
    nginx_http_params:
      - sendfile "on"
      - access_log "/var/log/nginx/access.log"
    nginx_sites:
      bar:
        - listen 8080
        - location / { try_files $uri $uri/ /index.html; }
        - location /images/ { try_files $uri $uri/ /index.html; }
    nginx_configs:
      proxy:
        - proxy_set_header X-Real-IP  $remote_addr
        - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

4) Install nginx and add extra variables to default config

-hosts: all
  vars:
    - my_extra_params:
      - client_max_body_size 200M
# retain defaults and add additional `client_max_body_size` param
  roles:
    - role: jdauphant.nginx
      nginx_http_params: "{{ nginx_http_default_params + my_extra_params }}"

Note: Each site added is represented by a list of hashes, and the configurations generated are populated in /etc/nginx/site-available/ and linked from /etc/nginx/site-enable/ to /etc/nginx/site-available.

The file name for the specific site configuration is specified in the hash with the key "file_name", any valid server directives can be added to the hash. Additional configurations are created in /etc/nginx/conf.d/

5) Install Nginx, add 2 sites (different method) and add additional configuration

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
         foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
         bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
      nginx_configs:
         proxy:
            - proxy_set_header X-Real-IP  $remote_addr
            - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

6) Install Nginx, add 2 sites, add additional configuration and an upstream configuration block

---
- hosts: all
  roles:
    - role: nginx
      nginx_error_log_level: info
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
        foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
        bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - if ( $host = example.com ) { rewrite ^(.*)$ http://www.example.com$1 permanent; }
           - location / {
             try_files $uri $uri/ /index.html;
             auth_basic            "Restricted";
             auth_basic_user_file  auth_basic/demo;
           }
           - location /images/ { try_files $uri $uri/ /index.html; }
      nginx_configs:
        proxy:
            - proxy_set_header X-Real-IP  $remote_addr
            - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
        upstream:
            # Results in:
            # upstream foo_backend {
            #   server 127.0.0.1:8080 weight=10;
            # }
            - upstream foo_backend { server 127.0.0.1:8080 weight=10; }
      nginx_auth_basic_files:
        demo:
           - foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo , generated by : htpasswd -nb foo demo
           - bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo , generated by : htpasswd -nb bar demo

7) Install Nginx, add a site and use special yaml syntax to make the location blocks multiline for clarity

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
        foo:
           - listen 443 ssl
           - server_name foo.example.com
           - set $myhost foo.example.com
           - |
             location / {
               proxy_set_header Host foo.example.com;
             }
           - |
             location ~ /v2/users/.+?/organizations {
               if ($request_method = PUT) {
                 set $myhost bar.example.com;
               }
               if ($request_method = DELETE) {
                 set $myhost bar.example.com;
               }
               proxy_set_header Host $myhost;
             }

8) Example to use this role with my ssl-certs role to generate or copie ssl certificate ( https://galaxy.ansible.com/list#/roles/3115 )

 - hosts: all
   roles:
     - jdauphant.ssl-certs
     - role: jdauphant.nginx
       nginx_configs:
          ssl:
               - ssl_certificate_key {{ssl_certs_privkey_path}}
               - ssl_certificate     {{ssl_certs_cert_path}}
       nginx_sites:
          default:
               - listen 443 ssl
               - server_name _
               - root "/usr/share/nginx/html"
               - index index.html

9) Site configuration using a custom template.

Instead of defining a site config file using a list of attributes, you may use a hash/dictionary that includes the filename of an alternate template. Additional values are accessible within the template via the item.value variable.

- hosts: all

  roles:
  - role: nginx
    nginx_sites:
      custom_bar:
        template: custom_bar.conf.j2
        server_name: custom_bar.example.com

Custom template: custom_bar.conf.j2:

# {{ ansible_managed }}
upstream backend {
  server 10.0.0.101;
}
server {
  server_name {{ item.value.server_name }};
  location / {
    proxy_pass http://backend;
  }
}

Using a custom template allows for unlimited flexibility in configuring the site config file. This example demonstrates the common practice of configuring a site server block in the same file as its complementary upstream block. If you use this option:

  • The hash must include a template: value, or the configuration task will fail.
  • This role cannot check tha validity of your custom template. If you use this method, the conf file formatting provided by this role is unavailable, and it is up to you to provide a template with valid content and formatting for NGINX.

10) Install Nginx, add 2 sites, use snippets to configure access controls

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_snippets:
        accesslist_devel:
          - allow 192.168.0.0/24
          - deny all
      nginx_sites:
        foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - include snippets/accesslist_devel.conf
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
        bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }

Dependencies

None

License

BSD

Author Information

  • Original : Benno Joy
  • Modified by : DAUPHANT Julien
None