| 1 |
|
| 2 |
|
| 3 |
from __future__ import (absolute_import, division, print_function) |
| 4 |
__metaclass__ = type |
| 5 |
|
| 6 |
DOCUMENTATION = """ |
| 7 |
name: santricity_host_detail |
| 8 |
author: |
| 9 |
- Nathan Swartz (@swartzn) |
| 10 |
- Vu Tran (@VuTran007) |
| 11 |
short_description: Expands the host information from santricity_host lookup |
| 12 |
description: |
| 13 |
- Expands the host information from santricity_host lookup to include system and port information |
| 14 |
options: |
| 15 |
hosts: |
| 16 |
description: |
| 17 |
- E-Series storage array inventory, hostvars[inventory_hostname]. |
| 18 |
- Run na_santricity_facts prior to calling |
| 19 |
required: True |
| 20 |
type: list |
| 21 |
elements: raw |
| 22 |
hosts_info: |
| 23 |
description: |
| 24 |
- The registered results from the setup module from each expected_hosts, hosts_info['results']. |
| 25 |
- Collected results from the setup module for each expected_hosts from the results of the santricity_host lookup plugin. |
| 26 |
required: True |
| 27 |
type: list |
| 28 |
elements: raw |
| 29 |
host_interface_ports: |
| 30 |
description: |
| 31 |
- List of dictionaries containing "stdout_lines" which is a list of iqn/wwpns for each expected_hosts from the results of |
| 32 |
the santricity_host lookup plugin. |
| 33 |
- Register the results from the shell module that is looped over each host in expected_hosts. The command issued should result |
| 34 |
in a newline delineated list of iqns, nqns, or wwpns. |
| 35 |
required: True |
| 36 |
type: list |
| 37 |
elements: raw |
| 38 |
protocol: |
| 39 |
description: |
| 40 |
- Storage system interface protocol (iscsi, sas, fc, ib-iser, ib-srp, nvme_ib, nvme_fc, or nvme_roce) |
| 41 |
required: True |
| 42 |
type: str |
| 43 |
|
| 44 |
""" |
| 45 |
import re |
| 46 |
from ansible.errors import AnsibleError |
| 47 |
from ansible.plugins.lookup import LookupBase |
| 48 |
|
| 49 |
|
| 50 |
class LookupModule(LookupBase): |
| 51 |
def run(self, hosts, hosts_info, host_interface_ports, protocol, **kwargs): |
| 52 |
if isinstance(hosts, list): |
| 53 |
hosts = hosts[0] |
| 54 |
|
| 55 |
if "expected_hosts" not in hosts: |
| 56 |
raise AnsibleError("Invalid argument: hosts must contain the output from santricity_host lookup plugin.") |
| 57 |
if not isinstance(hosts_info, list): |
| 58 |
raise AnsibleError("Invalid argument: hosts_info must contain the results from the setup module for each" |
| 59 |
" expected_hosts found in the output of the santricity_host lookup plugin.") |
| 60 |
if not isinstance(host_interface_ports, list): |
| 61 |
raise AnsibleError("Invalid argument: host_interface_ports must contain list of dictionaries containing 'stdout_lines' key" |
| 62 |
" which is a list of iqns, nqns, or wwpns for each expected_hosts from the results of the santricity_host lookup plugin") |
| 63 |
if protocol not in ["iscsi", "sas", "fc", "ib_iser", "ib_srp", "nvme_ib", "nvme_fc", "nvme_roce"]: |
| 64 |
raise AnsibleError("Invalid argument: protocol must one of the following: iscsi, sas, fc, ib_iser, ib_srp, nvme_ib, nvme_fc, nvme_roce.") |
| 65 |
|
| 66 |
for host in hosts["expected_hosts"].keys(): |
| 67 |
sanitized_hostname = re.sub("[.:-]", "_", host)[:20] |
| 68 |
|
| 69 |
|
| 70 |
for info in hosts_info: |
| 71 |
if info["item"] == host: |
| 72 |
|
| 73 |
|
| 74 |
if "host_type" not in hosts["expected_hosts"][host].keys(): |
| 75 |
if info["ansible_facts"]["ansible_os_family"].lower() == "windows": |
| 76 |
hosts["expected_hosts"][host]["host_type"] = "windows" |
| 77 |
elif info["ansible_facts"]["ansible_os_family"].lower() in ["redhat", "debian", "suse"]: |
| 78 |
hosts["expected_hosts"][host]["host_type"] = "linux dm-mp" |
| 79 |
|
| 80 |
|
| 81 |
hosts["expected_hosts"][host].update({"sanitized_hostname": sanitized_hostname, "ports": []}) |
| 82 |
|
| 83 |
|
| 84 |
for interface in host_interface_ports: |
| 85 |
if interface["item"] == host and "stdout_lines" in interface.keys(): |
| 86 |
if protocol == "sas": |
| 87 |
for index, address in enumerate([base[:-1] + str(index) for base in interface["stdout_lines"] for index in range(8)]): |
| 88 |
label = "%s_%s" % (sanitized_hostname, index) |
| 89 |
hosts["expected_hosts"][host]["ports"].append({"type": "sas", "label": label, "port": address}) |
| 90 |
elif protocol == "ib_iser" or protocol == "ib_srp": |
| 91 |
for index, address in enumerate(interface["stdout_lines"]): |
| 92 |
label = "%s_%s" % (sanitized_hostname, index) |
| 93 |
hosts["expected_hosts"][host]["ports"].append({"type": "ib", "label": label, "port": address}) |
| 94 |
elif protocol == "nvme_ib": |
| 95 |
for index, address in enumerate(interface["stdout_lines"]): |
| 96 |
label = "%s_%s" % (sanitized_hostname, index) |
| 97 |
hosts["expected_hosts"][host]["ports"].append({"type": "nvmeof", "label": label, "port": address}) |
| 98 |
elif protocol == "nvme_fc": |
| 99 |
for index, address in enumerate(interface["stdout_lines"]): |
| 100 |
label = "%s_%s" % (sanitized_hostname, index) |
| 101 |
hosts["expected_hosts"][host]["ports"].append({"type": "nvmeof", "label": label, "port": address}) |
| 102 |
elif protocol == "nvme_roce": |
| 103 |
for index, address in enumerate(interface["stdout_lines"]): |
| 104 |
label = "%s_%s" % (sanitized_hostname, index) |
| 105 |
hosts["expected_hosts"][host]["ports"].append({"type": "nvmeof", "label": label, "port": address}) |
| 106 |
else: |
| 107 |
for index, address in enumerate(interface["stdout_lines"]): |
| 108 |
label = "%s_%s" % (sanitized_hostname, index) |
| 109 |
hosts["expected_hosts"][host]["ports"].append({"type": protocol, "label": label, "port": address}) |
| 110 |
|
| 111 |
return [hosts] |