forked from Azure-Samples/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlock-resources.yml
66 lines (57 loc) · 2.09 KB
/
unlock-resources.yml
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Description
# ===========
# This playbook lists all the locks in current subscription using azure_rm_resource_facts,
# set variable force_delete=True to force delete lock, then resource such as resource group, vm
- name: Attempt to delete RG with locked resource
hosts: localhost
connection: local
vars:
force_delete: no
resource_id_list: []
resource_list: []
resource_sorted: []
tasks:
- name: List all locks in the subscription
azure_rm_resource_facts:
api_version: '2016-09-01'
# uncomment this to narrow the query to selected resource group
# resource_group: "{{ resource_group }}"
provider: authorization
resource_type: locks
register: output
no_log: true
- name: Get locked resources
set_fact:
resource_id_list: "{{ resource_id_list }} + [ '{{ item.id[:item.id.index('/providers/Microsoft.Authorization/locks')] }}' ]"
with_items: "{{ output.response[0].value }}"
no_log: true
# for Ansible 2.8 use following line:
# with_items: "{{ output.response }}"
- name: Parse resource type and resource name
set_fact:
resource_list: >
{{ resource_list }} +
[ '{{ "{0}: {1}".format(item.split("/")[6], item.split("/")[-1]) if item.split("/") | length > 5 else "{0}: {1}".format(item.split("/")[3], item.split("/")[-1]) }}' ]
with_items: "{{ resource_id_list }}"
no_log: true
- name: Sort output
set_fact:
resource_sorted: "{{ resource_list | sort(case_sensitive=False) }}"
no_log: true
- name: Print resource with locks
debug:
var: resource_sorted
- name: Delete all locks if force_delete is true
azure_rm_resource:
api_version: '2017-04-01'
url: "{{ item.id }}"
state: absent
with_items: "{{ output.response[0].value }}"
when: force_delete | bool
- name: Delete all locked resources if force_delete is true
azure_rm_resource:
api_version: '2018-05-01'
url: "{{ item | regex_replace('/', '%2F') }}"
state: absent
with_items: "{{ resource_id_list }}"
when: force_delete | bool