1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 ---
- 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 )