2017-07-15 13:45:46 +00:00
|
|
|
---
|
2017-12-23 11:30:23 +00:00
|
|
|
- block:
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Add nginx repository
|
|
|
|
yum_repository:
|
|
|
|
name: nginx
|
|
|
|
file: nginx
|
|
|
|
description: NGINX High Performance Web Server
|
|
|
|
baseurl: "https://nginx.org/packages/centos/{{ ansible_distribution_major_version }}/$basearch/"
|
|
|
|
gpgkey: https://nginx.org/keys/nginx_signing.key
|
|
|
|
gpgcheck: yes
|
2019-05-19 20:49:00 +00:00
|
|
|
when: nginx_official_repo_enabled | bool
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Installing nginx
|
2018-12-08 14:49:09 +00:00
|
|
|
package:
|
2018-10-22 08:56:39 +00:00
|
|
|
name: nginx
|
2018-12-08 14:49:09 +00:00
|
|
|
state: present
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Create group '{{ nginx_group }}'
|
|
|
|
group:
|
|
|
|
name: "{{ nginx_group }}"
|
|
|
|
state: present
|
|
|
|
when: nginx_group != "nginx"
|
2017-12-24 13:05:27 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Create user '{{ nginx_user }}'
|
|
|
|
user:
|
|
|
|
name: "{{ nginx_user }}"
|
|
|
|
group: "{{ nginx_group }}"
|
|
|
|
createhome: no
|
|
|
|
shell: /sbin/nologin
|
|
|
|
when: nginx_user != "nginx"
|
2017-12-24 13:05:27 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Prepare vhost directories
|
|
|
|
file:
|
|
|
|
path: "{{ item }}"
|
|
|
|
state: directory
|
|
|
|
owner: "{{ nginx_user }}"
|
|
|
|
group: "{{ nginx_group }}"
|
|
|
|
mode: 0750
|
2019-03-25 21:23:40 +00:00
|
|
|
loop:
|
2018-10-22 08:56:39 +00:00
|
|
|
- "{{ nginx_vhosts_dir }}"
|
|
|
|
- "{{ nginx_vhosts_dir }}/default"
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Prepare nginx directories
|
|
|
|
file:
|
|
|
|
path: "{{ item }}"
|
|
|
|
state: directory
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0640
|
2019-03-25 21:23:40 +00:00
|
|
|
loop:
|
2018-10-22 08:56:39 +00:00
|
|
|
- /etc/nginx/sites-available
|
|
|
|
- /etc/nginx/sites-enabled
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Update nginx.conf
|
|
|
|
template:
|
|
|
|
src: etc/nginx/nginx.conf.j2
|
|
|
|
dest: "/etc/nginx/nginx.conf"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0640
|
|
|
|
validate: /sbin/nginx -t -c %s
|
|
|
|
notify: __nginx_reload
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Remove default.conf from conf.d
|
|
|
|
file:
|
|
|
|
path: /etc/nginx/conf.d/default.conf
|
|
|
|
state: absent
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Update header.conf
|
|
|
|
template:
|
|
|
|
src: etc/nginx/conf.d/header.conf.j2
|
|
|
|
dest: /etc/nginx/conf.d/header.conf
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0640
|
2019-07-18 07:11:33 +00:00
|
|
|
validate: /bin/bash -c 'nginx -t -c /dev/stdin <<< "events {worker_connections 1;} http { include %s; }"'
|
2018-10-22 08:56:39 +00:00
|
|
|
notify: __nginx_reload
|
2017-12-23 11:30:23 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Open ports in iptables
|
|
|
|
iptables_raw:
|
|
|
|
name: allow_nginx_ports
|
|
|
|
state: present
|
2019-04-28 14:54:15 +00:00
|
|
|
rules: '-A INPUT -p tcp -m multiport --dports {{ nginx_open_ports | join(",") }} -j ACCEPT'
|
2019-05-19 20:49:00 +00:00
|
|
|
when: nginx_iptables_enabled | bool
|
2018-09-26 22:06:07 +00:00
|
|
|
|
2018-10-22 08:56:39 +00:00
|
|
|
- name: Set selinux booleans
|
|
|
|
seboolean:
|
2019-07-17 21:47:31 +00:00
|
|
|
name: "{{ item.name }}"
|
2018-10-22 08:56:39 +00:00
|
|
|
state: "{{ item.state }}"
|
|
|
|
persistent: "{{ item.persistent }}"
|
2019-03-25 21:23:40 +00:00
|
|
|
loop: "{{ nginx_set_sebooleans }}"
|
2018-10-22 08:56:39 +00:00
|
|
|
when: nginx_set_sebooleans is defined
|
2019-02-20 21:54:30 +00:00
|
|
|
|
|
|
|
- name: Fix selinux file context mappaing for pid file
|
|
|
|
sefcontext:
|
|
|
|
target: '/var/run/nginx(/.*)?'
|
|
|
|
setype: httpd_var_run_t
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Apply new SELinux file context to filesystem
|
|
|
|
command: restorecon -irv /var/run
|
2019-02-21 20:50:45 +00:00
|
|
|
changed_when: False
|
2017-12-23 11:30:23 +00:00
|
|
|
become: True
|
|
|
|
become_user: root
|
2017-12-22 20:31:20 +00:00
|
|
|
|
2018-08-12 09:31:12 +00:00
|
|
|
- block:
|
2019-07-17 21:56:25 +00:00
|
|
|
- name: Add default page
|
|
|
|
template:
|
|
|
|
src: var/www/vhosts/default/index.html.j2
|
|
|
|
dest: /var/www/vhosts/default/index.html
|
|
|
|
owner: "{{ nginx_user }}"
|
|
|
|
group: "{{ nginx_group }}"
|
|
|
|
mode: 0750
|
|
|
|
become: True
|
|
|
|
become_user: "{{ nginx_user }}"
|
|
|
|
|
|
|
|
- block:
|
|
|
|
- name: Add vhost configurations
|
2018-10-22 08:56:39 +00:00
|
|
|
template:
|
2019-07-17 21:32:11 +00:00
|
|
|
src: etc/nginx/sites-available/vhost.j2
|
|
|
|
dest: "/etc/nginx/sites-available/{{ item.file }}"
|
2018-10-22 08:56:39 +00:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0640
|
2019-07-18 07:11:33 +00:00
|
|
|
validate: /bin/bash -c 'nginx -t -c /dev/stdin <<< "events {worker_connections 1;} http { include %s; }"'
|
2019-07-17 22:16:24 +00:00
|
|
|
loop: "{{ nginx_vhosts_default + nginx_vhosts_extra }}"
|
2019-07-17 21:32:11 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.file }}"
|
2018-10-22 08:56:39 +00:00
|
|
|
notify: __nginx_reload
|
2017-12-23 11:25:55 +00:00
|
|
|
|
2019-07-17 21:32:11 +00:00
|
|
|
- name: Enable vhosts
|
2018-10-22 08:56:39 +00:00
|
|
|
file:
|
2019-07-17 21:32:11 +00:00
|
|
|
src: "/etc/nginx/sites-available/{{ item.file }}"
|
|
|
|
dest: "/etc/nginx/sites-enabled/{{ item.file }}"
|
2019-07-17 22:16:24 +00:00
|
|
|
state: "{{ 'link' if item.enabled | default(True) | bool else 'absent' }}"
|
|
|
|
loop: "{{ nginx_vhosts_default + nginx_vhosts_extra }}"
|
2019-07-17 21:32:11 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.file }}"
|
2018-10-22 08:56:39 +00:00
|
|
|
notify: __nginx_reload
|
2017-12-23 11:25:55 +00:00
|
|
|
become: True
|
|
|
|
become_user: root
|