ansible-nginx/tasks/site.yml

113 lines
3.8 KiB
YAML
Raw Normal View History

2015-07-15 20:18:51 +07:00
---
- name: FAIL | Check filenames
2021-09-01 16:58:39 +07:00
ansible.builtin.fail:
msg: "Forbidden keyword default on site {{ item | nginx_site_name }}"
when: item.filename is defined and item.filename == 'default'
2019-02-05 03:25:25 +07:00
loop: "{{ nginx_sites }}"
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2016-03-15 15:22:01 +07:00
- name: FAIL | Check HTTPS redir and proto
2021-09-01 16:58:39 +07:00
ansible.builtin.fail:
2017-07-27 17:21:10 +07:00
msg: "You can't have HTTP proto and HTTPS redirection at the same time"
when:
2016-03-15 15:22:01 +07:00
((item.proto is defined and 'http' in item.proto) or (item.proto is not defined)) and
(item.redirect_http is defined and item.redirect_http)
2019-02-05 03:25:25 +07:00
loop: "{{ nginx_sites }}"
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2016-03-15 15:22:01 +07:00
2015-11-04 15:16:59 +07:00
- name: FILE | Create root directory
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2017-07-27 17:21:10 +07:00
path: "{{ nginx_root }}"
state: directory
2021-09-01 16:45:44 +07:00
mode: 0755
owner: root
group: root
2015-11-04 15:16:59 +07:00
- name: FILE | Create root public folders (foreach nginx_sites)
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
path: "{{ nginx_root }}/{{ item | nginx_site_filename }}/public"
2017-07-27 17:21:10 +07:00
state: directory
owner: "{{ item.owner | default(nginx_user) }}"
group: "{{ item.group | default(nginx_user) }}"
mode: "{{ item.mode | default('0755') }}"
loop: "{{ nginx_sites }}"
2015-11-30 18:16:17 +07:00
when: >
item.root is not defined and
(item.template is defined and item.template not in nginx_templates_no_dir) and
2016-08-30 16:06:38 +07:00
(item.state is not defined or not item.state != 'absent') and
2015-12-01 21:46:57 +07:00
item.redirect_to is not defined
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2015-07-15 20:18:51 +07:00
- name: TEMPLATE | Create sites
2021-09-01 16:58:39 +07:00
ansible.builtin.template:
2017-07-27 17:21:10 +07:00
src: "etc/nginx/sites-available/{{ item.template if item.redirect_to is not defined else '_redirect' }}.j2"
dest: "{{ nginx_etc_dir }}/sites-available/{{ item | nginx_site_filename }}"
2021-09-01 16:45:44 +07:00
mode: 0644
owner: root
group: root
notify: ['reload nginx', 'restart nginx freebsd']
2016-08-30 16:18:14 +07:00
when: item.state is not defined or item.state != 'absent'
loop: "{{ nginx_sites }}"
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2015-07-15 20:18:51 +07:00
- name: FILE | Delete sites
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
path: "{{ nginx_etc_dir }}/{{ item.1 }}/{{ item.0 | nginx_site_filename }}"
2017-07-27 17:21:10 +07:00
state: absent
2019-02-08 04:29:34 +07:00
loop: "{{ nginx_sites | product(dirs) | list }}"
notify: ['reload nginx', 'restart nginx freebsd']
when: item.0.state is defined and item.0.state == 'absent'
2019-02-08 04:29:34 +07:00
vars:
dirs: ['sites-available', 'sites-enabled']
2017-11-03 16:56:18 +07:00
loop_control:
2017-11-03 17:06:57 +07:00
label: "{{ nginx_etc_dir }}/{{ item.1 }}/{{ item.0 | nginx_site_filename }}"
2015-07-30 18:02:21 +07:00
- name: FILE | Enable sites
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
src: "{{ nginx_etc_dir }}/sites-available/{{ item | nginx_site_filename }}"
dest: "{{ nginx_etc_dir }}/sites-enabled/{{ item | nginx_site_filename }}"
2017-07-27 17:21:10 +07:00
state: link
2019-02-05 03:25:25 +07:00
loop: "{{ nginx_sites }}"
notify: ['reload nginx', 'restart nginx freebsd']
2015-07-31 06:02:37 +07:00
when: >
2016-08-30 16:06:38 +07:00
item.state is not defined or item.state == 'present'
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2015-07-15 20:18:51 +07:00
- name: FILE | Disable sites
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
path: "{{ nginx_etc_dir }}/sites-enabled/{{ item | nginx_site_filename }}"
2017-07-27 17:21:10 +07:00
state: absent
2019-02-05 03:25:25 +07:00
loop: "{{ nginx_sites }}"
notify: ['reload nginx', 'restart nginx freebsd']
2016-11-29 20:32:27 +07:00
when: item.state is defined and item.state == 'disabled'
2017-11-03 16:56:18 +07:00
loop_control:
label: "{{ item | nginx_site_name }}"
2015-07-30 18:02:21 +07:00
- name: FILE | Delete default site when explicitely defined
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2017-07-27 17:21:10 +07:00
path: "{{ nginx_etc_dir }}/sites-enabled/default"
state: absent
notify: ['reload nginx', 'restart nginx freebsd']
when: nginx_default_site is not none
2016-01-21 23:08:01 +07:00
- name: FILE | Auto set default site
2021-09-01 16:58:39 +07:00
ansible.builtin.file:
2017-07-27 17:21:10 +07:00
src: "{{ nginx_etc_dir }}/sites-available/default"
dest: "{{ nginx_etc_dir }}/sites-enabled/default"
state: link
notify: ['reload nginx', 'restart nginx freebsd']
when: nginx_default_site is none
2016-11-29 20:32:27 +07:00
- name: TEMPLATE | Deploy facts
2021-09-01 16:58:39 +07:00
ansible.builtin.template:
2017-07-27 17:21:10 +07:00
src: etc/ansible/facts.d/nginx.fact.j2
dest: /etc/ansible/facts.d/nginx.fact
mode: 0644
2017-10-26 20:50:59 +07:00
notify: ['setup']