Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_ntp_server.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 = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: aci_ntp_server
20 short_description: Manage NTP servers (datetime:NtpProv)
21 description:
22 - Manage NTP server (datetimeNtpProv) configuration on Cisco ACI fabrics.
23 options:
24 ntp_policy:
25 description:
26 - Name of an existing NTP policy
27 type: str
28 required: true
29 aliases: [ policy_name ]
30 ntp_server:
31 description:
32 - Name of the NTP server
33 type: str
34 aliases: [ server_name ]
35 description:
36 description:
37 - Description of the NTP server
38 type: str
39 min_poll:
40 description:
41 - Minimum polling interval
42 type: int
43 max_poll:
44 description:
45 - Maximum polling interval
46 type: int
47 preferred:
48 description:
49 - Is this the preferred NTP server
50 type: bool
51 epg_type:
52 description:
53 - Type of management EPG to use to reach the NTP server, inb or oob
54 type: str
55 choices: [ inb, oob ]
56 epg_name:
57 description:
58 - Name of the management EPG to reach the NTP server
59 type: str
60 state:
61 description:
62 - Use C(present) or C(absent) for adding or removing.
63 - Use C(query) for listing an object or multiple objects.
64 type: str
65 choices: [ absent, present, query ]
66 default: present
67 extends_documentation_fragment:
68 - cisco.aci.aci
69 - cisco.aci.annotation
70 - cisco.aci.owner
71
72 notes:
73 - The used C(ntp_policy) must exist before using this module in your playbook.
74 The M(cisco.aci.aci_ntp_policy) module can be used for this.
75 seealso:
76 - name: APIC Management Information Model reference
77 description: More information about the internal APIC class B(datetime:NtpProv).
78 link: https://developer.cisco.com/docs/apic-mim-ref/
79 author:
80 - Tim Cragg (@timcragg)
81 """
82
83 EXAMPLES = r"""
84 - name: Add a new NTP server
85 cisco.aci.aci_ntp_server:
86 host: apic
87 username: admin
88 password: SomeSecretPassword
89 ntp_policy: my_ntp_policy
90 ntp_server: 10.20.30.40
91 min_poll: 3
92 max_poll: 8
93 preferred: true
94 state: present
95 delegate_to: localhost
96
97 - name: Remove a NTP server
98 cisco.aci.aci_ntp_server:
99 host: apic
100 username: admin
101 password: SomeSecretPassword
102 ntp_policy: my_ntp_policy
103 ntp_server: 10.20.30.40
104 state: absent
105 delegate_to: localhost
106
107 - name: Query a NTP server
108 cisco.aci.aci_ntp_server:
109 host: apic
110 username: admin
111 password: SomeSecretPassword
112 ntp_policy: my_ntp_policy
113 ntp_server: 10.20.30.40
114 state: query
115 delegate_to: localhost
116 register: query_result
117
118 - name: Query all NTP servers within a policy
119 cisco.aci.aci_ntp_server:
120 host: apic
121 username: admin
122 password: SomeSecretPassword
123 ntp_policy: my_ntp_policy
124 state: query
125 delegate_to: localhost
126 register: query_result
127 """
128
129 RETURN = r"""
130 current:
131 description: The existing configuration from the APIC after the module has finished
132 returned: success
133 type: list
134 sample:
135 [
136 {
137 "fvTenant": {
138 "attributes": {
139 "descr": "Production environment",
140 "dn": "uni/tn-production",
141 "name": "production",
142 "nameAlias": "",
143 "ownerKey": "",
144 "ownerTag": ""
145 }
146 }
147 }
148 ]
149 error:
150 description: The error information as returned from the APIC
151 returned: failure
152 type: dict
153 sample:
154 {
155 "code": "122",
156 "text": "unknown managed object class foo"
157 }
158 raw:
159 description: The raw output returned by the APIC REST API (xml or json)
160 returned: parse error
161 type: str
162 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
163 sent:
164 description: The actual/minimal configuration pushed to the APIC
165 returned: info
166 type: list
167 sample:
168 {
169 "fvTenant": {
170 "attributes": {
171 "descr": "Production environment"
172 }
173 }
174 }
175 previous:
176 description: The original configuration from the APIC before the module has started
177 returned: info
178 type: list
179 sample:
180 [
181 {
182 "fvTenant": {
183 "attributes": {
184 "descr": "Production",
185 "dn": "uni/tn-production",
186 "name": "production",
187 "nameAlias": "",
188 "ownerKey": "",
189 "ownerTag": ""
190 }
191 }
192 }
193 ]
194 proposed:
195 description: The assembled configuration from the user-provided parameters
196 returned: info
197 type: dict
198 sample:
199 {
200 "fvTenant": {
201 "attributes": {
202 "descr": "Production environment",
203 "name": "production"
204 }
205 }
206 }
207 filter_string:
208 description: The filter string used for the request
209 returned: failure or debug
210 type: str
211 sample: ?rsp-prop-include=config-only
212 method:
213 description: The HTTP method used for the request to the APIC
214 returned: failure or debug
215 type: str
216 sample: POST
217 response:
218 description: The HTTP response from the APIC
219 returned: failure or debug
220 type: str
221 sample: OK (30 bytes)
222 status:
223 description: The HTTP status from the APIC
224 returned: failure or debug
225 type: int
226 sample: 200
227 url:
228 description: The HTTP url used for the request to the APIC
229 returned: failure or debug
230 type: str
231 sample: https://10.11.12.13/api/mo/uni/tn-production.json
232 """
233
234 from ansible.module_utils.basic import AnsibleModule
235 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
236
237
238 def main():
239 argument_spec = aci_argument_spec()
240 argument_spec.update(aci_annotation_spec())
241 argument_spec.update(aci_owner_spec())
242 argument_spec.update(
243 ntp_policy=dict(type="str", aliases=["policy_name"], required=True),
244 ntp_server=dict(type="str", aliases=["server_name"]),
245 description=dict(type="str"),
246 min_poll=dict(type="int"),
247 max_poll=dict(type="int"),
248 preferred=dict(type="bool"),
249 epg_type=dict(type="str", choices=["inb", "oob"]),
250 epg_name=dict(type="str"),
251 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
252 )
253
254 module = AnsibleModule(
255 argument_spec=argument_spec,
256 supports_check_mode=True,
257 required_if=[
258 ["state", "absent", ["ntp_server"]],
259 ["state", "present", ["ntp_server"]],
260 ],
261 required_together=[
262 ["epg_type", "epg_name"],
263 ],
264 )
265 aci = ACIModule(module)
266
267 ntp_policy = module.params.get("ntp_policy")
268 ntp_server = module.params.get("ntp_server")
269 description = module.params.get("description")
270 min_poll = module.params.get("min_poll")
271 max_poll = module.params.get("max_poll")
272 preferred = aci.boolean(module.params.get("preferred"))
273 epg_type = module.params.get("epg_type")
274 epg_name = module.params.get("epg_name")
275 state = module.params.get("state")
276 child_classes = ["datetimeRsNtpProvToEpg"]
277
278 aci.construct_url(
279 root_class=dict(
280 aci_class="datetimePol",
281 aci_rn="fabric/time-{0}".format(ntp_policy),
282 module_object=ntp_policy,
283 target_filter={"name": ntp_policy},
284 ),
285 subclass_1=dict(
286 aci_class="datetimeNtpProv",
287 aci_rn="ntpprov-{0}".format(ntp_server),
288 module_object=ntp_server,
289 target_filter={"name": ntp_server},
290 ),
291 child_classes=child_classes,
292 )
293
294 aci.get_existing()
295
296 if state == "present":
297 child_configs = []
298 if epg_type is not None:
299 tdn = "uni/tn-mgmt/mgmtp-default/{0}-{1}".format(epg_type, epg_name)
300 child_configs.append(dict(datetimeRsNtpProvToEpg=dict(attributes=dict(tDn=tdn))))
301 aci.payload(
302 aci_class="datetimeNtpProv",
303 class_config=dict(
304 name=ntp_server,
305 descr=description,
306 maxPoll=max_poll,
307 minPoll=min_poll,
308 preferred=preferred,
309 ),
310 child_configs=child_configs,
311 )
312
313 aci.get_diff(aci_class="datetimeNtpProv")
314
315 aci.post_config()
316
317 elif state == "absent":
318 aci.delete_config()
319
320 aci.exit_json()
321
322
323 if __name__ == "__main__":
324 main()