
#  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
  include_tasks: "{{ playbook_dir }}/control_plane/roles/powervault_me4/tasks/ports.yml"

- name: Refresh ssh keys
  command: ssh-keygen -R {{ groups['powervault_me4'][0] }}
  changed_when: false
  tags: install
  failed_when: false

- name: Validate authentication of username and password
  command: ping -c1 {{ groups['powervault_me4'][0] }}
  register: validate_login
  changed_when: false
  failed_when: false

- name: Install packages
  package:
    name: iscsi-initiator-utils
    state: present
  tags: install

- name: Install packages
  package:
    name: sg3_utils
    state: present
  tags: install

- name: Set bootproto value
  lineinfile:
    path: "{{ nic_path }}"
    regexp: '^BOOTPROTO='
    line: 'BOOTPROTO=none'
  register: result

- name: Set onboot value
  lineinfile:
    path: "{{ nic_path }}"
    regexp: '^ONBOOT='
    line: 'ONBOOT=yes'

- name: Add ip address
  lineinfile:
    path: "{{ nic_path }}"
    insertafter: '^ONBOOT=yes'
    line: 'IPADDR={{ pv_nic_ip }}'

- name: Add netmask address
  lineinfile:
    path: "{{ nic_path }}"
    insertafter: '^IPADDR={{ pv_nic_ip }}'
    line: NETMASK=255.255.255.0

- name: Down the nic
  command: ifdown {{ pv_nic }}
  changed_when: true
  failed_when: false
  tags: install

- name: Up the nic
  command: ifup {{ pv_nic }}
  changed_when: true
  tags: install

- name: Show ip
  shell: >
    set -o pipefail && \
    ifconfig {{ pv_nic }} | grep 'inet' |cut -d: -f2 |  awk '{ print $2}'
  changed_when: false

- name: Discover nodes
  command: iscsiadm -m discovery -t sendtargets -p {{ item }}
  with_items: "{{ set_port_ip }}"
  register: ports_available
  failed_when: false
  changed_when: false
  tags: install

- name: Pv port ip
  add_host:
    name: pv
    map_ip: "{{ item.item }}"
  with_items: "{{ ports_available.results }}"
  when: item.rc == 0

- name: Pv port ip
  set_fact:
    map_ip_output: "{{ item.stdout_lines }}"
  with_items: "{{ ports_available.results }}"
  when: item.rc == 0

- name: Find feasible port ip
  set_fact:
    discover: "{{ item }}"
  with_items: "{{ map_ip_output }}"
  when: hostvars['pv']['map_ip'] in item

- name: Split on comma
  set_fact:
    ip_port: "{{ discover.split(',')[0] }}"

- name: Pv name
  set_fact:
    pv_name: "{{ discover.split(',')[1].split()[1] }}"

- name: IQDN id
  shell: >
    set -o pipefail && \
    cat /etc/iscsi/initiatorname.iscsi | cut -f2 -d"="
  register: iqdn_id
  changed_when: false
  tags: install

- name: Add ME4 volume data to dummy host
  add_host:
    name:   "server_iqdn_id"
    server_iqdn: "{{ iqdn_id.stdout }}"

- name: Login to the powervault
  command: iscsiadm -m node --login {{ pv_name }} -p {{ ip_port }}
  changed_when: true
  tags: install