Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # Copyright (c) 2021-2022 Hewlett Packard Enterprise, Inc. All rights reserved.
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 from __future__ import annotations
6
7 DOCUMENTATION = r"""
8 module: ilo_redfish_config
9 short_description: Sets or updates configuration attributes on HPE iLO with Redfish OEM extensions
10 version_added: 4.2.0
11 description:
12 - Builds Redfish URIs locally and sends them to iLO to set or update a configuration attribute.
13 - For use with HPE iLO operations that require Redfish OEM extensions.
14 extends_documentation_fragment:
15 - community.general._attributes
16 - community.general._redfish
17 attributes:
18 check_mode:
19 support: none
20 diff_mode:
21 support: none
22 options:
23 category:
24 required: true
25 type: str
26 description:
27 - Command category to execute on iLO.
28 choices: ['Manager']
29 command:
30 required: true
31 description:
32 - List of commands to execute on iLO.
33 type: list
34 elements: str
35 baseuri:
36 required: true
37 description:
38 - Base URI of iLO.
39 type: str
40 username:
41 description:
42 - Username for authenticating to iLO.
43 type: str
44 password:
45 description:
46 - Password for authenticating to iLO.
47 type: str
48 auth_token:
49 description:
50 - Security token for authenticating to iLO.
51 type: str
52 timeout:
53 description:
54 - Timeout in seconds for HTTP requests to iLO.
55 default: 10
56 type: int
57 attribute_name:
58 required: true
59 description:
60 - Name of the attribute to be configured.
61 type: str
62 attribute_value:
63 description:
64 - Value of the attribute to be configured.
65 type: str
66 validate_certs:
67 version_added: 10.6.0
68 ca_path:
69 version_added: 10.6.0
70 ciphers:
71 version_added: 10.6.0
72 author:
73 - "Bhavya B (@bhavya06)"
74 """
75
76 EXAMPLES = r"""
77 - name: Disable WINS Registration
78 community.general.ilo_redfish_config:
79 category: Manager
80 command: SetWINSReg
81 baseuri: 15.X.X.X
82 username: Admin
83 password: Testpass123
84 attribute_name: WINSRegistration
85
86 - name: Set Time Zone
87 community.general.ilo_redfish_config:
88 category: Manager
89 command: SetTimeZone
90 baseuri: 15.X.X.X
91 username: Admin
92 password: Testpass123
93 attribute_name: TimeZone
94 attribute_value: Chennai
95
96 - name: Set NTP Servers
97 community.general.ilo_redfish_config:
98 category: Manager
99 command: SetNTPServers
100 baseuri: 15.X.X.X
101 username: Admin
102 password: Testpass123
103 attribute_name: StaticNTPServers
104 attribute_value: X.X.X.X
105 """
106
107 RETURN = r"""
108 msg:
109 description: Message with action result or error description.
110 returned: always
111 type: str
112 sample: "Action was successful"
113 """
114
115 CATEGORY_COMMANDS_ALL = {"Manager": ["SetTimeZone", "SetDNSserver", "SetDomainName", "SetNTPServers", "SetWINSReg"]}
116
117 from ansible.module_utils.basic import AnsibleModule
118
119 from ansible_collections.community.general.plugins.module_utils._ilo_redfish_utils import iLORedfishUtils
120 from ansible_collections.community.general.plugins.module_utils._redfish_utils import REDFISH_COMMON_ARGUMENT_SPEC
121
122
123 def main():
124 result = {}
125 argument_spec = dict(
126 category=dict(required=True, choices=list(CATEGORY_COMMANDS_ALL.keys())),
127 command=dict(required=True, type="list", elements="str"),
128 baseuri=dict(required=True),
129 username=dict(),
130 password=dict(no_log=True),
131 auth_token=dict(no_log=True),
132 attribute_name=dict(required=True),
133 attribute_value=dict(type="str"),
134 timeout=dict(type="int", default=10),
135 )
136 argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
137 module = AnsibleModule(
138 argument_spec,
139 required_together=[
140 ("username", "password"),
141 ],
142 required_one_of=[
143 ("username", "auth_token"),
144 ],
145 mutually_exclusive=[
146 ("username", "auth_token"),
147 ],
148 supports_check_mode=False,
149 )
150
151 category = module.params["category"]
152 command_list = module.params["command"]
153
154 creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
155
156 timeout = module.params["timeout"]
157
158 root_uri = f"https://{module.params['baseuri']}"
159 rf_utils = iLORedfishUtils(creds, root_uri, timeout, module)
160 mgr_attributes = {
161 "mgr_attr_name": module.params["attribute_name"],
162 "mgr_attr_value": module.params["attribute_value"],
163 }
164 changed = False
165
166 offending = [cmd for cmd in command_list if cmd not in CATEGORY_COMMANDS_ALL[category]]
167
168 if offending:
169 module.fail_json(msg=f"Invalid Command(s): '{offending}'. Allowed Commands = {CATEGORY_COMMANDS_ALL[category]}")
170
171 if category == "Manager":
172 resource = rf_utils._find_managers_resource()
173 if not resource["ret"]:
174 module.fail_json(msg=resource["msg"])
175
176 dispatch = dict(
177 SetTimeZone=rf_utils.set_time_zone,
178 SetDNSserver=rf_utils.set_dns_server,
179 SetDomainName=rf_utils.set_domain_name,
180 SetNTPServers=rf_utils.set_ntp_server,
181 SetWINSReg=rf_utils.set_wins_registration,
182 )
183
184 for command in command_list:
185 result[command] = dispatch[command](mgr_attributes)
186 if "changed" in result[command]:
187 changed |= result[command]["changed"]
188
189 module.exit_json(ilo_redfish_config=result, changed=changed)
190
191
192 if __name__ == "__main__":
193 main()