Guillaume Coré
2020-03-11 1d970fbaa4bd88c9d094d9587db59fdf9cd0239a
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
- environment:
    AZURE_CLIENT_ID: "{{azure_service_principal}}"
    AZURE_TENANT: "{{azure_tenant}}"
    AZURE_SECRET: "{{azure_password}}"
    AZURE_SUBSCRIPTION_ID: "{{azure_subscription_id}}"
    # AZURE_CONFIG_DIR: create a specific config dir for this stack to allow concurrent access for 'az' command
    AZURE_CONFIG_DIR: "/tmp/.azure-{{project_tag}}"
  block:
    - name: Delete delegation for NS to the main DNSZone
      when: az_dnszone_resource_group != 'none'
      azure_rm_dnsrecordset:
        resource_group: "{{az_dnszone_resource_group|default('dns')}}"
        relative_name: "{{guid}}"
        zone_name: "{{HostedZoneId}}"
        record_type: NS
        state: absent
 
    - name: Destroy method resourceGroup (standalone ResourceGroup)
      when: az_destroy_method|default('resource_group') == 'resource_group'
      block:
        - name: Delete the resource group
          azure_rm_resourcegroup:
            name: "{{az_resource_group}}"
            state: absent
            force: true
          tags:
            - destroying
            - destroy_cloud_deployment
            - destroy_azure_deployment
          register: azure_result
          until: azure_result is succeeded
          retries: 6
          delay: 10
          ignore_errors: yes
          when:
            - cloud_provider == 'azure'
 
        - name: report azure error
          fail:
            msg: "FAIL {{ az_resource_group }} Azure delete group"
          when:
            - azure_result is failed
            - cloud_provider == 'azure'
          tags:
            - destroying
            - destroy_cloud_deployment
            - destroy_azure_deployment
 
    - name: Method destroy deployment (shared ResourceGroup)
      when: az_destroy_method|default('resource_group') == 'deployment'
      block:
        - set_fact:
            t_dest: "{{output_dir}}/{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
 
        - name: Get all resources from the deployment
          command: az group deployment show --name {{env_type}}.{{guid}} --resource-group {{az_resource_group}}
          changed_when: false
          register: az_dep
          until: az_dep is succeeded
          retries: 5
 
        - debug:
            var: az_dep
            verbosity: 2
 
        - name: Get all resources from tag
          command: az resource list --tag 'Project={{project_tag}}'
          register: az_tag
          retries: 5
          until: az_tag is succeeded
          changed_when: false
 
        - name: delete all resources
          vars:
            ids_tags: "{{ az_tag.stdout|from_json|json_query('[*].id')}}"
            ids_deployment: "{{ az_dep.stdout|from_json|json_query('properties.additionalProperties.outputResources[*].id')}}"
            ids: "{{ids_deployment|union(ids_tags)}}"
          command: "az resource delete --ids {{ids|join(' ')}} --resource-group {{az_resource_group}}"
          register: az_delete
          until: az_delete is succeeded
          retries: 5
          when: ids|length > 0