Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_ntp_server.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': 'certified'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: ucs_ntp_server
16 short_description: Configures NTP server on Cisco UCS Manager
17 extends_documentation_fragment: cisco.ucs.ucs
18 description:
19 - Configures NTP server on Cisco UCS Manager.
20 options:
21 state:
22 description:
23 - If C(absent), will remove an NTP server.
24 - If C(present), will add or update an NTP server.
25 choices: [absent, present]
26 default: present
27 type: str
28
29 ntp_server:
30 description:
31 - NTP server IP address or hostname.
32 - Enter up to 63 characters that form a valid hostname.
33 - Enter a valid IPV4 Address.
34 aliases: [ name ]
35 type: str
36
37 description:
38 description:
39 - A user-defined description of the NTP server.
40 - Enter up to 256 characters.
41 - "You can use any characters or spaces except the following:"
42 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
43 aliases: [ descr ]
44 type: str
45
46 requirements:
47 - ucsmsdk
48 author:
49 - David Soper (@dsoper2)
50 - John McDonough (@movinalot)
51 - CiscoUcs (@CiscoUcs)
52 '''
53
54 EXAMPLES = r'''
55 - name: Configure NTP server
56 cisco.ucs.ucs_ntp_server:
57 hostname: 172.16.143.150
58 username: admin
59 password: password
60 ntp_server: 10.10.10.10
61 description: Internal NTP Server by IP address
62 state: present
63
64 - name: Configure NTP server
65 cisco.ucs.ucs_ntp_server:
66 hostname: 172.16.143.150
67 username: admin
68 password: password
69 ntp_server: pool.ntp.org
70 description: External NTP Server by hostname
71 state: present
72
73 - name: Remove NTP server
74 cisco.ucs.ucs_ntp_server:
75 hostname: 172.16.143.150
76 username: admin
77 password: password
78 ntp_server: 10.10.10.10
79 state: absent
80
81 - name: Remove NTP server
82 cisco.ucs.ucs_ntp_server:
83 hostname: 172.16.143.150
84 username: admin
85 password: password
86 ntp_server: pool.ntp.org
87 state: absent
88 '''
89
90 RETURN = r'''
91 #
92 '''
93
94 from ansible.module_utils.basic import AnsibleModule
95 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
96
97
98 def run_module():
99 argument_spec = ucs_argument_spec.copy()
100 argument_spec.update(
101 ntp_server=dict(type='str', aliases=['name']),
102 description=dict(type='str', aliases=['descr']),
103 state=dict(type='str', default='present', choices=['present', 'absent']),
104 )
105
106 module = AnsibleModule(
107 argument_spec,
108 supports_check_mode=True,
109 required_if=[
110 ['state', 'present', ['ntp_server']],
111 ],
112 )
113 # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation.
114 ucs = UCSModule(module)
115
116 err = False
117
118 from ucsmsdk.mometa.comm.CommNtpProvider import CommNtpProvider
119
120 changed = False
121 try:
122 mo_exists = False
123 props_match = False
124
125 dn = 'sys/svc-ext/datetime-svc/ntp-' + module.params['ntp_server']
126
127 mo = ucs.login_handle.query_dn(dn)
128 if mo:
129 mo_exists = True
130
131 if module.params['state'] == 'absent':
132 if mo_exists:
133 if not module.check_mode:
134 ucs.login_handle.remove_mo(mo)
135 ucs.login_handle.commit()
136 changed = True
137 else:
138 if mo_exists:
139 # check top-level mo props
140 kwargs = dict(descr=module.params['description'])
141 if mo.check_prop_match(**kwargs):
142 props_match = True
143
144 if not props_match:
145 if not module.check_mode:
146 # update/add mo
147 mo = CommNtpProvider(parent_mo_or_dn='sys/svc-ext/datetime-svc',
148 name=module.params['ntp_server'],
149 descr=module.params['description'])
150 ucs.login_handle.add_mo(mo, modify_present=True)
151 ucs.login_handle.commit()
152 changed = True
153
154 except Exception as e:
155 err = True
156 ucs.result['msg'] = "setup error: %s " % str(e)
157
158 ucs.result['changed'] = changed
159 if err:
160 module.fail_json(**ucs.result)
161 module.exit_json(**ucs.result)
162
163
164 def main():
165 run_module()
166
167
168 if __name__ == '__main__':
169 main()