Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Tim Cragg (@timcragg)
5 # Copyright: (c) 2023, Akini Ross (@akinross)
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_snmp_user
17 short_description: Manage SNMP v3 Users (snmp:UserP)
18 description:
19 - Manage SNMP v3 Users.
20 - Note that all properties within the snmpUserP class are Create-only. To modify any property of an existing user, you must delete and re-create it.
21 options:
22 auth_type:
23 description:
24 - SNMP authentication method
25 type: str
26 choices: [ hmac-md5-96, hmac-sha1-96, hmac-sha2-224, hmac-sha2-256, hmac-sha2-384, hmac-sha2-512 ]
27 auth_key:
28 description:
29 - SNMP authentication key
30 - Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC.
31 type: str
32 name:
33 description:
34 - Name of the SNMP user policy
35 type: str
36 aliases: [ snmp_user_policy ]
37 description:
38 description:
39 - Description of the SNMP user policy
40 type: str
41 aliases: [ descr ]
42 policy:
43 description:
44 - Name of an existing SNMP policy
45 type: str
46 aliases: [ snmp_policy, snmp_policy_name ]
47 privacy_type:
48 description:
49 - SNMP privacy type
50 type: str
51 choices: [ aes-128, des, none ]
52 privacy_key:
53 description:
54 - SNMP privacy key
55 - Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC.
56 type: str
57 state:
58 description:
59 - Use C(present) or C(absent) for adding or removing.
60 - Use C(query) for listing an object or multiple objects.
61 type: str
62 choices: [ absent, present, query ]
63 default: present
64 extends_documentation_fragment:
65 - cisco.aci.aci
66 - cisco.aci.annotation
67
68 notes:
69 - The C(policy) used must exist before using this module in your playbook.
70 The M(cisco.aci.aci_snmp_policy) module can be used for this.
71 seealso:
72 - module: cisco.aci.aci_snmp_policy
73 - name: APIC Management Information Model reference
74 description: More information about the internal APIC class B(snmp:UserP).
75 link: https://developer.cisco.com/docs/apic-mim-ref/
76 author:
77 - Tim Cragg (@timcragg)
78 - Akini Ross (@akinross)
79 """
80
81 EXAMPLES = r"""
82 - name: Create an SNMP user
83 cisco.aci.aci_snmp_user:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 policy: my_snmp_policy
88 name: my_snmp_user
89 auth_type: hmac-sha2-256
90 auth_key: "{{ hmac_key }}"
91 state: present
92 delegate_to: localhost
93
94 - name: Create an SNMP user with both authentication and privacy
95 cisco.aci.aci_snmp_user:
96 host: apic
97 username: admin
98 password: SomeSecretPassword
99 policy: my_snmp_policy
100 name: my_snmp_user
101 auth_type: hmac-sha1-96
102 auth_key: "{{ hmac_key }}"
103 privacy_type: aes-128
104 privacy_key: "{{ aes_key }}"
105 state: present
106 delegate_to: localhost
107
108 - name: Remove an SNMP user
109 cisco.aci.aci_snmp_user:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 policy: my_snmp_policy
114 name: my_snmp_user
115 state: absent
116 delegate_to: localhost
117
118 - name: Query an SNMP user
119 cisco.aci.aci_snmp_user:
120 host: apic
121 username: admin
122 password: SomeSecretPassword
123 policy: my_snmp_policy
124 name: my_snmp_user
125 state: query
126 delegate_to: localhost
127 register: query_result
128
129 - name: Query all SNMP users
130 cisco.aci.aci_snmp_user:
131 host: apic
132 username: admin
133 password: SomeSecretPassword
134 state: query
135 delegate_to: localhost
136 register: query_result
137 """
138
139 RETURN = r"""
140 current:
141 description: The existing configuration from the APIC after the module has finished
142 returned: success
143 type: list
144 sample:
145 [
146 {
147 "fvTenant": {
148 "attributes": {
149 "descr": "Production environment",
150 "dn": "uni/tn-production",
151 "name": "production",
152 "nameAlias": "",
153 "ownerKey": "",
154 "ownerTag": ""
155 }
156 }
157 }
158 ]
159 error:
160 description: The error information as returned from the APIC
161 returned: failure
162 type: dict
163 sample:
164 {
165 "code": "122",
166 "text": "unknown managed object class foo"
167 }
168 raw:
169 description: The raw output returned by the APIC REST API (xml or json)
170 returned: parse error
171 type: str
172 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
173 sent:
174 description: The actual/minimal configuration pushed to the APIC
175 returned: info
176 type: list
177 sample:
178 {
179 "fvTenant": {
180 "attributes": {
181 "descr": "Production environment"
182 }
183 }
184 }
185 previous:
186 description: The original configuration from the APIC before the module has started
187 returned: info
188 type: list
189 sample:
190 [
191 {
192 "fvTenant": {
193 "attributes": {
194 "descr": "Production",
195 "dn": "uni/tn-production",
196 "name": "production",
197 "nameAlias": "",
198 "ownerKey": "",
199 "ownerTag": ""
200 }
201 }
202 }
203 ]
204 proposed:
205 description: The assembled configuration from the user-provided parameters
206 returned: info
207 type: dict
208 sample:
209 {
210 "fvTenant": {
211 "attributes": {
212 "descr": "Production environment",
213 "name": "production"
214 }
215 }
216 }
217 filter_string:
218 description: The filter string used for the request
219 returned: failure or debug
220 type: str
221 sample: ?rsp-prop-include=config-only
222 method:
223 description: The HTTP method used for the request to the APIC
224 returned: failure or debug
225 type: str
226 sample: POST
227 response:
228 description: The HTTP response from the APIC
229 returned: failure or debug
230 type: str
231 sample: OK (30 bytes)
232 status:
233 description: The HTTP status from the APIC
234 returned: failure or debug
235 type: int
236 sample: 200
237 url:
238 description: The HTTP url used for the request to the APIC
239 returned: failure or debug
240 type: str
241 sample: https://10.11.12.13/api/mo/uni/tn-production.json
242 """
243
244
245 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
246 from ansible.module_utils.basic import AnsibleModule
247
248
249 def main():
250 argument_spec = aci_argument_spec()
251 argument_spec.update(aci_annotation_spec())
252 argument_spec.update(
253 policy=dict(type="str", aliases=["snmp_policy", "snmp_policy_name"]),
254 name=dict(type="str", aliases=["snmp_user_policy"]),
255 description=dict(type="str", aliases=["descr"]),
256 auth_type=dict(type="str", choices=["hmac-md5-96", "hmac-sha1-96", "hmac-sha2-224", "hmac-sha2-256", "hmac-sha2-384", "hmac-sha2-512"]),
257 auth_key=dict(type="str", no_log=True),
258 privacy_type=dict(type="str", choices=["aes-128", "des", "none"]),
259 privacy_key=dict(type="str", no_log=True),
260 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
261 )
262
263 module = AnsibleModule(
264 argument_spec=argument_spec,
265 supports_check_mode=True,
266 required_if=[
267 ["state", "absent", ["policy", "name"]],
268 ["state", "present", ["policy", "name"]],
269 ],
270 )
271
272 aci = ACIModule(module)
273
274 policy = module.params.get("policy")
275 name = module.params.get("name")
276 description = module.params.get("description")
277 auth_type = module.params.get("auth_type")
278 auth_key = module.params.get("auth_key")
279 privacy_type = module.params.get("privacy_type")
280 privacy_key = module.params.get("privacy_key")
281 state = module.params.get("state")
282
283 aci.construct_url(
284 root_class=dict(
285 aci_class="snmpPol",
286 aci_rn="fabric/snmppol-{0}".format(policy),
287 module_object=policy,
288 target_filter={"name": policy},
289 ),
290 subclass_1=dict(
291 aci_class="snmpUserP",
292 aci_rn="user-{0}".format(name),
293 module_object=name,
294 target_filter={"name": name},
295 ),
296 )
297
298 aci.get_existing()
299
300 if state == "present":
301 aci.payload(
302 aci_class="snmpUserP",
303 class_config=dict(privType=privacy_type, privKey=privacy_key, authType=auth_type, authKey=auth_key, name=name, descr=description),
304 )
305
306 aci.get_diff(aci_class="snmpUserP")
307
308 aci.post_config()
309
310 elif state == "absent":
311 aci.delete_config()
312
313 aci.exit_json()
314
315
316 if __name__ == "__main__":
317 main()