Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Tim Cragg (@timcragg)
5 # Copyright: (c) 2025, Dev Sinha (@DevSinha13)
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_syslog_group
17 short_description: Manage Syslog groups (syslog:Group, syslog:Console, syslog:File and syslog:Prof)
18 description:
19 - Manage syslog groups.
20 options:
21 admin_state:
22 description:
23 - Administrative state of the syslog group.
24 type: str
25 choices: [ enabled, disabled ]
26 console_logging:
27 description:
28 - Log events to console.
29 type: str
30 choices: [ enabled, disabled ]
31 console_log_severity:
32 description:
33 - Severity of events to log to console.
34 - If unset during creation, value defaults to C(alerts).
35 type: str
36 choices: [ alerts, critical, emergencies]
37 local_file_logging:
38 description:
39 - Log to local file.
40 type: str
41 choices: [ enabled, disabled ]
42 local_file_log_severity:
43 description:
44 - Severity of events to log to local file.
45 - If unset during creation, value defaults to C(alerts).
46 type: str
47 choices: [ alerts, critical, debugging, emergencies, error, information, notifications, warnings ]
48 format:
49 description:
50 - Format of the syslog messages.
51 - If unset during creation the value defaults to C(aci).
52 - C(rfc5424_ts) is only available starting from ACI version 5.2(8).
53 - C(enhanced_log) is only available starting from ACI version 6.0(9d) and is an alias to C(rfc5424_ts).
54 type: str
55 choices: [ aci, nxos, rfc5424_ts, enhanced_log ]
56 local_file_log_format:
57 description:
58 - The format of the local file log messages.
59 - If unset during creation and O(format) is provided then it is set to the same value as format. If O(format) is not provided it is set to C(aci).
60 - C(rfc5424_ts) is only available starting from ACI version 5.2(8).
61 - C(enhanced_log) is only available starting from ACI version 6.0(9d) and is an alias to C(rfc5424_ts).
62 type: str
63 choices: [ aci, nxos, rfc5424_ts, enhanced_log ]
64 console_log_format:
65 description:
66 - Format of the console log messages.
67 - If unset during creation and O(format) is provided then it is set to the same value as format. If O(format) is not provided it is set to C(aci).
68 - The option C(rfc5424_ts) is only available in ACI version 5.2(8) or later.
69 - The option C(enhanced_log) is only available in ACI version 6.0(9d) or later and is an alias to C(rfc5424_ts).
70 type: str
71 choices: [ aci, nxos, rfc5424_ts, enhanced_log ]
72 include_ms:
73 description:
74 - Include milliseconds in log timestamps.
75 type: bool
76 include_time_zone:
77 description:
78 - Include timezone in log timestamps.
79 type: bool
80 name:
81 description:
82 - Name of the syslog group.
83 type: str
84 aliases: [ syslog_group, syslog_group_name ]
85 description:
86 description:
87 - Description for the syslog group.
88 type: str
89 aliases: [ descr ]
90 state:
91 description:
92 - Use C(present) or C(absent) for adding or removing.
93 - Use C(query) for listing an object or multiple objects.
94 type: str
95 choices: [ absent, present, query ]
96 default: present
97 extends_documentation_fragment:
98 - cisco.aci.aci
99 - cisco.aci.annotation
100
101 seealso:
102 - name: APIC Management Information Model reference
103 description: More information about the internal APIC classes B(syslog:Group), B(syslog:Console), B(syslog:File) and B(syslog:Prof).
104 link: https://developer.cisco.com/docs/apic-mim-ref/
105 author:
106 - Tim Cragg (@timcragg)
107 - Dev Sinha (@DevSinha13)
108 """
109
110 EXAMPLES = r"""
111 - name: Create a syslog group
112 cisco.aci.aci_syslog_group:
113 host: apic
114 username: admin
115 password: SomeSecretPassword
116 name: my_syslog_group
117 local_file_logging: enabled
118 local_file_log_severity: warnings
119 console_logging: enabled
120 console_log_severity: critical
121 description: syslog group
122 state: present
123 delegate_to: localhost
124
125 - name: Create a syslog group with local_file_log_format and console_log_format
126 cisco.aci.aci_syslog_group:
127 host: apic
128 username: admin
129 password: SomeSecretPassword
130 format: aci
131 name: my_syslog_group
132 local_file_logging: enabled
133 local_file_log_severity: warnings
134 console_logging: enabled
135 console_log_severity: critical
136 local_file_log_format: rfc5424_ts
137 console_log_format: rfc5424_ts
138 description: syslog group
139 state: present
140
141
142 - name: Disable logging to local file
143 cisco.aci.aci_syslog_group:
144 host: apic
145 username: admin
146 password: SomeSecretPassword
147 name: my_syslog_group
148 local_file_logging: disabled
149 state: present
150 delegate_to: localhost
151
152 - name: Remove a syslog group
153 cisco.aci.aci_syslog_group:
154 host: apic
155 username: admin
156 password: SomeSecretPassword
157 name: my_syslog_group
158 state: absent
159 delegate_to: localhost
160
161 - name: Query a syslog group
162 cisco.aci.aci_syslog_group:
163 host: apic
164 username: admin
165 password: SomeSecretPassword
166 name: my_syslog_group
167 state: query
168 delegate_to: localhost
169 register: query_result
170
171 - name: Query all syslog groups
172 cisco.aci.aci_syslog_group:
173 host: apic
174 username: admin
175 password: SomeSecretPassword
176 state: query
177 delegate_to: localhost
178 register: query_result
179 """
180
181 RETURN = r"""
182 current:
183 description: The existing configuration from the APIC after the module has finished
184 returned: success
185 type: list
186 sample:
187 [
188 {
189 "fvTenant": {
190 "attributes": {
191 "descr": "Production environment",
192 "dn": "uni/tn-production",
193 "name": "production",
194 "nameAlias": "",
195 "ownerKey": "",
196 "ownerTag": ""
197 }
198 }
199 }
200 ]
201 error:
202 description: The error information as returned from the APIC
203 returned: failure
204 type: dict
205 sample:
206 {
207 "code": "122",
208 "text": "unknown managed object class foo"
209 }
210 raw:
211 description: The raw output returned by the APIC REST API (xml or json)
212 returned: parse error
213 type: str
214 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
215 sent:
216 description: The actual/minimal configuration pushed to the APIC
217 returned: info
218 type: list
219 sample:
220 {
221 "fvTenant": {
222 "attributes": {
223 "descr": "Production environment"
224 }
225 }
226 }
227 previous:
228 description: The original configuration from the APIC before the module has started
229 returned: info
230 type: list
231 sample:
232 [
233 {
234 "fvTenant": {
235 "attributes": {
236 "descr": "Production",
237 "dn": "uni/tn-production",
238 "name": "production",
239 "nameAlias": "",
240 "ownerKey": "",
241 "ownerTag": ""
242 }
243 }
244 }
245 ]
246 proposed:
247 description: The assembled configuration from the user-provided parameters
248 returned: info
249 type: dict
250 sample:
251 {
252 "fvTenant": {
253 "attributes": {
254 "descr": "Production environment",
255 "name": "production"
256 }
257 }
258 }
259 filter_string:
260 description: The filter string used for the request
261 returned: failure or debug
262 type: str
263 sample: ?rsp-prop-include=config-only
264 method:
265 description: The HTTP method used for the request to the APIC
266 returned: failure or debug
267 type: str
268 sample: POST
269 response:
270 description: The HTTP response from the APIC
271 returned: failure or debug
272 type: str
273 sample: OK (30 bytes)
274 status:
275 description: The HTTP status from the APIC
276 returned: failure or debug
277 type: int
278 sample: 200
279 url:
280 description: The HTTP url used for the request to the APIC
281 returned: failure or debug
282 type: str
283 sample: https://10.11.12.13/api/mo/uni/tn-production.json
284 """
285
286
287 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
288 from ansible.module_utils.basic import AnsibleModule
289 from ansible_collections.cisco.aci.plugins.module_utils.constants import SYSLOG_FORMATS
290
291
292 def main():
293 argument_spec = aci_argument_spec()
294 argument_spec.update(aci_annotation_spec())
295 argument_spec.update(
296 name=dict(type="str", aliases=["syslog_group", "syslog_group_name"]),
297 format=dict(type="str", choices=list(SYSLOG_FORMATS)),
298 local_file_log_format=dict(type="str", choices=list(SYSLOG_FORMATS)),
299 console_log_format=dict(type="str", choices=list(SYSLOG_FORMATS)),
300 admin_state=dict(type="str", choices=["enabled", "disabled"]),
301 console_logging=dict(type="str", choices=["enabled", "disabled"]),
302 console_log_severity=dict(type="str", choices=["alerts", "critical", "emergencies"]),
303 local_file_logging=dict(type="str", choices=["enabled", "disabled"]),
304 local_file_log_severity=dict(
305 type="str", choices=["alerts", "critical", "debugging", "emergencies", "error", "information", "notifications", "warnings"]
306 ),
307 include_ms=dict(type="bool"),
308 include_time_zone=dict(type="bool"),
309 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
310 description=dict(type="str", aliases=["descr"]),
311 )
312
313 module = AnsibleModule(
314 argument_spec=argument_spec,
315 supports_check_mode=True,
316 required_if=[
317 ["state", "absent", ["name"]],
318 ["state", "present", ["name"]],
319 ],
320 )
321
322 aci = ACIModule(module)
323
324 name = module.params.get("name")
325 format = SYSLOG_FORMATS.get(module.params.get("format"))
326 admin_state = module.params.get("admin_state")
327 console_logging = module.params.get("console_logging")
328 console_log_severity = module.params.get("console_log_severity")
329 console_log_format = SYSLOG_FORMATS.get(module.params.get("console_log_format"))
330 local_file_log_format = SYSLOG_FORMATS.get(module.params.get("local_file_log_format"))
331 local_file_logging = module.params.get("local_file_logging")
332 local_file_log_severity = module.params.get("local_file_log_severity")
333 include_ms = aci.boolean(module.params.get("include_ms"))
334 include_time_zone = aci.boolean(module.params.get("include_time_zone"))
335 state = module.params.get("state")
336 description = module.params.get("description")
337
338 if console_log_format is None:
339 console_log_format = format
340
341 if local_file_log_format is None:
342 local_file_log_format = format
343
344 aci.construct_url(
345 root_class=dict(
346 aci_class="syslogGroup",
347 aci_rn="fabric/slgroup-{0}".format(name),
348 module_object=name,
349 target_filter={"name": name},
350 ),
351 child_classes=["syslogRemoteDest", "syslogProf", "syslogFile", "syslogConsole"],
352 )
353
354 aci.get_existing()
355
356 if state == "present":
357 class_config = dict(
358 name=name,
359 format=format,
360 descr=description,
361 includeMilliSeconds=include_ms,
362 )
363 if include_time_zone is not None:
364 class_config["includeTimeZone"] = include_time_zone
365 aci.payload(
366 aci_class="syslogGroup",
367 class_config=class_config,
368 child_configs=[
369 dict(
370 syslogProf=dict(
371 attributes=dict(adminState=admin_state, name="syslog"),
372 ),
373 ),
374 dict(
375 syslogFile=dict(
376 attributes=dict(adminState=local_file_logging, format=local_file_log_format, severity=local_file_log_severity),
377 ),
378 ),
379 dict(
380 syslogConsole=dict(
381 attributes=dict(adminState=console_logging, format=console_log_format, severity=console_log_severity),
382 ),
383 ),
384 ],
385 )
386
387 aci.get_diff(aci_class="syslogGroup")
388
389 aci.post_config()
390
391 elif state == "absent":
392 aci.delete_config()
393
394 aci.exit_json()
395
396
397 if __name__ == "__main__":
398 main()