# 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: Include control_plane_common vars
  include_vars: "{{ playbook_dir }}/../roles/control_plane_common/vars/main.yml"
  run_once: true

- name: Include idrac_vars.yml
  include_vars: "{{ playbook_dir }}/../{{ idrac_input_filename }}"
  run_once: true

- name: Check hostname
  command: hostname
  changed_when: false
  register: hostname
  run_once: true

- name: Set idrac credentials
  set_fact:
    idrac_username: "{{ lookup('env','ANSIBLE_NET_USERNAME') }}"
    idrac_password: "{{ lookup('env','ANSIBLE_NET_PASSWORD') }}"
  no_log: true
  when: '"awx-" in hostname.stdout'
  run_once: true

- name: Fetch idrac credentials
  include_tasks: "{{ playbook_dir }}/../roles/provision_idrac/tasks/fetch_idrac_credentials.yml"
  vars:
    login_input_filename: "{{ playbook_dir }}/../{{ login_vars_filename }}"
    login_vault_filename: "{{ playbook_dir }}/../{{ vault_filename }}"
  when: '"awx-" not in hostname.stdout'

- name: Show status of the Lifecycle Controller
  dellemc.openmanage.idrac_lifecycle_controller_status_info:
    idrac_ip: "{{ inventory_hostname }}"
    idrac_user: "{{ idrac_username }}"
    idrac_password: "{{ idrac_password }}"
  register: lc_check_status

- name: LC not available
  fail:
    msg: "{{ lc_check_fail_msg }}"
  when: not lc_check_status.lc_status_info.LCReady
  register: lc_fail