xoxys.nginx/tasks/install.yml

130 lines
3.1 KiB
YAML

---
- block:
- 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
when: nginx_official_repo_enabled
- name: Installing nginx
yum:
name: nginx
state: installed
- name: Create group '{{ nginx_group }}'
group:
name: "{{ nginx_group }}"
state: present
when: nginx_group != "nginx"
- name: Create user '{{ nginx_user }}'
user:
name: "{{ nginx_user }}"
group: "{{ nginx_group }}"
createhome: no
shell: /sbin/nologin
when: nginx_user != "nginx"
- name: Prepare vhost directories
file:
path: "{{ item }}"
state: directory
owner: nginx
group: nginx
mode: 0750
with_items:
- "{{ nginx_vhosts_dir }}"
- "{{ nginx_vhosts_dir }}/default"
- name: Prepare nginx directories
file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: 0640
with_items:
- /etc/nginx/sites-available
- /etc/nginx/sites-enabled
- 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
- name: Remove default.conf from conf.d
file:
path: /etc/nginx/conf.d/default.conf
state: absent
- 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
validate: bash -c 'nginx -t -c /dev/stdin <<< "events {worker_connections 1;} http { include %s; }"'
notify: __nginx_reload
- name: Open ports in iptables
iptables_raw:
name: allow_nginx_ports
state: present
rules: '-A INPUT -p tcp -m multiport --dports {{ nginx_open_ports|join(",") }} -j ACCEPT'
when: nginx_iptables_enabled
become: True
become_user: root
- block:
- name: Add default page
template:
src: var/www/vhosts/default/index.html.j2
dest: /var/www/vhosts/default/index.html
owner: nginx
group: nginx
mode: 0750
when: nginx_default_page_enabled
become: True
become_user: nginx
- block:
- name: Add default page configuration file
template:
src: etc/nginx/sites-available/default.j2
dest: /etc/nginx/sites-available/default
owner: root
group: root
mode: 0640
notify: __nginx_reload
- name: Enable default page
file:
src: /etc/nginx/sites-available/default
dest: /etc/nginx/sites-enabled/default
owner: root
group: root
state: link
notify: __nginx_reload
when: nginx_default_page_enabled
become: True
become_user: root
- name: Enable nginx service
systemd:
state: started
daemon_reload: yes
enabled: yes
name: nginx
become: True
become_user: root