--- - name: Delete default nginx site configuration ansible.builtin.file: path: "{{ nginx_site_config_path }}/default" state: absent when: nginx_delete_default_site_config notify: - Validate nginx configuration - Reload nginx - name: Find existing site configs ansible.builtin.find: paths: "{{ nginx_site_config_path }}" file_type: file register: nginx_existing_site_configs when: nginx_prune_sites - name: Remove site configs no longer in nginx_sites ansible.builtin.file: path: "{{ item.path }}" state: absent loop: "{{ nginx_existing_site_configs.files | default([]) }}" loop_control: label: "{{ item.path | basename }}" when: - nginx_prune_sites - (item.path | basename) not in (nginx_sites | map(attribute='name') | list) notify: - Validate nginx configuration - Reload nginx - name: Find existing enabled-site symlinks ansible.builtin.find: paths: "{{ nginx_site_enabled_path }}" file_type: any register: nginx_existing_enabled_sites when: nginx_prune_sites - name: Remove enabled-site symlinks no longer in nginx_sites ansible.builtin.file: path: "{{ item.path }}" state: absent loop: "{{ nginx_existing_enabled_sites.files | default([]) }}" loop_control: label: "{{ item.path | basename }}" when: - nginx_prune_sites - (item.path | basename) not in (nginx_sites | map(attribute='name') | list) notify: - Validate nginx configuration - Reload nginx - name: Add all the template site configurations ansible.builtin.template: src: "{{ item.template_path | default(nginx_site_config_template) }}" dest: "{{ nginx_site_config_path }}/{{ item.name }}" owner: root mode: "0644" when: item.conf_file is undefined loop: "{{ nginx_sites }}" notify: - Validate nginx configuration - Reload nginx - name: Add all site configs using custom files ansible.builtin.copy: src: "{{ item.conf_file }}" dest: "{{ nginx_site_config_path }}/{{ item.name }}" owner: root mode: "0644" when: item.conf_file is defined loop: "{{ nginx_sites }}" notify: - Validate nginx configuration - Reload nginx - name: Manage symlinks for sites ansible.builtin.file: src: "{{ nginx_site_config_path }}/{{ item.name }}" dest: "{{ nginx_site_enabled_path }}/{{ item.name }}" state: "{{ (item.enabled | default(nginx_site_enabled_by_default)) | ternary('link', 'absent') }}" loop: "{{ nginx_sites }}" notify: - Validate nginx configuration - Reload nginx - name: Copy over configuration snippets ansible.builtin.copy: src: "snippets/{{ item }}" dest: "{{ nginx_snippets_path }}/{{ item }}" owner: root mode: "0644" loop: "{{ nginx_snippets }}" notify: - Validate nginx configuration - Reload nginx - name: Render templated conf.d files ansible.builtin.template: src: "conf.d/{{ item }}.j2" dest: "{{ nginx_conf_d_path }}/{{ item }}" owner: root mode: "0644" loop: "{{ nginx_conf_d_templates }}" notify: - Validate nginx configuration - Reload nginx - name: Add the main nginx configuration ansible.builtin.copy: src: nginx.conf dest: /etc/nginx/nginx.conf owner: root mode: "0644" validate: 'nginx -t -c %s' notify: - Validate nginx configuration - Reload nginx