# 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: Initialise host description
  set_fact:
    host_description: "Description Unavailable"
    
- name: Fetch description
  set_fact:
    host_description: "CPU:{{ hostvars[item]['ansible_processor_count'] }}
    Cores:{{ hostvars[item]['ansible_processor_cores'] }}
    Memory:{{ hostvars[item]['ansible_memtotal_mb'] }}MB
    BIOS:{{ hostvars[item]['ansible_bios_version'] }}"
  ignore_errors: yes

- name: Fetch the hosts in awx node inventory
  command: >-
    awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
    --conf.insecure hosts list --inventory node_inventory
  changed_when: false
  no_log: true
  when:
     - host_description != "Description Unavailable"
  register: hosts
  ignore_errors: yes
  
- name: Add the host to awx node inventory if not present
  command: >-
    awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
    --conf.insecure hosts create --name {{ item }} --inventory node_inventory
  changed_when: true
  when: item not in hosts.stdout
  no_log: true
  ignore_errors: yes

- name: Host added msg
  debug:
    msg: "{{ host_added_msg + item }}"
  when:
    - host_description != "Description Unavailable"