Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Tim Cragg (@timcragg)
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_aaa_ssh_auth
16 short_description: Manage AAA SSH auth objects (aaa:SshAuth)
17 description:
18 - Manage AAA SSH Auth key configuration on Cisco ACI fabrics.
19 options:
20 aaa_user:
21 description:
22 - Name of an existing AAA user
23 type: str
24 required: true
25 auth_name:
26 description:
27 - Name of the AAA SSH Auth key
28 type: str
29 data:
30 description:
31 - SSH key data
32 type: str
33 state:
34 description:
35 - Use C(present) or C(absent) for adding or removing.
36 - Use C(query) for listing an object or multiple objects.
37 type: str
38 choices: [ absent, present, query ]
39 default: present
40 extends_documentation_fragment:
41 - cisco.aci.aci
42 - cisco.aci.annotation
43 - cisco.aci.owner
44
45 notes:
46 - The C(aaa_user) must exist before using this module in your playbook.
47 The M(cisco.aci.aci_aaa_user) modules can be used for this.
48 seealso:
49 - name: APIC Management Information Model reference
50 description: More information about the internal APIC class B(aaa:SshAuth).
51 link: https://developer.cisco.com/docs/apic-mim-ref/
52 author:
53 - Tim Cragg (@timcragg)
54 """
55
56 EXAMPLES = r"""
57 - name: Add a new SSH key
58 cisco.aci.aci_aaa_ssh_auth:
59 host: apic
60 username: admin
61 password: SomeSecretPassword
62 aaa_user: my_user
63 auth_name: my_key
64 data: "{{ ssh_key_data_var }}"
65 state: present
66 delegate_to: localhost
67
68 - name: Remove an SSH key
69 cisco.aci.aci_aaa_ssh_auth:
70 host: apic
71 username: admin
72 password: SomeSecretPassword
73 aaa_user: my_user
74 auth_name: my_key
75 state: absent
76 delegate_to: localhost
77
78 - name: Query an SSH key
79 cisco.aci.aci_aaa_ssh_auth:
80 host: apic
81 username: admin
82 password: SomeSecretPassword
83 aaa_user: my_user
84 auth_name: my_key
85 state: query
86 delegate_to: localhost
87 register: query_result
88
89 - name: Query all SSH auth keys under a user
90 cisco.aci.aci_aaa_ssh_auth:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 aaa_user: my_user
95 state: query
96 delegate_to: localhost
97 register: query_result
98 """
99
100 RETURN = r"""
101 current:
102 description: The existing configuration from the APIC after the module has finished
103 returned: success
104 type: list
105 sample:
106 [
107 {
108 "fvTenant": {
109 "attributes": {
110 "descr": "Production environment",
111 "dn": "uni/tn-production",
112 "name": "production",
113 "nameAlias": "",
114 "ownerKey": "",
115 "ownerTag": ""
116 }
117 }
118 }
119 ]
120 error:
121 description: The error information as returned from the APIC
122 returned: failure
123 type: dict
124 sample:
125 {
126 "code": "122",
127 "text": "unknown managed object class foo"
128 }
129 raw:
130 description: The raw output returned by the APIC REST API (xml or json)
131 returned: parse error
132 type: str
133 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
134 sent:
135 description: The actual/minimal configuration pushed to the APIC
136 returned: info
137 type: list
138 sample:
139 {
140 "fvTenant": {
141 "attributes": {
142 "descr": "Production environment"
143 }
144 }
145 }
146 previous:
147 description: The original configuration from the APIC before the module has started
148 returned: info
149 type: list
150 sample:
151 [
152 {
153 "fvTenant": {
154 "attributes": {
155 "descr": "Production",
156 "dn": "uni/tn-production",
157 "name": "production",
158 "nameAlias": "",
159 "ownerKey": "",
160 "ownerTag": ""
161 }
162 }
163 }
164 ]
165 proposed:
166 description: The assembled configuration from the user-provided parameters
167 returned: info
168 type: dict
169 sample:
170 {
171 "fvTenant": {
172 "attributes": {
173 "descr": "Production environment",
174 "name": "production"
175 }
176 }
177 }
178 filter_string:
179 description: The filter string used for the request
180 returned: failure or debug
181 type: str
182 sample: ?rsp-prop-include=config-only
183 method:
184 description: The HTTP method used for the request to the APIC
185 returned: failure or debug
186 type: str
187 sample: POST
188 response:
189 description: The HTTP response from the APIC
190 returned: failure or debug
191 type: str
192 sample: OK (30 bytes)
193 status:
194 description: The HTTP status from the APIC
195 returned: failure or debug
196 type: int
197 sample: 200
198 url:
199 description: The HTTP url used for the request to the APIC
200 returned: failure or debug
201 type: str
202 sample: https://10.11.12.13/api/mo/uni/tn-production.json
203 """
204
205 from ansible.module_utils.basic import AnsibleModule
206 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
207
208
209 def main():
210 argument_spec = aci_argument_spec()
211 argument_spec.update(aci_annotation_spec())
212 argument_spec.update(aci_owner_spec())
213 argument_spec.update(
214 aaa_user=dict(type="str", required=True),
215 auth_name=dict(type="str"),
216 data=dict(type="str"),
217 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
218 )
219
220 module = AnsibleModule(
221 argument_spec=argument_spec,
222 supports_check_mode=True,
223 required_if=[
224 ["state", "absent", ["auth_name"]],
225 ["state", "present", ["auth_name", "data"]],
226 ],
227 )
228
229 aaa_user = module.params.get("aaa_user")
230 auth_name = module.params.get("auth_name")
231 data = module.params.get("data")
232 state = module.params.get("state")
233
234 aci = ACIModule(module)
235 aci.construct_url(
236 root_class=dict(
237 aci_class="aaaUser",
238 aci_rn="userext/user-{0}".format(aaa_user),
239 module_object=aaa_user,
240 target_filter={"name": aaa_user},
241 ),
242 subclass_1=dict(
243 aci_class="aaaSshAuth",
244 aci_rn="sshauth-{0}".format(auth_name),
245 module_object=auth_name,
246 target_filter={"name": auth_name},
247 ),
248 )
249
250 aci.get_existing()
251
252 if state == "present":
253 aci.payload(
254 aci_class="aaaSshAuth",
255 class_config=dict(name=auth_name, data=data),
256 )
257
258 aci.get_diff(aci_class="aaaSshAuth")
259
260 aci.post_config()
261
262 elif state == "absent":
263 aci.delete_config()
264
265 aci.exit_json()
266
267
268 if __name__ == "__main__":
269 main()