Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bennojoy committed Dec 18, 2013
0 parents commit b6ba712
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 0 deletions.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
nginx
========

This role enables user to install and configure nginx, The user can specify any http paramters they wish to apply thier site.
Also any number of sites can be added with configurations of your choice.

Requirements
------------

This role requires ansible 1.4 or higher and platform requirements are listed in the metadata file.

Role Variables
--------------

The variables that can be passed to this role and a brief description about them are as follows.

```
nginx_max_clients: 512 # The max clients allowed
nginx_http_params: # A hash of the http paramters. note that any valid http paramters can be
sendfile: "on" # added here.
tcp_nopush: "on"
tcp_nodelay: "on"
keepalive_timeout: "65"
access_log: "/var/log/nginx/access.log"
error_log: "/var/log/nginx/error.log"
nginx_sites: # A list of hash's that define the servers for nginx, as with http paramters
- server: # any valid server paramters can be added here.
file_name: foo
listen: 8080
server_name: localhost
root: "/tmp/site1"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
- server:
file_name: bar
listen: 9090
server_name: ansible
root: "/tmp/site2"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
```

- Examples of using this role

1) Eg: Install nginx with http directives of choices, but no sites configured.

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


1) Eg: Install nginx with diffrent http directives than previous example, but no sites configured.

```
- hosts: all
roles:
- {role: nginx, nginx_http_params: {tcp_nodelay: "on", error_log: "/var/log/nginx/error.log"}, nginx_sites: none}
Note: Please make sure the http directive's passed are valid as this role wont check for the validity of the directives.
```

3) eg: 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: [server: {file_name: bar, listen: 8080, location1: {name: /, try_files: "$uri $uri/ /index.html"}, location2: {name: /images/, try_files: "$uri $uri/ /index.html"}}]}
```
Note: Each site added is represented by list of hashes and the configurations generated are populated in /etc/nginx/conf.d/
The file name for the specific site configurtaion is specified in the hash with the key "file_name", any valid server directives
can be added to hash. For location directive add the key "location" suffixed by a unique number, the value for the location is hash, please make sure they are valid location directives.


4) Eg: Install Nginx and add 2 sites (diffrent method)

```
---
- hosts: all
roles:
- role: nginx
nginx_http_params: {sendfile: "on", access_log: "/var/log/nginx/access.log"}
nginx_sites:
- server:
file_name: foo
listen: 8080
server_name: localhost
root: "/tmp/site1"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
- server:
file_name: bar
listen: 9090
server_name: ansible
root: "/tmp/site2"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
```

Dependencies
------------

None

License
-------

BSD

Author Information
------------------

Benno Joy


27 changes: 27 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---

nginx_max_clients: 512

nginx_http_params:
sendfile: "on"
tcp_nopush: "on"
tcp_nodelay: "on"
keepalive_timeout: "65"
access_log: "/var/log/nginx/access.log"
error_log: "/var/log/nginx/error.log"

nginx_sites:
- server:
file_name: foo
listen: 8080
server_name: localhost
root: "/tmp/site1"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
- server:
file_name: bar
listen: 9090
server_name: ansible
root: "/tmp/site2"
location1: {name: /, try_files: "$uri $uri/ /index.html"}
location2: {name: /images/, try_files: "$uri $uri/ /index.html"}
26 changes: 26 additions & 0 deletions files/epel.repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch/debug
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/6/SRPMS
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=1
6 changes: 6 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: restart nginx
service: name=nginx state=restarted

- name: reload nginx
service: name=nginx state=reloaded
26 changes: 26 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
galaxy_info:
author: "Benno Joy"
company: AnsibleWorks
license: license (BSD)
min_ansible_version: 1.4 (or higher)
platforms:
- name: EL
versions:
- 5
- 6
- name: Fedora
versions:
- 16
- 17
- 18
- name: Ubuntu
versions:
- precise
- quantal
- raring
- saucy
categories:
- web:nginx
dependencies: []

52 changes: 52 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---

- name: Install the selinux python module
yum: name=libselinux-python state=present
when: ansible_os_family == "RedHat"

- name: Copy the epel packages
copy: src=epel.repo dest=/etc/yum.repos.d/epel_ansible.repo
when: ansible_os_family == "RedHat"

- name: Install the nginx packages
yum: name={{ item }} state=present
with_items: redhat_pkg
when: ansible_os_family == "RedHat"

- name: Install the nginx packages
apt: name={{ item }} state=present update_cache=yes
with_items: ubuntu_pkg
environment: env
when: ansible_os_family == "Debian"

- name: Create the directories for site specific configurations
file: path=/etc/nginx/{{ item }} state=directory owner=root group=root mode=0755
with_items:
- "sites-available"
- "sites-enabled"

- name: Copy the nginx configuration file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx

- name: Copy the nginx default configuration file
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf

- name: Copy the nginx default site configuration file
template: src=default.j2 dest=/etc/nginx/sites-available/default


- name: Create the link for site enabled specific configurations
file: path=/etc/nginx/sites-enabled/default state=link src=/etc/nginx/sites-available/default

- name: Create the configurations for sites
template: src=site.j2 dest=/etc/nginx/conf.d/{{ item['server']['file_name'] }}.conf
with_items: nginx_sites
when: nginx_sites|lower != 'none'
notify:
- reload nginx

- name: start the nginx service
service: name=nginx state=started enabled=yes

1 change: 1 addition & 0 deletions templates/default.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#{{ ansible_managed }}
1 change: 1 addition & 0 deletions templates/default.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#{{ ansible_managed }}
31 changes: 31 additions & 0 deletions templates/nginx.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#{{ ansible_managed }}
{% if ansible_os_family == 'RedHat' %}
user nginx;
{% endif %}
{% if ansible_os_family == 'Debian' %}
user www-data;
{% endif %}

worker_processes {{ ansible_processor_count }};
pid /var/run/nginx.pid;


events {
worker_connections {{ nginx_max_clients }};
}


http {

include /etc/nginx/mime.types;
default_type application/octet-stream;
{% for k,v in nginx_http_params.iteritems() %}
{{ k }} {{ v }};
{% endfor %}

gzip on;
gzip_disable "msie6";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
17 changes: 17 additions & 0 deletions templates/site.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {

{% for k,v in item.server.iteritems() %}
{% if k.find('location') == -1 and k != 'file_name' %}
{{ k }} {{ v }};
{% endif %}
{% endfor %}

{% for k,v in item.server.iteritems() if k.find('location') != -1 %}
location {{ v.name }} {
{% for x,y in v.iteritems() if x != 'name' %}
{{ x }} {{ y }};
{% endfor %}
}
{% endfor %}
}

13 changes: 13 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---

env:
RUNLEVEL: 1

redhat_pkg:
- nginx

ubuntu_pkg:
- python-selinux
- nginx


0 comments on commit b6ba712

Please sign in to comment.