---
- name: look for network software config
  shell: |
    grep "OS::TripleO::.*::Net::SoftwareConfig" {{ item }}
  ignore_errors: true
  register: nic_config_envs
  loop: "{{ initial_env_file.stdout_lines }}"

- name: set nic_config_env fact
  set_fact:
    nic_config_env: "{{ item.item }}"
  when: item.stdout|length > 0
  loop: "{{ nic_config_envs.results }}"

- block:
    - name: register nic templates files defined in the environment file
      shell: |
        set -o pipefail
        awk -F': ' '/OS::TripleO::.*::Net::SoftwareConfig/ {print $2}' {{ nic_config_env }}
      register: nic_template_files

    - name: adjust nic templates in absolute paths location
      shell: |
        set -o pipefail
        sed -i -E "s/^[ ]{0,}#.*//" {{ item }}
        echo y | /usr/share/openstack-tripleo-heat-templates/tools/yaml-nic-config-2-script.py {{ item }}
      when:
        - "item[0] == '/'"
        - "tht_directory not in item"
      loop: "{{ nic_template_files.stdout_lines }}"

    - name: adjust nic templates in relative paths location
      shell: |
        set -o pipefail
        sed -i -E "s/^[ ]{0,}#.*//" {{ '/'.join(nic_config_env.split('/')[0:-1]) }}/{{ item }}
        echo y | /usr/share/openstack-tripleo-heat-templates/tools/yaml-nic-config-2-script.py \
            {{ '/'.join(nic_config_env.split('/')[0:-1]) }}/{{ item }}
      when:
        - "item[0] != '/'"
        - "tht_directory not in '/'.join(nic_config_env.split('/')[0:-1])"
      loop: "{{ nic_template_files.stdout_lines }}"

    - name: Register roles data file location if exists
      shell: "grep '\\-r\\ \\|\\-\\-roles' {{ overcloud_deploy_script }} | awk {'print $2'}"
      register: custom_roles_file
      ignore_errors: true

    - block:
        - name: Make a copy of the custom roles data file
          copy:
            src: "{{ custom_roles_file.stdout }}"
            dest: "{{ custom_roles_file.stdout }}.networks_update"
            remote_src: true

        - name: Get roles
          shell: |
            awk -F': ' '/ name:/ {print $2}' {{ custom_roles_file.stdout }}
          register: roles

        - name: Generate head of the network update script
          shell: |
            echo "{{ json_dump }} < {{ custom_roles_file.stdout }}.networks_update {{ escape }}" > update_networks.sh

        - name: Generate the network update script
          shell: |
            grep get_param $( awk -F': ' '/OS::TripleO::{{ item }}::Net::SoftwareConfig/ {print $2}' {{ nic_config_env }}) |\
            grep -o -e InternalApiIpSubnet -e StorageIpSubnet -e StorageMgmtIpSubnet -e TenantIpSubnet -e ExternalIpSubnet -e ManagementIpSubnet |\
            while read network ; do
              echo "jq \"map( if .name | contains (\\\"{{ item }}\\\")
                              then .networks |= (. += [\\\"${network/%IpSubnet}\\\"] | unique)
                              else . end)\" {{ escape }}" >> update_networks.sh
            done
          with_items: "{{ roles.stdout_lines }}"

        - name: Generate tail of the network update script
          shell: |
            echo "{{ yaml_dump }} > {{ custom_roles_file.stdout }}" >> update_networks.sh

        - name: Apply the network update script
          shell: bash update_networks.sh

      when: custom_roles_file.stdout|length  > 0
      vars:
        escape: '|\\'
        json_dump: "python -c \\\"import sys, yaml, simplejson as json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)\\\""
        yaml_dump: "python -c \\\"import simplejson, sys, yaml; print yaml.dump(simplejson.loads(str(sys.stdin.read())), default_flow_style=False)\\\""
  when: nic_config_env is defined
