#  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: Perform validations
  include_tasks: validations.yml

- name: Fetch passwords
  include_tasks: fetch_password.yml

- name: Check if omnia is running from AWX
  block:
    - name: Initialize variables
      set_fact:
        control_plane_status: false
        powervault_status: false
        nfs_node_status: false

    - name: Check AWX instance
      command: awx --version
      changed_when: false
      failed_when: false
      register: awx_version_check

    - name: Check AWX hostname
      command: hostname
      changed_when: false
      register: awx_hostname

    - name: Set control_plane_status
      set_fact:
        control_plane_status: true
      when:
        - not awx_version_check.failed
        - '"awx-" in awx_hostname.stdout'

    - name: Set NFS node status
      set_fact:
        nfs_node_status: true
      when:
        - control_plane_status
        - groups['nfs_node'] | length == 1

    - name: Fetch powervault status
      include_tasks: fetch_powervault_status.yml
      when: nfs_node_status

- name: omnia.yml runing on host
  block:
    - name: Passwordless SSH status
      debug:
        msg: "omnia.yml running on host"

    - name: Check whether ansible config file exists
      stat:
        path: "{{ ansible_conf_file_path }}/ansible.cfg"
      register: ansible_conf_exists

    - name: Create the directory if it does not exist
      file:
        path: "{{ ansible_conf_file_path }}"
        state: directory
        mode: "{{ file_perm }}"
      when: not ansible_conf_exists.stat.exists

    - name: Create ansible config file if it does not exist
      copy:
        dest: "{{ ansible_conf_file_path }}/ansible.cfg"
        mode: "{{ file_perm }}"
        content: |
          [defaults]
          log_path = /var/log/omnia.log
      when: not ansible_conf_exists.stat.exists

    - name: Set omnia.log file
      replace:
        path: "{{ ansible_conf_file_path }}/ansible.cfg"
        regexp: '#log_path = /var/log/ansible.log'
        replace: 'log_path = /var/log/omnia.log'
      when: ansible_conf_exists.stat.exists
  when: not control_plane_status