# 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: Get the hosts in node_inventory
  command: >-
    awx --conf.host {{ awx_host }} --conf.username {{ awx_admin_user }} --conf.password {{ awx_password }}
    --conf.insecure hosts list --inventory {{ node_inventory }} -f human --filter "name"
  changed_when: false
  no_log: true
  register: hosts_list

- name: Add the host to the group in node_inventory if present
  awx.awx.tower_group:
    name: "{{ item.split(',')[3] }}"
    inventory: "{{ node_inventory }}"
    hosts:
      - "{{ item.split(',')[2] }}"
    tower_config_file: "{{ tower_config_file }}"
  when:
    - item.split(',')[2] != "IP"
    - item.split(',')[2] in hosts_list.stdout