|
133 | 133 | ansible.builtin.find:
|
134 | 134 | paths: "{{ grafana_ini.paths.data }}/dashboards"
|
135 | 135 | hidden: true
|
| 136 | + recurse: true |
136 | 137 | patterns:
|
137 | 138 | - "*.json"
|
138 | 139 | register: __dashboards_present
|
139 | 140 | when: grafana_provisioning_synced
|
140 | 141 |
|
| 142 | + - name: "Register previously created folders" |
| 143 | + ansible.builtin.find: |
| 144 | + paths: "{{ grafana_ini.paths.data }}/dashboards" |
| 145 | + recurse: yes |
| 146 | + file_type: directory |
| 147 | + register: __dashboards_dir_present |
| 148 | + become: true |
| 149 | + when: grafana_provisioning_synced |
| 150 | + |
141 | 151 | - name: "Import grafana.net dashboards"
|
142 | 152 | ansible.builtin.copy:
|
143 | 153 | src: "{{ item }}"
|
|
153 | 163 | when: not ansible_check_mode
|
154 | 164 |
|
155 | 165 | - name: "Import custom grafana dashboards"
|
156 |
| - ansible.builtin.copy: |
157 |
| - src: "{{ grafana_dashboards_dir }}" |
158 |
| - dest: "{{ grafana_ini.paths.data }}/dashboards/" |
159 |
| - owner: root |
160 |
| - group: grafana |
161 |
| - mode: "0640" |
162 |
| - become: true |
163 |
| - register: __dashboards_copied |
164 |
| - notify: "provisioned dashboards changed" |
| 166 | + block: |
| 167 | + - name: "Find which directories to create" |
| 168 | + ansible.builtin.find: |
| 169 | + paths: "{{ grafana_dashboards_dir }}" |
| 170 | + recurse: yes |
| 171 | + file_type: directory |
| 172 | + register: __dashboards_subdirs |
| 173 | + delegate_to: localhost |
| 174 | + become: true |
| 175 | + |
| 176 | + - name: "Create dashboard folders" |
| 177 | + ansible.builtin.file: |
| 178 | + path: "{{ grafana_ini.paths.data }}/dashboards/{{ item }}" |
| 179 | + state: "directory" |
| 180 | + recurse: false |
| 181 | + owner: "grafana" |
| 182 | + group: "grafana" |
| 183 | + mode: "0755" |
| 184 | + loop: "{{ __dashboards_subdirs.files | map(attribute='path') | sort | regex_replace(grafana_dashboards_dir, '') }}" |
| 185 | + become: true |
| 186 | + register: __dashboards_dir_created |
| 187 | + when: not ansible_check_mode |
| 188 | + |
| 189 | + - name: "Copy dashboard files" |
| 190 | + ansible.builtin.copy: |
| 191 | + src: "{{ item.path }}" |
| 192 | + dest: '{{ grafana_ini.paths.data }}/dashboards/{{ item.path | regex_replace(grafana_dashboards_dir, "") }}' |
| 193 | + owner: root |
| 194 | + group: grafana |
| 195 | + mode: "0644" |
| 196 | + loop: "{{ __found_dashboards | json_query('files[*]') }}" |
| 197 | + become: true |
| 198 | + register: __dashboards_copied |
| 199 | + notify: "provisioned dashboards changed" |
| 200 | + when: not ansible_check_mode |
| 201 | + |
| 202 | + - name: "Register present and copied folders list" |
| 203 | + ansible.builtin.set_fact: |
| 204 | + __folders_present_list: "{{ __dashboards_dir_present | json_query('files[*].path') | default([]) }}" |
| 205 | + __folders_copied_list: "{{ __dashboards_dir_created | json_query('results[*].path') | default([]) }}" |
165 | 206 | when: not ansible_check_mode
|
166 | 207 |
|
167 | 208 | - name: "Register present and copied dashboards list"
|
|
177 | 218 | loop: "{{ __dashboards_present_list | difference(__dashboards_copied_list) }}"
|
178 | 219 | become: true
|
179 | 220 | when: grafana_provisioning_synced and not ansible_check_mode
|
| 221 | + |
| 222 | + - name: "Remove folders not present on deployer machine (synchronize)" |
| 223 | + ansible.builtin.file: |
| 224 | + path: "{{ item }}" |
| 225 | + state: absent |
| 226 | + loop: "{{ __folders_present_list | difference(__folders_copied_list) }}" |
| 227 | + become: true |
| 228 | + when: grafana_provisioning_synced and not ansible_check_mode |
0 commit comments