From 144dbf8bf274b7740b4efbde080af572032200d1 Mon Sep 17 00:00:00 2001
From: Guillaume Coré <gucore@redhat.com>
Date: Thu, 21 Feb 2019 17:51:31 +0100
Subject: [PATCH] OCP 4: DRY, run dynamically 'delete_zone.yml' for each zone

---
 ansible/configs/ocp4-coreos-deployer/delete_zone.yml |   52 +++++++++++++++++
 ansible/configs/ocp4-coreos-deployer/destroy_env.yml |  127 ++---------------------------------------
 2 files changed, 60 insertions(+), 119 deletions(-)

diff --git a/ansible/configs/ocp4-coreos-deployer/delete_zone.yml b/ansible/configs/ocp4-coreos-deployer/delete_zone.yml
new file mode 100644
index 0000000..2553581
--- /dev/null
+++ b/ansible/configs/ocp4-coreos-deployer/delete_zone.yml
@@ -0,0 +1,52 @@
+---
+- name: Get all records from the environment private zone
+  route53_facts:
+    hosted_zone_id: "{{ _hostedzoneid }}"
+    query: record_sets
+  register: records
+  until: records is succeeded
+  retries: 10
+  delay: "{{ 60|random(start=3, step=1) }}"
+
+- debug:
+    var: records
+    verbosity: 2
+
+- name: Delete all non-alias records from the environment zone
+  route53:
+    private_zone: "{{ _zone.Config.PrivateZone }}"
+    zone: "{{aws_public_zone}}"
+    record: "{{item.Name}}"
+    type: "{{item.Type}}"
+    value: "{{item.ResourceRecords|json_query('[].Value')}}"
+    ttl: "{{item.TTL}}"
+    state: absent
+  when: >-
+    'Name' in item
+    and 'ResourceRecords' in item
+    and item.Name != aws_public_zone
+  with_items: "{{records.ResourceRecordSets }}"
+  ignore_errors: yes
+
+- name: Delete all ALIAS records from the environment public zone
+  route53:
+    private_zone: "{{ _zone.Config.PrivateZone }}"
+    zone: "{{aws_public_zone}}"
+    record: "{{item.Name | regex_replace('\\\\052', '*') }}"
+    type: "{{item.Type}}"
+    alias: yes
+    alias_hosted_zone_id: "{{ item.AliasTarget.HostedZoneId }}"
+    value: "{{ item.AliasTarget.DNSName }}"
+    alias_evaluate_target_health: "{{ item.AliasTarget.EvaluateTargetHealth }}"
+    state: absent
+  when: >-
+    'Name' in item
+    and 'AliasTarget' in item
+    and item.Name != aws_public_zone
+
+  with_items: "{{records.ResourceRecordSets }}"
+  ignore_errors: yes
+
+- name: Remove route53 zone
+  command: >-
+    aws route53 delete-hosted-zone --id {{ _hostedzoneid }}
diff --git a/ansible/configs/ocp4-coreos-deployer/destroy_env.yml b/ansible/configs/ocp4-coreos-deployer/destroy_env.yml
index 705ca3f..13d7081 100644
--- a/ansible/configs/ocp4-coreos-deployer/destroy_env.yml
+++ b/ansible/configs/ocp4-coreos-deployer/destroy_env.yml
@@ -294,127 +294,16 @@
       register: awsroute53zone
       changed_when: false
 
-    - set_fact:
-        hostedzoneid: "{{ item.Id | regex_replace('/hostedzone/', '') }}"
+    - name: delete zones
+      include_tasks: delete_zone.yml
+      vars:
+        _zone: "{{ loopzone }}"
+        _hostedzoneid: "{{ loopzone.Id | regex_replace('/hostedzone/', '') }}"
       when:
-        - item.Name == aws_public_zone
-        - not item.Config.PrivateZone
+        - loopzone.Name == aws_public_zone
       loop: "{{ awsroute53zone.stdout|from_json|json_query('HostedZones') }}"
-
-    - set_fact:
-        hostedzoneidprivate: "{{ item.Id | regex_replace('/hostedzone/', '') }}"
-      when:
-        - item.Name == aws_public_zone
-        - item.Config.PrivateZone
-      loop: "{{ awsroute53zone.stdout|from_json|json_query('HostedZones') }}"
-
-    - when: hostedzoneidprivate is defined
-      block:
-        - name: Get all records from the environment private zone
-          route53_facts:
-            hosted_zone_id: "{{ hostedzoneidprivate }}"
-            query: record_sets
-          register: records
-          until: records is succeeded
-          retries: 10
-          delay: "{{ 60|random(start=3, step=1) }}"
-
-        - debug:
-            var: records
-            verbosity: 2
-
-        - name: Delete all non-alias records from the environment private zone
-          route53:
-            private_zone: yes
-            zone: "{{aws_public_zone}}"
-            record: "{{item.Name}}"
-            type: "{{item.Type}}"
-            value: "{{item.ResourceRecords|json_query('[].Value')}}"
-            ttl: "{{item.TTL}}"
-            state: absent
-          when: >-
-            'Name' in item
-            and 'ResourceRecords' in item
-            and item.Name != aws_public_zone
-          with_items: "{{records.ResourceRecordSets }}"
-          ignore_errors: yes
-
-        - name: Delete all ALIAS records from the environment public zone
-          route53:
-            private_zone: yes
-            zone: "{{aws_public_zone}}"
-            record: "{{item.Name | regex_replace('\\\\052', '*') }}"
-            type: "{{item.Type}}"
-            alias: yes
-            alias_hosted_zone_id: "{{ item.AliasTarget.HostedZoneId }}"
-            value: "{{ item.AliasTarget.DNSName }}"
-            alias_evaluate_target_health: "{{ item.AliasTarget.EvaluateTargetHealth }}"
-            state: absent
-          when: >-
-            'Name' in item
-            and 'AliasTarget' in item
-            and item.Name != aws_public_zone
-
-          with_items: "{{records.ResourceRecordSets }}"
-          ignore_errors: yes
-
-        - name: Remove private dns zone manually because sometimes it is still there (managed by terraform)
-          command: >-
-            aws route53 delete-hosted-zone --id {{ hostedzoneidprivate }}
-
-    - when: hostedzoneid is defined
-      block:
-        - name: Get all records from the environment public zone
-          route53_facts:
-            hosted_zone_id: "{{ hostedzoneid }}"
-            query: record_sets
-          register: records
-          until: records is succeeded
-          retries: 10
-          delay: "{{ 60|random(start=3, step=1) }}"
-
-        - debug:
-            var: records
-            verbosity: 2
-
-        - name: Delete all non-alias records from the environment public zone
-          route53:
-            zone: "{{aws_public_zone}}"
-            record: "{{item.Name}}"
-            type: "{{item.Type}}"
-            value: "{{item.ResourceRecords|json_query('[].Value')}}"
-            ttl: "{{item.TTL}}"
-            state: absent
-          when: >-
-            'Name' in item
-            and 'ResourceRecords' in item
-            and item.Name != aws_public_zone
-          with_items: "{{records.ResourceRecordSets }}"
-          ignore_errors: yes
-
-        - name: Delete all ALIAS records from the environment public zone
-          route53:
-            zone: "{{aws_public_zone}}"
-            record: "{{item.Name | regex_replace('\\\\052', '*') }}"
-            type: "{{item.Type}}"
-            alias: yes
-            alias_hosted_zone_id: "{{ item.AliasTarget.HostedZoneId }}"
-            value: "{{ item.AliasTarget.DNSName }}"
-            alias_evaluate_target_health: "{{ item.AliasTarget.EvaluateTargetHealth }}"
-            state: absent
-          when: >-
-            'Name' in item
-            and 'AliasTarget' in item
-            and item.Name != aws_public_zone
-
-          with_items: "{{records.ResourceRecordSets }}"
-          ignore_errors: yes
-
-        - name: Remove public dns zone manually because sometimes it still has A records
-          route53_zone:
-            zone: "{{ aws_public_zone }}"
-            state: absent
-          ignore_errors: yes
+      loop_control:
+        loop_var: loopzone
 
     - name: Run infra-ec2-template-destroy
       include_role:

--
Gitblit v1.9.3