Debian Codegraph @

ansible_collections/community/general/plugins/modules/lxca_nodes.py

raw
1 #!/usr/bin/python
2 # Copyright (c) Ansible project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 #
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 author:
10 - Naval Patel (@navalkp)
11 - Prashant Bhosale (@prabhosa)
12 module: lxca_nodes
13 short_description: Custom module for lxca nodes inventory utility
14 description:
15 - This module returns/displays a inventory details of nodes.
16 attributes:
17 check_mode:
18 support: none
19 diff_mode:
20 support: none
21
22 options:
23 uuid:
24 description: UUID of device, this is string with length greater than 16.
25 type: str
26
27 command_options:
28 description: Options to filter nodes information.
29 default: nodes
30 choices:
31 - nodes
32 - nodes_by_uuid
33 - nodes_by_chassis_uuid
34 - nodes_status_managed
35 - nodes_status_unmanaged
36 type: str
37
38 chassis:
39 description: UUID of chassis, this is string with length greater than 16.
40 type: str
41
42 extends_documentation_fragment:
43 - community.general._lxca_common
44 - community.general._attributes
45 """
46
47 EXAMPLES = r"""
48 # get all nodes info
49 - name: Get nodes data from LXCA
50 community.general.lxca_nodes:
51 login_user: USERID
52 login_password: Password
53 auth_url: "https://10.243.15.168"
54 command_options: nodes
55
56 # get specific nodes info by uuid
57 - name: Get nodes data from LXCA
58 community.general.lxca_nodes:
59 login_user: USERID
60 login_password: Password
61 auth_url: "https://10.243.15.168"
62 uuid: "3C737AA5E31640CE949B10C129A8B01F"
63 command_options: nodes_by_uuid
64
65 # get specific nodes info by chassis uuid
66 - name: Get nodes data from LXCA
67 community.general.lxca_nodes:
68 login_user: USERID
69 login_password: Password
70 auth_url: "https://10.243.15.168"
71 chassis: "3C737AA5E31640CE949B10C129A8B01F"
72 command_options: nodes_by_chassis_uuid
73
74 # get managed nodes
75 - name: Get nodes data from LXCA
76 community.general.lxca_nodes:
77 login_user: USERID
78 login_password: Password
79 auth_url: "https://10.243.15.168"
80 command_options: nodes_status_managed
81
82 # get unmanaged nodes
83 - name: Get nodes data from LXCA
84 community.general.lxca_nodes:
85 login_user: USERID
86 login_password: Password
87 auth_url: "https://10.243.15.168"
88 command_options: nodes_status_unmanaged
89 """
90
91 RETURN = r"""
92 result:
93 description: Nodes detail from lxca.
94 returned: always
95 type: dict
96 sample:
97 nodeList:
98 - machineType: '6241'
99 model: 'AC1'
100 type: 'Rack-TowerServer'
101 uuid: '118D2C88C8FD11E4947B6EAE8B4BDCDF'
102 # bunch of properties
103 - machineType: '8871'
104 model: 'AC1'
105 type: 'Rack-TowerServer'
106 uuid: '223D2C88C8FD11E4947B6EAE8B4BDCDF'
107 # bunch of properties
108 # Multiple nodes details
109 """
110
111 import traceback
112
113 from ansible.module_utils.basic import AnsibleModule
114
115 from ansible_collections.community.general.plugins.module_utils._lxca_common import (
116 LXCA_COMMON_ARGS,
117 connection_object,
118 has_pylxca,
119 )
120
121 try:
122 from pylxca import nodes
123 except ImportError:
124 pass
125
126
127 UUID_REQUIRED = "UUID of device is required for nodes_by_uuid command."
128 CHASSIS_UUID_REQUIRED = "UUID of chassis is required for nodes_by_chassis_uuid command."
129
130
131 def _nodes(module, lxca_con):
132 return nodes(lxca_con)
133
134
135 def _nodes_by_uuid(module, lxca_con):
136 if not module.params["uuid"]:
137 module.fail_json(msg=UUID_REQUIRED)
138 return nodes(lxca_con, module.params["uuid"])
139
140
141 def _nodes_by_chassis_uuid(module, lxca_con):
142 if not module.params["chassis"]:
143 module.fail_json(msg=CHASSIS_UUID_REQUIRED)
144 return nodes(lxca_con, chassis=module.params["chassis"])
145
146
147 def _nodes_status_managed(module, lxca_con):
148 return nodes(lxca_con, status="managed")
149
150
151 def _nodes_status_unmanaged(module, lxca_con):
152 return nodes(lxca_con, status="unmanaged")
153
154
155 def setup_module_object():
156 """
157 this function merge argument spec and create ansible module object
158 :return:
159 """
160 args_spec = dict(LXCA_COMMON_ARGS)
161 args_spec.update(INPUT_ARG_SPEC)
162 module = AnsibleModule(argument_spec=args_spec, supports_check_mode=False)
163
164 return module
165
166
167 FUNC_DICT = {
168 "nodes": _nodes,
169 "nodes_by_uuid": _nodes_by_uuid,
170 "nodes_by_chassis_uuid": _nodes_by_chassis_uuid,
171 "nodes_status_managed": _nodes_status_managed,
172 "nodes_status_unmanaged": _nodes_status_unmanaged,
173 }
174
175
176 INPUT_ARG_SPEC = dict(
177 command_options=dict(
178 default="nodes",
179 choices=["nodes", "nodes_by_uuid", "nodes_by_chassis_uuid", "nodes_status_managed", "nodes_status_unmanaged"],
180 ),
181 uuid=dict(),
182 chassis=dict(),
183 )
184
185
186 def execute_module(module):
187 """
188 This function invoke commands
189 :param module: Ansible module object
190 """
191 try:
192 with connection_object(module) as lxca_con:
193 result = FUNC_DICT[module.params["command_options"]](module, lxca_con)
194 module.exit_json(changed=False, msg=f"Success {module.params['command_options']} result", result=result)
195 except Exception as exception:
196 error_msg = "; ".join(exception.args)
197 module.fail_json(msg=error_msg, exception=traceback.format_exc())
198
199
200 def main():
201 module = setup_module_object()
202 has_pylxca(module)
203 execute_module(module)
204
205
206 if __name__ == "__main__":
207 main()