# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---

- name: Include provision_idrac vars
  include_vars: "{{ playbook_dir }}/../roles/provision_idrac/vars/main.yml"
  run_once: true

- name: Check tower_cli.cfg is encrypted
  command: cat "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
  changed_when: false
  register: awx_content
  run_once: true

- name: Decrpyt tower_cli.cfg
  command: >-
    ansible-vault decrypt "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
    --vault-password-file "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vaultname }}"
  changed_when: false
  run_once: true
  when: "'$ANSIBLE_VAULT;' in awx_content.stdout"

- name: Fetch awx host
  command: grep "host:" "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
  register: fetch_awx_host
  changed_when: false
  run_once: true

- name: Fetch awx username
  command: grep "username:" "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
  register: fetch_awx_username
  changed_when: false
  run_once: true
  no_log: true

- name: Fetch awx password
  command: grep "password:" "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
  register: fetch_awx_password
  changed_when: false
  run_once: true
  no_log: true

- name: Set awx variables
  set_fact:
    awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
    awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
    awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
  no_log: true

- name: Encrypt tower_cli.cfg
  command: >-
    ansible-vault encrypt "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vars_filename }}"
    --vault-password-file "{{ playbook_dir }}/../roles/webui_awx/files/{{ awx_vaultname }}"
  changed_when: false
  run_once: true
  when: "'$ANSIBLE_VAULT;' in awx_content.stdout"

- name: Get inventory list
  command: >-
    awx --conf.host "{{ awx_host }}" --conf.username "{{ awx_username }}" --conf.password "{{ awx_password }}"
    inventory list -f human --filter "name"
  register: inventory_list
  run_once: true
  changed_when: false
  no_log: true

- block:
    - name: Fetch provisioned_idrac inventory
      command: >-
        awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
        hosts list --inventory "{{ provisioned_idrac_inventory_name }}" -f human --filter "name"
      register: fetch_inventory
      run_once: true
      changed_when: false
      no_log: true
    
    - name: Launch ethernet_template
      command: >-
        awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
        job_templates launch {{ ethernet_template_name }} --wait
      run_once: true
      changed_when: true
      no_log: true

    - name: Wait for 10 mins for DHCP to assign IP to devices
      wait_for:
        timeout: "{{ dhcp_wait_time }}"

    - name: Launch idrac_template
      command: >-
        awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
        job_templates launch {{ idrac_template_name }} --wait
      run_once: true
      changed_when: false
      no_log: true
      when: inventory_hostname not in fetch_inventory.stdout
  when: provisioned_idrac_inventory_name in inventory_list.stdout
