├── ansible.cfg ├── vars ├── main.yml └── RedHat.yml ├── templates ├── nginx.repo.j2 ├── vhosts.conf.j2 └── nginx.conf.j2 ├── handlers └── main.yml ├── files └── test.conf ├── meta └── main.yml ├── tasks ├── setup-RedHat.yml ├── main.yml └── vhosts.yml ├── README.md ├── test.yml └── defaults └── main.yml /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | roles_path = ../ 3 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for nginx 3 | -------------------------------------------------------------------------------- /templates/nginx.repo.j2: -------------------------------------------------------------------------------- 1 | [nginx] 2 | name=nginx repo 3 | baseurl={{ nginx_repo_baseurl }} 4 | gpgcheck=0 5 | enabled=1 6 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for nginx 3 | 4 | - name: restart nginx 5 | service: name=nginx state=restarted 6 | -------------------------------------------------------------------------------- /files/test.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 8000; 3 | server_name localhost; 4 | 5 | location / { 6 | root html; 7 | index index.html index.htm; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /vars/RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for RedHat 3 | 4 | nginx_conf_path: /etc/nginx/conf.d 5 | nginx_vhost_path: /etc/nginx/conf.d 6 | nginx_default_vhost_path: /etc/nginx/conf.d/default.conf 7 | nginx_user: nginx 8 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: z 4 | description: Installs and Configures Nginx. 5 | company: 6 | license: license (BSD, MIT) 7 | min_ansible_version: 1.8 8 | platforms: 9 | - name: EL 10 | versions: 11 | - 6 12 | - 7 13 | categories: 14 | - web 15 | dependencies: [] 16 | -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file RedHat 3 | 4 | - name: Enable nginx repo. 5 | template: 6 | src: nginx.repo.j2 7 | dest: /etc/yum.repos.d/nginx.repo 8 | owner: root 9 | group: root 10 | mode: 0644 11 | when: nginx_repo_enable 12 | 13 | - name: Ensure nginx is installed. 14 | yum: name=nginx state=installed 15 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for nginx 3 | 4 | # Variable setup. 5 | - name: Include OS-specific variables. 6 | include_vars: "{{ ansible_os_family }}.yml" 7 | 8 | # Setup/install tasks. 9 | - include: setup-RedHat.yml 10 | when: ansible_os_family == 'RedHat' 11 | 12 | # Configure tasks. 13 | - name: Copy nginx configuration in place. 14 | template: 15 | src: nginx.conf.j2 16 | dest: /etc/nginx/nginx.conf 17 | owner: root 18 | group: root 19 | mode: 0644 20 | notify: restart nginx 21 | 22 | # Configure vhosts 23 | - include: vhosts.yml 24 | 25 | - name: Ensure nginx is started and enabled to start at boot. 26 | service: name=nginx state=started enabled=yes 27 | -------------------------------------------------------------------------------- /tasks/vhosts.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for nginx vhosts 3 | 4 | - name: Remove default nginx vhost config file (if configured). 5 | file: 6 | path: "{{ nginx_default_vhost_path }}" 7 | state: absent 8 | when: nginx_remove_default_vhost 9 | notify: restart nginx 10 | 11 | - name: Add managed vhost config file (if any vhosts are configured). 12 | template: 13 | src: vhosts.conf.j2 14 | dest: "{{ nginx_vhost_path }}/vhosts.conf" 15 | mode: 0644 16 | when: nginx_vhosts 17 | notify: restart nginx 18 | 19 | - name: Add custom vhost config file (if any vhosts conf files are configured) 20 | copy: 21 | src: "{{ item }}.conf" 22 | dest: "{{ nginx_vhost_path }}/{{ item }}.conf" 23 | mode: 0644 24 | with_items: nginx_vhosts_files 25 | when: nginx_vhosts_files is defined 26 | -------------------------------------------------------------------------------- /templates/vhosts.conf.j2: -------------------------------------------------------------------------------- 1 | {% for vhost in nginx_vhosts %} 2 | #################### 3 | {% if vhost.upstreams is defined %} 4 | {% for upstream in vhost.upstreams %} 5 | upstream {{ upstream.name }} { 6 | {% if upstream.strategy is defined %} 7 | {{ upstream.strategy }}; 8 | {% endif %} 9 | {% for server in upstream.servers %} 10 | server {{ server }}; 11 | {% endfor %} 12 | } 13 | {% endfor %} 14 | {% endif %} 15 | 16 | server { 17 | listen {{ vhost.listen | default('80') }}; 18 | server_name {{ vhost.server_name | default('localhost') }}; 19 | 20 | root {{ vhost.root }}; 21 | index {{ vhost.index | default('index.html index.htm') }}; 22 | 23 | {% if vhost.error_page is defined %} 24 | error_page {{ vhost.error_page }}; 25 | {% endif %} 26 | {% if vhost.access_log is defined %} 27 | access_log {{ vhost.access_log }}; 28 | {% endif %} 29 | {% if vhost.return is defined %} 30 | return {{ vhost.return }}; 31 | {% endif %} 32 | 33 | {% if vhost.extra_parameters is defined %} 34 | {{ vhost.extra_parameters }} 35 | {% endif %} 36 | } 37 | 38 | {% endfor %} 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: nginx 2 | 3 | Installs and Configures Nginx. 4 | 5 | ## Requirements 6 | 7 | None. 8 | 9 | ## Role Variables 10 | 11 | ### `defaults/main.yml` 12 | 13 | * nginx_repo_enable: true 14 | * nginx_repo_baseurl: http://nginx.org/packages/centos/{{ ansible_distribution_major_version }}/$basearch/ 15 | * `nginx_remove_default_vhost: false` 16 | * `nginx_worker_processes: "auto"` 17 | * `nginx_events_worker_connections: "1024"` 18 | * `nginx_events_use: ""` 19 | * `nginx_error_log: "/var/log/nginx/error.log warn"` 20 | * `nginx_access_log: "/var/log/nginx/access.log main"` 21 | * `nginx_sendfile: "on"` 22 | * `nginx_tcp_nopush: "on"` 23 | * `nginx_tcp_nodelay: "on"` 24 | * `nginx_keepalive_timeout: "65"` 25 | * `nginx_types_hash_max_size: "2048"` 26 | * `nginx_client_max_body_size: ""` 27 | * `nginx_proxy_cache_path: ""` 28 | * `nginx_conf_extra_parameters: []` 29 | * `nginx_vhosts: []` 30 | * `nginx_vhosts_files: []` 31 | 32 | ### `vars/RedHat.yml` 33 | vars for RedHat/CentOS 34 | 35 | * `nginx_conf_path: /etc/nginx/conf.d` 36 | * `nginx_vhost_path: /etc/nginx/conf.d` 37 | * `nginx_default_vhost_path: /etc/nginx/conf.d/default.conf` 38 | * `nginx_user: nginx` 39 | 40 | ## Dependencies 41 | 42 | None. 43 | 44 | ## Example Playbook 45 | 46 | - hosts: servers 47 | roles: 48 | - role: nginx 49 | nginx_conf_extra_parameters: 50 | - server_names_hash_bucket_size 64 51 | 52 | ## Author Information 53 | 54 | z 55 | -------------------------------------------------------------------------------- /templates/nginx.conf.j2: -------------------------------------------------------------------------------- 1 | user {{ nginx_user }}; 2 | 3 | error_log {{ nginx_error_log }}; 4 | pid /var/run/nginx.pid; 5 | 6 | worker_processes {{ nginx_worker_processes }}; 7 | 8 | events { 9 | worker_connections {{ nginx_worker_connections }}; 10 | {% if nginx_use %} 11 | use {{ nginx_use }}; 12 | {% endif %} 13 | } 14 | 15 | http { 16 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 17 | '$status $body_bytes_sent "$http_referer" ' 18 | '"$http_user_agent" "$http_x_forwarded_for"'; 19 | 20 | access_log {{ nginx_access_log }}; 21 | 22 | sendfile {{ nginx_sendfile }}; 23 | tcp_nopush {{ nginx_tcp_nopush }}; 24 | tcp_nodelay {{ nginx_tcp_nodelay }}; 25 | keepalive_timeout {{ nginx_keepalive_timeout }}; 26 | types_hash_max_size {{ nginx_types_hash_max_size }}; 27 | 28 | include /etc/nginx/mime.types; 29 | default_type application/octet-stream; 30 | 31 | {% if nginx_client_max_body_size %} 32 | client_max_body_size {{ nginx_client_max_body_size }}; 33 | {% endif %} 34 | 35 | #gzip on; 36 | 37 | {% if nginx_proxy_cache_path %} 38 | proxy_cache_path {{ nginx_proxy_cache_path }}; 39 | {% endif %} 40 | 41 | {% if nginx_conf_extra_parameters %} 42 | {% for parameters in nginx_conf_extra_parameters %} 43 | {{ parameters }}; 44 | {% endfor %} 45 | {% endif %} 46 | 47 | include {{ nginx_conf_path }}/*.conf; 48 | {% if nginx_conf_path != nginx_vhost_path %} 49 | include {{ nginx_vhost_path }}/*.conf; 50 | {% endif %} 51 | } 52 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # test file 3 | 4 | - name: Install Nginx. 5 | hosts: servers 6 | roles: 7 | - role: ansible-role-nginx 8 | nginx_conf_extra_parameters: 9 | - server_names_hash_bucket_size 64 10 | nginx_vhosts: 11 | - listen: "8081" 12 | server_name: "localhost" 13 | root: "/var/www/" 14 | upstreams: 15 | - name: test01 16 | strategy: "ip_hash" 17 | servers: 18 | - srv1.test.com 19 | - srv2.test.com 20 | extra_parameters: | 21 | location / { 22 | proxy_pass_header Server; 23 | proxy_set_header Host $http_host; 24 | proxy_redirect off; 25 | proxy_set_header X-Real-IP $remote_addr; 26 | proxy_set_header X-Scheme $scheme; 27 | proxy_pass http://test01; 28 | } 29 | - listen: "8082" 30 | server_name: "localhost" 31 | root: "/var/www/" 32 | upstreams: 33 | - name: test02 34 | strategy: "ip_hash" 35 | servers: 36 | - srv3.test.com 37 | - srv4.test.com 38 | extra_parameters: | 39 | location / { 40 | proxy_pass_header Server; 41 | proxy_set_header Host $http_host; 42 | proxy_redirect off; 43 | proxy_set_header X-Real-IP $remote_addr; 44 | proxy_set_header X-Scheme $scheme; 45 | proxy_pass http://test02; 46 | } 47 | 48 | nginx_vhosts_files: 49 | - test 50 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file nginx 3 | 4 | nginx_repo_enable: true 5 | nginx_repo_baseurl: http://nginx.org/packages/centos/{{ ansible_distribution_major_version }}/$basearch/ 6 | nginx_remove_default_vhost: false 7 | 8 | nginx_worker_processes: "auto" 9 | nginx_worker_connections: "1024" 10 | nginx_use: "" 11 | nginx_error_log: "/var/log/nginx/error.log warn" 12 | nginx_access_log: "/var/log/nginx/access.log main" 13 | nginx_sendfile: "on" 14 | nginx_tcp_nopush: "on" 15 | nginx_tcp_nodelay: "on" 16 | nginx_keepalive_timeout: "65" 17 | nginx_types_hash_max_size: "2048" 18 | nginx_client_max_body_size: "" 19 | nginx_proxy_cache_path: "" 20 | 21 | nginx_conf_extra_parameters: [] 22 | 23 | nginx_vhosts: [] 24 | # Example vhost below, showing all available options: 25 | # - listen: "80 default_server" # default: "80" 26 | # server_name: "example.com" # default: "localhost" 27 | # root: "/var/www/example.com" # default: N/A 28 | # index: "index.html index.htm" # default: "index.html index.htm" 29 | # 30 | # # Properties that are only added if defined: 31 | # error_page: "" 32 | # access_log: "" 33 | # return: "" 34 | # upstreams: 35 | # - name: test 36 | # strategy: "ip_hash" 37 | # servers: 38 | # - srv1.test.com 39 | # - srv2.test.com 40 | # extra_parameters: "" # Can be used to add extra config blocks (multiline). 41 | # location ~ \.php$ { 42 | # root html; 43 | # fastcgi_pass 127.0.0.1:9000; 44 | # fastcgi_index index.php; 45 | # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 46 | # include fastcgi_params; 47 | # } 48 | 49 | nginx_vhosts_files: [] 50 | # To copy custom conf files to server 51 | # Examples 52 | # nginx_vhosts_files: 53 | # - test 54 | --------------------------------------------------------------------------------