Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_system_banner.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Tim Cragg (@timcragg) <[email protected]>
5 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_system_banner
17 short_description: Manages Alias and Banners (aaa:PreLoginBanner)
18 description:
19 - Manages Alias and Banners on Cisco ACI fabrics.
20 options:
21 description:
22 description:
23 - The description of the AAA login banner.
24 type: str
25 gui_alias:
26 description:
27 - The system GUI alias.
28 type: str
29 controller_banner:
30 description:
31 - The contents of the CLI informational banner to be displayed before user login authentication.
32 - The CLI banner is a text based string printed as-is to the console.
33 type: str
34 switch_banner:
35 description:
36 - The switch banner message.
37 type: str
38 application_banner:
39 description:
40 - The application banner message.
41 type: str
42 severity:
43 description:
44 - The application banner severity.
45 type: str
46 choices: [ critical, info, major, minor, warning ]
47 gui_banner:
48 description:
49 - The contents of the GUI informational banner to be displayed before user login authentication.
50 - When I(gui_banner) starts with I(http://) or I(https://) the banner will be of URL type.
51 - Note that the URL site owner must allow the site to be placed in an iFrame to display the informational banner.
52 type: str
53 state:
54 description:
55 - Use C(present) for updating.
56 - Use C(query) for listing an object.
57 type: str
58 choices: [ present, query ]
59 default: present
60 name_alias:
61 description:
62 - The alias for the current object. This relates to the nameAlias field in ACI.
63 type: str
64 extends_documentation_fragment:
65 - cisco.aci.aci
66 - cisco.aci.annotation
67 - cisco.aci.owner
68
69 seealso:
70 - name: APIC Management Information Model reference
71 description: More information about the internal APIC class B(aaa:PreLoginBanner).
72 link: https://developer.cisco.com/docs/apic-mim-ref/
73
74 author:
75 - Tim Cragg (@timcragg)
76 - Akini Ross (@akinross)
77 """
78
79 EXAMPLES = r"""
80 - name: Configure banner
81 cisco.aci.aci_system_banner:
82 host: apic
83 username: admin
84 password: SomeSecretPassword
85 gui_alias: "Test GUI Alias"
86 controller_banner: "Test Controller Banner"
87 application_banner: "Test Application Banner"
88 severity: critical
89 switch_banner: "Test Switch Banner"
90 gui_banner: "Test GUI Banner"
91 state: present
92 delegate_to: localhost
93
94 - name: Configure banner with a url
95 cisco.aci.aci_system_banner:
96 host: apic
97 username: admin
98 password: SomeSecretPassword
99 gui_alias: "Test GUI Alias"
100 controller_banner: "Test Controller Banner Message"
101 application_banner: "Test Application Banner"
102 severity: critical
103 switch_banner: "Test Switch Banner Message"
104 gui_banner: https://www.cisco.com
105 state: present
106 delegate_to: localhost
107
108 - name: Query banner
109 cisco.aci.aci_system_banner:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 state: query
114 delegate_to: localhost
115 """
116
117 RETURN = r"""
118 current:
119 description: The existing configuration from the APIC after the module has finished
120 returned: success
121 type: list
122 sample:
123 [
124 {
125 "fvTenant": {
126 "attributes": {
127 "descr": "Production environment",
128 "dn": "uni/tn-production",
129 "name": "production",
130 "nameAlias": "",
131 "ownerKey": "",
132 "ownerTag": ""
133 }
134 }
135 }
136 ]
137 error:
138 description: The error information as returned from the APIC
139 returned: failure
140 type: dict
141 sample:
142 {
143 "code": "122",
144 "text": "unknown managed object class foo"
145 }
146 raw:
147 description: The raw output returned by the APIC REST API (xml or json)
148 returned: parse error
149 type: str
150 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
151 sent:
152 description: The actual/minimal configuration pushed to the APIC
153 returned: info
154 type: list
155 sample:
156 {
157 "fvTenant": {
158 "attributes": {
159 "descr": "Production environment"
160 }
161 }
162 }
163 previous:
164 description: The original configuration from the APIC before the module has started
165 returned: info
166 type: list
167 sample:
168 [
169 {
170 "fvTenant": {
171 "attributes": {
172 "descr": "Production",
173 "dn": "uni/tn-production",
174 "name": "production",
175 "nameAlias": "",
176 "ownerKey": "",
177 "ownerTag": ""
178 }
179 }
180 }
181 ]
182 proposed:
183 description: The assembled configuration from the user-provided parameters
184 returned: info
185 type: dict
186 sample:
187 {
188 "fvTenant": {
189 "attributes": {
190 "descr": "Production environment",
191 "name": "production"
192 }
193 }
194 }
195 filter_string:
196 description: The filter string used for the request
197 returned: failure or debug
198 type: str
199 sample: ?rsp-prop-include=config-only
200 method:
201 description: The HTTP method used for the request to the APIC
202 returned: failure or debug
203 type: str
204 sample: POST
205 response:
206 description: The HTTP response from the APIC
207 returned: failure or debug
208 type: str
209 sample: OK (30 bytes)
210 status:
211 description: The HTTP status from the APIC
212 returned: failure or debug
213 type: int
214 sample: 200
215 url:
216 description: The HTTP url used for the request to the APIC
217 returned: failure or debug
218 type: str
219 sample: https://10.11.12.13/api/mo/uni/tn-production.json
220 """
221
222
223 import re
224 from ansible.module_utils.basic import AnsibleModule
225 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
226
227
228 def main():
229 argument_spec = aci_argument_spec()
230 argument_spec.update(aci_annotation_spec())
231 argument_spec.update(aci_owner_spec())
232 argument_spec.update(
233 description=dict(type="str"),
234 gui_alias=dict(type="str"),
235 controller_banner=dict(type="str"),
236 switch_banner=dict(type="str"),
237 application_banner=dict(type="str"),
238 severity=dict(type="str", choices=["critical", "info", "major", "minor", "warning"]),
239 gui_banner=dict(type="str"),
240 name_alias=dict(type="str"),
241 state=dict(type="str", default="present", choices=["present", "query"]),
242 )
243
244 module = AnsibleModule(
245 argument_spec=argument_spec,
246 supports_check_mode=True,
247 required_by={"severity": "application_banner"},
248 )
249 aci = ACIModule(module)
250
251 description = module.params.get("description")
252 gui_alias = module.params.get("gui_alias")
253 controller_banner = module.params.get("controller_banner")
254 switch_banner = module.params.get("switch_banner")
255 application_banner = module.params.get("application_banner")
256 severity = module.params.get("severity")
257 gui_banner = module.params.get("gui_banner")
258 state = module.params.get("state")
259 name_alias = module.params.get("name_alias")
260
261 aci.construct_url(
262 root_class=dict(
263 aci_class="aaaPreLoginBanner",
264 aci_rn="userext/preloginbanner",
265 ),
266 )
267 aci.get_existing()
268
269 if state == "present":
270 regex_url = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$"
271
272 if gui_banner is not None and re.fullmatch(regex_url, gui_banner):
273 is_gui_message_text = "no"
274 else:
275 is_gui_message_text = "yes"
276
277 aci.payload(
278 aci_class="aaaPreLoginBanner",
279 class_config=dict(
280 descr=description,
281 bannerMessage=application_banner,
282 bannerMessageSeverity=severity,
283 guiMessage=gui_banner,
284 guiTextMessage=gui_alias,
285 isGuiMessageText=is_gui_message_text,
286 message=controller_banner,
287 showBannerMessage="yes" if application_banner else "no",
288 switchMessage=switch_banner,
289 nameAlias=name_alias,
290 ),
291 )
292
293 aci.get_diff(aci_class="aaaPreLoginBanner")
294
295 aci.post_config()
296
297 aci.exit_json()
298
299
300 if __name__ == "__main__":
301 main()