Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_ntp_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_ntp_policy
16 short_description: NTP policy configuration for Cisco Intersight
17 description:
18 - NTP policy configuration for Cisco Intersight.
19 - Used to configure NTP servers and timezone settings on Cisco Intersight managed devices.
20 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
21 extends_documentation_fragment: intersight
22 options:
23 state:
24 description:
25 - If C(present), will verify the resource is present and will create if needed.
26 - If C(absent), will verify the resource is absent and will delete if needed.
27 type: str
28 choices: [present, absent]
29 default: present
30 organization:
31 description:
32 - The name of the Organization this resource is assigned to.
33 - Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization.
34 type: str
35 default: default
36 name:
37 description:
38 - The name assigned to the NTP policy.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 tags:
43 description:
44 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
45 type: list
46 elements: dict
47 description:
48 description:
49 - The user-defined description of the NTP policy.
50 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
51 type: str
52 aliases: [descr]
53 enable:
54 description:
55 - Enable or disable NTP.
56 type: bool
57 default: true
58 ntp_servers:
59 description:
60 - List of NTP servers configured on the endpoint.
61 type: list
62 elements: str
63 timezone:
64 description:
65 - Timezone of services on the endpoint.
66 type: str
67 author:
68 - David Soper (@dsoper2)
69 '''
70
71 EXAMPLES = r'''
72 - name: Configure NTP Policy
73 cisco.intersight.intersight_ntp_policy:
74 api_private_key: "{{ api_private_key }}"
75 api_key_id: "{{ api_key_id }}"
76 organization: DevNet
77 name: lab-ntp
78 description: NTP policy for lab use
79 tags:
80 - Key: Site
81 Value: RCDN
82 ntp_servers:
83 - ntp.esl.cisco.com
84 timezone: America/Chicago
85
86 - name: Delete NTP Policy
87 cisco.intersight.intersight_ntp_policy:
88 api_private_key: "{{ api_private_key }}"
89 api_key_id: "{{ api_key_id }}"
90 organization: DevNet
91 name: lab-ntp
92 state: absent
93 '''
94
95 RETURN = r'''
96 api_response:
97 description: The API response output returned by the specified resource.
98 returned: always
99 type: dict
100 sample:
101 "api_response": {
102 "Name": "lab-ntp",
103 "ObjectType": "ntp.Policy",
104 "Tags": [
105 {
106 "Key": "Site",
107 "Value": "RCDN"
108 }
109 ]
110 }
111 '''
112
113
114 from ansible.module_utils.basic import AnsibleModule
115 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
116
117
118 def main():
119 argument_spec = intersight_argument_spec.copy()
120 argument_spec.update(
121 state=dict(type='str', choices=['present', 'absent'], default='present'),
122 organization=dict(type='str', default='default'),
123 name=dict(type='str', required=True),
124 description=dict(type='str', aliases=['descr']),
125 tags=dict(type='list', elements='dict'),
126 enable=dict(type='bool', default=True),
127 ntp_servers=dict(type='list', elements='str'),
128 timezone=dict(type='str'),
129 )
130
131 module = AnsibleModule(
132 argument_spec,
133 supports_check_mode=True,
134 )
135
136 intersight = IntersightModule(module)
137 intersight.result['api_response'] = {}
138 intersight.result['trace_id'] = ''
139
140 # Resource path used to configure policy
141 resource_path = '/ntp/Policies'
142 # Define API body used in compares or create
143 intersight.api_body = {
144 'Organization': {
145 'Name': intersight.module.params['organization'],
146 },
147 'Name': intersight.module.params['name'],
148 'Enabled': intersight.module.params['enable']
149 }
150 if intersight.module.params['state'] == 'present':
151 if not intersight.module.params['ntp_servers'] and intersight.module.params['enable']:
152 intersight.module.fail_json(msg="NTP servers are required when state is present and NTP is enabled")
153 else:
154 intersight.api_body['NtpServers'] = intersight.module.params['ntp_servers']
155 if intersight.module.params['timezone']:
156 intersight.api_body['Timezone'] = intersight.module.params['timezone']
157 intersight.set_tags_and_description()
158 # Prevent a case where idempotency fails because of NtpServers (None vs [])
159 if intersight.module.params['enable'] is False:
160 intersight.api_body['NtpServers'] = []
161
162 intersight.configure_policy_or_profile(resource_path=resource_path)
163
164 module.exit_json(**intersight.result)
165
166
167 if __name__ == '__main__':
168 main()