# Copyright 2020 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: Initialize reachable and unreachable host number
  set_fact:
    reachable_host_number: 0
    unreachable_host_number: 0
    unreachable_ssh_host_number: 0
    unreachable_ping_host_number: 0

- name: Set reachable host number
  set_fact:
    reachable_host_number: "{{ groups['reachable_ssh'] | length}}"
  when: groups['reachable_ssh'] is defined

- name: Set unreachable host number
  set_fact:
    unreachable_host_number: "{{ groups['ungrouped'] | length | int + groups['unreachable_ssh'] | length | int }}"
    unreachable_ssh_host_number: "{{ groups['unreachable_ssh'] | length }}"
    unreachable_ping_host_number: "{{ groups['ungrouped'] | length }}"
  when: groups['unreachable_ssh'] is defined

- name: Set unreachable host number
  set_fact:
    unreachable_host_number: "{{ groups['ungrouped'] | length }}"
    unreachable_ping_host_number: "{{ groups['ungrouped'] | length }}"
  when: groups['unreachable_ssh'] is not defined
            
- name: Create files folder
  file:
    path: "{{ role_path}}/files"
    state: directory
    
- name: Copy dhcpd.leases from cobbler
  command: docker cp cobbler:/var/lib/dhcpd/dhcpd.leases {{ role_path}}/files/dhcpd.leases
  changed_when: true

- name: Fetch ethernet details of unreachable hosts
  shell: sed -n '/{{ item }}/,/ethernet/p' {{ role_path}}/files/dhcpd.leases | grep "ethernet" | awk '{ print $3 }' | uniq
  register: ethernet_detail_unreachable_ping
  changed_when: false
  args:
    warn: no
  with_items:
    - "{{ groups['ungrouped'] }}"

- name: Fetch ethernet details of unreachable hosts
  shell: sed -n '/{{ item }}/,/ethernet/p' {{ role_path}}/files/dhcpd.leases | grep "ethernet" | awk '{ print $3 }' | uniq
  register: ethernet_detail_unreachable_ssh
  changed_when: false
  args:
    warn: no
  when: groups['unreachable_ssh'] is defined
  with_items:
    - "{{ groups['unreachable_ssh'] }}"

- name: Fetch ethernet details of reachable hosts
  shell: sed -n '/{{ item }}/,/ethernet/p' {{ role_path}}/files/dhcpd.leases | grep "ethernet" | awk '{ print $3 }' | uniq
  register: ethernet_detail_reachable
  changed_when: false
  args:
    warn: no
  when: groups['reachable_ssh'] is defined
  with_items:
    - "{{ groups['reachable_ssh'] }}"

- name: Copy host information to file
  template:
    src: provision_host_report.j2
    dest: "{{ role_path}}/files/provision_host_report.txt"

- name: Read provision host report
  command: cat {{ role_path}}/files/provision_host_report.txt
  register: host_report
  changed_when: false

- name: Display provision host report
  debug:
    var: host_report.stdout_lines