Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/lookup/santricity_host.py

raw symbols SCIP
1 # (c) 2024, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import (absolute_import, division, print_function)
4 __metaclass__ = type
5
6 DOCUMENTATION = """
7 name: santricity_host
8 author:
9 - Nathan Swartz (@swartzn)
10 - Vu Tran (@VuTran007)
11 short_description: Collects host information
12 description:
13 - Collects current host, expected host and host group inventory definitions.
14 options:
15 inventory:
16 description:
17 - E-Series storage array inventory, hostvars[inventory_hostname].
18 - Run na_santricity_facts prior to calling
19 required: True
20 type: raw
21 volumes:
22 description:
23 - Volume information returned from santricity_volume lookup plugin which expands
24 type: raw
25 """
26 from ansible.errors import AnsibleError
27 from ansible.plugins.lookup import LookupBase
28
29
30 class LookupModule(LookupBase):
31
32 # pylint: disable=arguments-renamed
33 def run(self, inventory, volumes, **kwargs):
34 if isinstance(inventory, list):
35 inventory = inventory[0]
36
37 if not isinstance(volumes, list):
38 volumes = [volumes]
39
40 if ("eseries_storage_pool_configuration" not in inventory or not isinstance(inventory["eseries_storage_pool_configuration"], list) or
41 len(inventory["eseries_storage_pool_configuration"]) == 0):
42 return list()
43
44 if "eseries_storage_pool_configuration" not in inventory.keys():
45 raise AnsibleError("eseries_storage_pool_configuration must be defined. See nar_santricity_host role documentation.")
46
47 info = {"current_hosts": {}, "expected_hosts": {}, "host_groups": {}}
48
49 groups = []
50 hosts = []
51 non_inventory_hosts = []
52 non_inventory_groups = []
53 for group in inventory["groups"].keys():
54 groups.append(group)
55 hosts.extend(inventory["groups"][group])
56
57 if "eseries_host_object" in inventory.keys():
58 non_inventory_hosts = [host["name"] for host in inventory["eseries_host_object"]]
59 non_inventory_groups = [host["group"] for host in inventory["eseries_host_object"] if "group" in host]
60
61 for volume in volumes:
62 if volume["state"] == "present" and "host" in volume.keys():
63
64 if volume["host"] in groups:
65 # Add all expected group hosts
66 for expected_host in inventory["groups"][volume["host"]]:
67 if "host_type" in volume:
68 info["expected_hosts"].update({expected_host: {"state": "present",
69 "host_type": volume["host_type"],
70 "group": volume["host"]}})
71 else:
72 info["expected_hosts"].update({expected_host: {"state": "present",
73 "group": volume["host"]}})
74
75 info["host_groups"].update({volume["host"]: inventory["groups"][volume["host"]]})
76
77 elif volume["host"] in hosts:
78 if "host_type" in volume:
79 info["expected_hosts"].update({volume["host"]: {"state": "present",
80 "host_type": volume["host_type"],
81 "group": None}})
82 else:
83 info["expected_hosts"].update({volume["host"]: {"state": "present",
84 "group": None}})
85 elif volume["host"] not in non_inventory_hosts and volume["host"] not in non_inventory_groups:
86 raise AnsibleError("Expected host or host group does not exist in your Ansible inventory and is not specified in"
87 " eseries_host_object variable! [%s]." % volume["host"])
88
89 return [info]