- name: setup nginx gather_facts: no hosts: "{{target}}" become: "{{become_var}}" vars: brotli: no tasks: - name: check nginx is installed shell: executable: /bin/bash cmd: "[[ -x `which nginx` ]]" changed_when: false check_mode: false # fine for both debian and redhat systems (incl. Bitrix) - name: catch-all vhost page copy: content: |
nothing here dest: /var/www/html/index.html # single master template for all? #- name: nginx setup # template: # src: nginx.conf # dest: /etc/nginx/nginx.conf # mode: 0644 # notify: reload nginx # templating possible but not necessary - name: host-specific overall nginx config template: src: "{{playbook_dir}}/../../configs/nginx/{{inventory_hostname_short}}/nginx.conf" dest: /etc/nginx/nginx.conf notify: reload nginx - name: define conf.d/ file list set_fact: # fileglob lists file pathes separated by a comma - convert to list # also we want the basename to be able to check found files against that list confs: "{{ lookup( 'fileglob', '{{playbook_dir}}/../../configs/nginx/' + inventory_hostname_short + '/conf.d/*.conf' ) | split(',') | map('basename') }}" - debug: var=confs # assuming raw file without templating - name: host-specific nginx vhost configs copy: src: "{{playbook_dir}}/../../configs/nginx/{{inventory_hostname_short}}/conf.d/{{item}}" dest: /etc/nginx/conf.d/{{item}} loop: "{{confs}}" notify: reload nginx - name: find existing files find: paths: /etc/nginx/conf.d/ register: found - name: clean-up unknown files stat: path: "{{item.path}}" loop: "{{found.files}}" loop_control: label: "descr {{item.mode}} {{item.pw_name}} {{item.gr_name}} {{item.path}}" changed_when: item.path | basename not in confs register: statecheck - fail: msg: remove those files or add to code when: statecheck.changed handlers: - name: reload nginx shell: executable: /bin/bash cmd: "systemctl reload nginx 2>/dev/null || nginx -s reload" - name: restart nginx shell: executable: /bin/bash cmd: "systemctl restart nginx 2>dev/null || nginx -s reopen"