mirror of
https://github.com/HanXHX/ansible-nginx.git
synced 2026-02-28 09:22:10 +07:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a612a55b9 | ||
|
|
1280a441ee | ||
|
|
21edb6b584 | ||
|
|
c524b97b0f | ||
|
|
993310641a | ||
|
|
f5885c5c55 | ||
|
|
bb5e00d6f5 | ||
|
|
cf010e4a4b | ||
|
|
39d3f5f06a | ||
|
|
247f849b86 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
*.swp
|
||||
*.retry
|
||||
*.pyc
|
||||
/tests/HanXHX.php
|
||||
|
||||
@@ -26,6 +26,7 @@ before_install:
|
||||
|
||||
install:
|
||||
- pip install "$ANSIBLE_VERSION"
|
||||
- ansible-galaxy install -p ./tests HanXHX.php,master
|
||||
|
||||
script:
|
||||
- VAGRANT_DEFAULT_PROVIDER=docker vagrant up $PLATFORM
|
||||
|
||||
10
README.md
10
README.md
@@ -105,6 +105,16 @@ Dependencies
|
||||
|
||||
None
|
||||
|
||||
|
||||
If you need to dev this role locally
|
||||
------------------------------------
|
||||
|
||||
Before use vagrant, run once:
|
||||
|
||||
```
|
||||
ansible-galaxy install -p ./tests/ HanXHX.php,master
|
||||
```
|
||||
|
||||
Example Playbook
|
||||
----------------
|
||||
|
||||
|
||||
@@ -10,11 +10,29 @@ def nginx_site_name(site):
|
||||
else:
|
||||
return site['name']
|
||||
|
||||
def nginx_ssl_dir(pair, ssl_dir):
|
||||
return ssl_dir + '/' + nginx_site_filename(pair)
|
||||
|
||||
def nginx_key_path(pair, ssl_dir):
|
||||
if pair.has_key('dest_key'):
|
||||
return pair['dest_key']
|
||||
else:
|
||||
return nginx_ssl_dir(pair, ssl_dir) + '/' + nginx_site_filename(pair) + '.key'
|
||||
|
||||
def nginx_cert_path(pair, ssl_dir):
|
||||
if pair.has_key('dest_cert'):
|
||||
return pair['dest_cert']
|
||||
else:
|
||||
return nginx_ssl_dir(pair, ssl_dir) + '/' + nginx_site_filename(pair) + '.crt'
|
||||
|
||||
class FilterModule(object):
|
||||
''' Nginx module '''
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
'nginx_site_filename': nginx_site_filename,
|
||||
'nginx_site_name': nginx_site_name
|
||||
'nginx_site_name': nginx_site_name,
|
||||
'nginx_ssl_dir': nginx_ssl_dir,
|
||||
'nginx_key_path': nginx_key_path,
|
||||
'nginx_cert_path': nginx_cert_path
|
||||
}
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
def php_default_upstream_socket(php_version):
|
||||
if php_version == '5.6':
|
||||
return '/run/php5-fpm.sock'
|
||||
else:
|
||||
return '/run/php/php%s-fpm.sock' % php_version
|
||||
return '/run/php/php%s-fpm.sock' % php_version
|
||||
|
||||
def php_default_upstream_name(php_version):
|
||||
return 'default_php_%s' % php_version
|
||||
|
||||
def php_fpm_service(php_version):
|
||||
if php_version == '5.6':
|
||||
return 'php5-fpm'
|
||||
else:
|
||||
return 'php%s-fpm' % php_version
|
||||
return 'php%s-fpm' % php_version
|
||||
|
||||
class FilterModule(object):
|
||||
''' PHP module '''
|
||||
|
||||
@@ -26,4 +26,3 @@ galaxy_info:
|
||||
- nginx
|
||||
- cdn
|
||||
dependencies: []
|
||||
|
||||
|
||||
@@ -11,17 +11,9 @@
|
||||
cache_valid_time: 3600
|
||||
changed_when: false
|
||||
|
||||
- name: APT | Force OpenSSL from backports (fix dependency break)
|
||||
apt:
|
||||
pkg: openssl
|
||||
state: latest
|
||||
default_release: "{{ ansible_distribution_release + '-backports' }}"
|
||||
when: nginx_backports
|
||||
|
||||
- name: APT | Install nginx and dependencies
|
||||
apt:
|
||||
pkg: "{{ nginx_apt_package }}"
|
||||
state: present
|
||||
default_release: "{{ ansible_distribution_release + '-backports' if nginx_backports else ansible_distribution_release }}"
|
||||
|
||||
- name: APT | Install nginx modules
|
||||
@@ -50,11 +42,13 @@
|
||||
repo: 'https://github.com/Neilpang/acme.sh.git'
|
||||
dest: '{{ nginx_acmesh_git_dir }}'
|
||||
update: no
|
||||
version: master
|
||||
|
||||
- name: SHELL | Install acme.sh
|
||||
shell: ./acme.sh --install --home "{{ nginx_acmesh_dir }}"
|
||||
- name: COMMAND | Install acme.sh
|
||||
command: ./acme.sh --install --home "{{ nginx_acmesh_dir }}"
|
||||
args:
|
||||
chdir: "{{ nginx_acmesh_git_dir }}"
|
||||
creates: "{{ nginx_acmesh_dir }}"
|
||||
|
||||
when: not acme.stat.exists
|
||||
|
||||
|
||||
@@ -7,14 +7,24 @@
|
||||
register: nginx_version
|
||||
changed_when: false
|
||||
check_mode: no
|
||||
tags:
|
||||
- skip_ansible_lint
|
||||
|
||||
- name: SHELL | Get module list
|
||||
shell: nginx -V 2>&1 | tr -- - '\n' | grep -A 1 with | grep _module | sed -r 's/_module//g; s/\s+//g' | sort
|
||||
shell: |
|
||||
nginx -V 2>&1 |
|
||||
tr -- - '\n' |
|
||||
grep -A 1 with |
|
||||
grep _module |
|
||||
sed -r 's/_module//g; s/\s+//g' |
|
||||
sort
|
||||
args:
|
||||
executable: /bin/sh
|
||||
register: shell_modules
|
||||
changed_when: false
|
||||
check_mode: no
|
||||
tags:
|
||||
- skip_ansible_lint
|
||||
|
||||
- name: SET_FACT | Save modules
|
||||
set_fact:
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
|
||||
- name: FILE | Disable sites
|
||||
file:
|
||||
path: "{{ nginx_etc_dir}}/sites-enabled/{{ item | nginx_site_filename }}"
|
||||
path: "{{ nginx_etc_dir }}/sites-enabled/{{ item | nginx_site_filename }}"
|
||||
state: absent
|
||||
loop: "{{ nginx_sites }}"
|
||||
notify: ['reload nginx', 'restart nginx freebsd']
|
||||
|
||||
@@ -46,8 +46,13 @@
|
||||
warn: false
|
||||
when: fake_site.changed and ansible_virtualization_type == 'docker'
|
||||
|
||||
- name: SHELL | Get certificates
|
||||
shell: '{{ nginx_acmesh_bin }} --home {{ nginx_acmesh_dir }} --issue{% if item.name is string %} -d {{ item.name }}{% else %}{% for name in item.name %} -d {{ name }}{% endfor %}{% endif %} --nginx {% if nginx_acmesh_test %}--test{% endif %}'
|
||||
- name: COMMAND | Get certificates
|
||||
command: |
|
||||
{{ nginx_acmesh_bin }}
|
||||
--home {{ nginx_acmesh_dir }}
|
||||
--issue{% if item.name is string %} -d {{ item.name }}{% else %}{% for name in item.name %} -d {{ name }}{% endfor %}{% endif %}
|
||||
--nginx
|
||||
{% if nginx_acmesh_test %}--test{% endif %}
|
||||
args:
|
||||
creates: "{{ nginx_acmesh_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.key"
|
||||
loop: "{{ acme_create }}"
|
||||
@@ -60,8 +65,14 @@
|
||||
path: "{{ nginx_ssl_dir }}/{{ item | nginx_site_name }}"
|
||||
loop: "{{ acme_create }}"
|
||||
|
||||
- name: SHELL | Install certificates
|
||||
shell: '{{ nginx_acmesh_bin }} --home {{ nginx_acmesh_dir }} --install-cert -d {{ item | nginx_site_name }} --fullchain-file {{ nginx_ssl_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.crt --key-file {{ nginx_ssl_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.key --reloadcmd "service nginx restart"'
|
||||
- name: COMMAND | Install certificates
|
||||
command: |
|
||||
{{ nginx_acmesh_bin }}
|
||||
--home {{ nginx_acmesh_dir }}
|
||||
--install-cert -d {{ item | nginx_site_name }}
|
||||
--fullchain-file {{ nginx_ssl_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.crt
|
||||
--key-file {{ nginx_ssl_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.key
|
||||
--reloadcmd "service nginx reload"
|
||||
args:
|
||||
creates: "{{ nginx_ssl_dir }}/{{ item | nginx_site_name }}/{{ item | nginx_site_name }}.key"
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
@@ -70,12 +81,6 @@
|
||||
|
||||
rescue:
|
||||
|
||||
- name: FILE | Delete acme.sh files
|
||||
file:
|
||||
path: "{{ nginx_acmesh_dir }}/{{ item | nginx_site_name }}/"
|
||||
state: absent
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
|
||||
- name: FAIL | Explicit
|
||||
fail:
|
||||
msg: "Something is bad... Auto crash!"
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
- name: FILE | Create SSL directories
|
||||
file:
|
||||
path: "{{ nginx_ssl_dir + '/' + item | nginx_site_name }}"
|
||||
path: "{{ item | nginx_ssl_dir(nginx_ssl_dir) }}"
|
||||
state: directory
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
when: item.dest_key is not defined or item.dest_cert is not defined
|
||||
@@ -39,7 +39,7 @@
|
||||
- name: COPY | Deploy SSL keys
|
||||
copy:
|
||||
content: "{{ item.key }}"
|
||||
dest: "{{ nginx_ssl_dir + '/' + item | nginx_site_name + '/' + item | nginx_site_name + '.key' if item.dest_key is not defined else item.dest_key }}"
|
||||
dest: "{{ item | nginx_key_path(nginx_ssl_dir) }}"
|
||||
mode: 0640
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
when: item.key is defined
|
||||
@@ -49,7 +49,7 @@
|
||||
- name: COPY | Deploy SSL certs
|
||||
copy:
|
||||
content: "{{ item.cert }}"
|
||||
dest: "{{ nginx_ssl_dir + '/' + item | nginx_site_name + '/' + item | nginx_site_name + '.crt' if item.dest_cert is not defined else item.dest_cert }}"
|
||||
dest: "{{ item | nginx_cert_path(nginx_ssl_dir) }}"
|
||||
mode: 0644
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
when: item.cert is defined
|
||||
@@ -57,10 +57,15 @@
|
||||
no_log: not nginx_debug_role
|
||||
|
||||
- name: COMMAND | Create self-signed certificates
|
||||
command: "openssl req -new -newkey rsa:2048 -sha256 -days 3650 -nodes -x509 -subj '/CN={{ item | nginx_site_name }}' -keyout {{ item | nginx_site_name + '.key' }} -out {{ item | nginx_site_name + '.crt' }}"
|
||||
command: |
|
||||
openssl req
|
||||
-new -newkey rsa:2048 -sha256 -days 3650 -nodes -x509
|
||||
-subj '/CN={{ item | nginx_site_name }}'
|
||||
-keyout {{ item | nginx_key_path(nginx_ssl_dir) }}
|
||||
-out {{ item | nginx_cert_path(nginx_ssl_dir) }}
|
||||
args:
|
||||
chdir: "{{ nginx_ssl_dir + '/' + item | nginx_site_name }}"
|
||||
creates: "{% if item.force is defined and item.force %}/tmp/dummy{% else %}{{ nginx_ssl_dir + '/' + item | nginx_site_name + '/' + item | nginx_site_name + '.crt' }}{% endif %}"
|
||||
chdir: "{{ item | nginx_ssl_dir(nginx_ssl_dir) }}"
|
||||
creates: "{{ '/tmp/dummy' if item.force is defined and item.force else item | nginx_cert_path(nginx_ssl_dir) }}"
|
||||
loop: "{{ nginx_ssl_pairs }}"
|
||||
when: item.self_signed is defined
|
||||
notify: restart nginx
|
||||
|
||||
@@ -1,18 +1,40 @@
|
||||
---
|
||||
|
||||
- block:
|
||||
|
||||
- name: SET_FACT | temp
|
||||
set_fact:
|
||||
tmp_fpm_pool: |
|
||||
[
|
||||
{% for p in ansible_local.hanxhx_php.fpm_pool %}
|
||||
{
|
||||
"upstream_name": "{{ p.name }}",
|
||||
"sockets": [{
|
||||
{% if p.listen.startswith('/') %}
|
||||
"unix": "{{ p.listen }}"
|
||||
{% else %}
|
||||
{% set host = p.listen.split(":")[0] %}
|
||||
{% set port = p.listen.split(":")[1] %}
|
||||
"host": "{{ host }}",
|
||||
"port": "{{ port }}"
|
||||
{% endif %}
|
||||
}]
|
||||
}{% if not loop.last %},{% endif %}
|
||||
{% endfor %}
|
||||
]
|
||||
|
||||
- name: SET_FACT | new php
|
||||
set_fact:
|
||||
nginx_php: "{{ nginx_php + tmp_fpm_pool }}"
|
||||
|
||||
when: ansible_local.hanxhx_php.fpm_pool is defined
|
||||
|
||||
- name: TEMPLATE | Deploy PHP upstream to Nginx
|
||||
template:
|
||||
src: "etc/nginx/conf.d/php.conf.j2"
|
||||
dest: "{{ nginx_etc_dir }}/conf.d/php.conf"
|
||||
when: nginx_php | length > 0
|
||||
notify: reload nginx
|
||||
|
||||
- name: FILE | Delete PHP upstream
|
||||
file:
|
||||
path: "{{ nginx_etc_dir }}/conf.d/php.conf"
|
||||
state: absent
|
||||
when: nginx_php | length == 0
|
||||
|
||||
- name: TEMPLATE | Deploy other upstreams
|
||||
template:
|
||||
src: "etc/nginx/conf.d/_upstream.conf.j2"
|
||||
|
||||
@@ -14,7 +14,18 @@ upstream {{ php.upstream_name | default((php.version | php_default_upstream_name
|
||||
server unix:{{ php.version | php_default_upstream_socket }} weight=1;
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
{% if ansible_local.hanxhx_php.fpm_pool is defined%}
|
||||
# -------------------------------------------------------
|
||||
# Auto-detected PHP config for HanXHX.php ansible role
|
||||
# -------------------------------------------------------
|
||||
|
||||
{% for php in ansible_local.hanxhx_php.fpm_pool %}
|
||||
upstream {{ php.name }} {
|
||||
server {% if php.listen.startswith('/') %}unix:{{ php.listen }}{% else %}{{ php.listen }}{% endif %};
|
||||
}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
# vim:filetype=nginx
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
{%- endmacro %}
|
||||
{% macro ssl(ssl_name) %}
|
||||
{% for sn in nginx_ssl_pairs if ((sn.name is string and sn.name == ssl_name) or (sn.name.0 == ssl_name)) %}
|
||||
ssl_certificate {{ nginx_ssl_dir + '/' + ssl_name + '/' + ssl_name + '.crt' if sn.dest_cert is not defined else sn.dest_cert }};
|
||||
ssl_certificate_key {{ nginx_ssl_dir + '/' + ssl_name + '/' + ssl_name + '.key' if sn.dest_key is not defined else sn.dest_key }};
|
||||
ssl_certificate {{ sn | nginx_cert_path(nginx_ssl_dir) }};
|
||||
ssl_certificate_key {{ sn | nginx_key_path(nginx_ssl_dir) }};
|
||||
{% endfor %}
|
||||
{%- endmacro %}
|
||||
{% macro httpsredirect(name) %}
|
||||
@@ -106,7 +106,7 @@ server {
|
||||
{% block template_headers %}
|
||||
# --> Custom headers
|
||||
{% for key, value in __headers.iteritems() %}
|
||||
add_header {{ key }} "{{ value }}";
|
||||
add_header {{ key }} "{{ value | regex_replace('\s+always$', '') }}"{% if value | regex_search('\s+always$') %} always{% endif %};
|
||||
{% endfor %}
|
||||
# <-- Custom headers
|
||||
{% endblock %}
|
||||
@@ -140,7 +140,7 @@ server {
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
location ~* \.(txt|js|css|png|jpe?g|gif|ico|svg)$ {
|
||||
location ~* \.(txt|js|css|png|jpe?g|gif|ico|svg|(o|t)tf|woff2?|eot)$ {
|
||||
expires 30d;
|
||||
log_not_found off;
|
||||
}
|
||||
|
||||
28
templates/etc/nginx/sites-available/_symfony.j2
Normal file
28
templates/etc/nginx/sites-available/_symfony.j2
Normal file
@@ -0,0 +1,28 @@
|
||||
{% extends "_php.j2" %}
|
||||
|
||||
{% block template_try_files %}
|
||||
try_files $uri /index.php$is_args$args;
|
||||
{% endblock %}
|
||||
|
||||
{% block template_upstream_location %}
|
||||
location /bundles {
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
location ~ ^/index\.php(/|$) {
|
||||
# {{ php_info }}
|
||||
fastcgi_pass {{ php_upstream }};
|
||||
fastcgi_index index.php;
|
||||
{% if item.upstream_params is defined and item.upstream_params is iterable %}
|
||||
{% for param in item.upstream_params %}
|
||||
{{ param }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
include fastcgi.conf;
|
||||
internal;
|
||||
}
|
||||
|
||||
location ~ \.php$ {
|
||||
return 404;
|
||||
}
|
||||
{% endblock %}
|
||||
@@ -78,3 +78,24 @@
|
||||
- name: SET_FACT | ngrok_path
|
||||
set_fact:
|
||||
ngrok_path: '/tmp/ngrok'
|
||||
|
||||
- name: USER | Create PHP User foo
|
||||
user:
|
||||
name: foo
|
||||
system: yes
|
||||
|
||||
- name: INCLUDE_ROLE | HanXHX.php
|
||||
include_role:
|
||||
name: "{{ playbook_dir }}/HanXHX.php"
|
||||
vars:
|
||||
php_version: "{{ nginx_php.0.version }}"
|
||||
php_autoremove_default_pool: false
|
||||
php_fpm_poold:
|
||||
- name: 'hx_unix'
|
||||
user: 'foo'
|
||||
php_value:
|
||||
display_errors: 'Off'
|
||||
php_admin_value:
|
||||
memory_limit: '98M'
|
||||
- name: 'hx_ip'
|
||||
listen: '127.0.0.1:9636'
|
||||
|
||||
@@ -23,10 +23,20 @@
|
||||
src: "file/test.key"
|
||||
dest: "{{ int_ansible_ssl_dir }}/test.key"
|
||||
|
||||
- name: LINEINFILE | Add all hosts in /etc/hosts
|
||||
lineinfile:
|
||||
line: "127.0.0.1\tlocalhost {% for s in nginx_sites %}{% if s.name is string %}{{ s.name }}{% else %}{% for n in s.name %}{{ n }} {% endfor %}{% endif %} {% if s.redirect_from is defined %}{% for rf in s.redirect_from %}{{ rf }} {% endfor %}{% endif %}{% endfor %}"
|
||||
regexp: '^127\.0\.0\.1'
|
||||
- name: COPY | Add all hosts in /etc/hosts
|
||||
copy:
|
||||
content: |
|
||||
127.0.0.1 localhost
|
||||
{% for s in nginx_sites %}
|
||||
{% if s.name is string %}
|
||||
127.0.0.1 {{ s.name }}
|
||||
{% else %}
|
||||
127.0.0.1 {% for n in s.name %}{{ n }} {% endfor %}
|
||||
{% endif %}
|
||||
{% if s.redirect_from is defined %}
|
||||
127.0.0.1 {% for rf in s.redirect_from %}{{ rf }} {% endfor %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
dest: "/etc/hosts"
|
||||
unsafe_writes: yes
|
||||
|
||||
@@ -203,8 +213,10 @@
|
||||
use_access_log: true
|
||||
- name: 'test-php-index.local'
|
||||
template: '_php_index'
|
||||
php_upstream: 'hx_unix'
|
||||
- name: 'test-php-index2.local'
|
||||
template: '_php_index2'
|
||||
php_upstream: 'hx_ip'
|
||||
- name: 'test-proxy.local'
|
||||
listen:
|
||||
- 8080
|
||||
@@ -355,7 +367,7 @@
|
||||
# --------------------------------
|
||||
- name: -- VERIFY PHP SITES --
|
||||
uri:
|
||||
url: "http://{{ item.name}}/"
|
||||
url: "http://{{ item.name }}/"
|
||||
return_content: yes
|
||||
register: p
|
||||
loop: "{{ nginx_sites }}"
|
||||
@@ -510,3 +522,5 @@
|
||||
executable: /bin/sh
|
||||
changed_when: false
|
||||
when: nginx_auto_config_httpv2 and 'http_v2' in nginx_modules
|
||||
tags:
|
||||
- skip_ansible_lint
|
||||
|
||||
@@ -46,6 +46,6 @@ nginx_templates_no_dir:
|
||||
nginx_servers_default_headers:
|
||||
'X-Frame-Options': 'DENY always'
|
||||
'X-Content-Type-Options': 'nosniff always'
|
||||
'X-XSS-Protection': '1; mode=block'
|
||||
'X-XSS-Protection': '1; mode=block always'
|
||||
|
||||
nginx_acmesh_bin: "{{ nginx_acmesh_dir }}/acme.sh"
|
||||
|
||||
Reference in New Issue
Block a user