--- - name: "Create directory: /etc/nginx/vhost" file: path: /etc/nginx/vhosts state: directory owner: "{{ services.nginx.user | default('http') }}" group: "{{ services.nginx.group | default('http') }}" - name: Generate nginx vhosts template: src: vhost.conf.j2 dest: /etc/nginx/vhosts/{{item.key}}.conf owner: "{{ services.nginx.user | default('http') }}" group: "{{ services.nginx.group | default('http') }}" mode: 0644 notify: "Restart nginx" register: deployed_nginx_vhosts loop: "{{ lookup('dict', services.nginx.vhosts, wantlist=True) }}" - name: "Collect files in directory: /etc/nginx/vhosts" find: path: /etc/nginx/vhosts hidden: yes register: nginx_vhosts check_mode: no changed_when: false - name: "Remove unmanaged files in directory: /etc/nginx/vhosts" file: path: "/etc/nginx/vhosts/{{ item.path | basename }}" state: absent with_items: - "{{ nginx_vhosts.files }}" notify: "Restart nginx" when: - (item.path) not in ( deployed_nginx_vhosts | json_query('results[].invocation.module_args.dest') | list )