# Copyright (c) 2019 Dell Inc. or its subsidiaries. All Rights Reserved.
- name: Configure CAM egress
  block:
    - name: Get AWX statefulset (sts)
      shell: >
        kubectl get sts --namespace={{ awx_namespace }}
        --selector=app={{ awx_deployment_name }}
        --output=jsonpath='{.items..metadata.name}'
      register: awx_sts_name_output

    - name: Set AWX statefulset name
      set_fact:
        awx_sts_name: "{{ awx_sts_name_output.stdout }}"

    - name: Patch AWX statefulset
      shell: >
        kubectl -n {{ awx_namespace }} patch sts {{ awx_sts_name }}
        --patch '{"spec":{"template":{"metadata":{"annotations":
        {"traffic.sidecar.istio.io/excludeOutboundIPRanges": "{{ default_exclude_outbound_IP_ranges }}"}}}}}'

    - name: Get all AWX pods
      shell: >
        kubectl -n {{ awx_namespace }} get pods
        --output=jsonpath='{.items..metadata.name}'
      register: awx_pods_name_output


    - name: Create a list of all AWX pod names
      set_fact:
        awx_pod_names_list: "{{ awx_pods_name_output.stdout_lines[0].split(' ') }}"

    - name: Delete AWX pods
      k8s:
        state: absent
        api_version: v1
        kind: pod
        namespace: "{{ awx_namespace }}"
        name: "{{ item }}"
      with_items:
        "{{ awx_pod_names_list }}"

    - name: Add a delay to ensure all containers are up and running
      wait_for: timeout=300
