| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: ome_application_network_time |
| 19 |
short_description: Updates the network time on OpenManage Enterprise |
| 20 |
version_added: "2.1.0" |
| 21 |
description: This module allows the configuration of network time on OpenManage Enterprise. |
| 22 |
extends_documentation_fragment: |
| 23 |
- dellemc.openmanage.ome_auth_options |
| 24 |
options: |
| 25 |
enable_ntp: |
| 26 |
description: |
| 27 |
- Enables or disables Network Time Protocol(NTP). |
| 28 |
- If I(enable_ntp) is false, then the NTP addresses reset to their default values. |
| 29 |
required: true |
| 30 |
type: bool |
| 31 |
system_time: |
| 32 |
description: |
| 33 |
- Time in the current system. |
| 34 |
- This option is only applicable when I(enable_ntp) is false. |
| 35 |
- This option must be provided in following format 'yyyy-mm-dd hh:mm:ss'. |
| 36 |
type: str |
| 37 |
time_zone: |
| 38 |
description: |
| 39 |
- The valid timezone ID to be used. |
| 40 |
- This option is applicable for both system time and NTP time synchronization. |
| 41 |
type: str |
| 42 |
primary_ntp_address: |
| 43 |
description: |
| 44 |
- The primary NTP address. |
| 45 |
- This option is applicable when I(enable_ntp) is true. |
| 46 |
type: str |
| 47 |
secondary_ntp_address1: |
| 48 |
description: |
| 49 |
- The first secondary NTP address. |
| 50 |
- This option is applicable when I(enable_ntp) is true. |
| 51 |
type: str |
| 52 |
secondary_ntp_address2: |
| 53 |
description: |
| 54 |
- The second secondary NTP address. |
| 55 |
- This option is applicable when I(enable_ntp) is true. |
| 56 |
type: str |
| 57 |
requirements: |
| 58 |
- "python >= 3.9.6" |
| 59 |
author: |
| 60 |
- "Sajna Shetty(@Sajna-Shetty)" |
| 61 |
- "Mangirish Kenkare(@MangirishK)" |
| 62 |
notes: |
| 63 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 64 |
- This module supports C(check_mode). |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = r''' |
| 68 |
--- |
| 69 |
- name: Configure system time |
| 70 |
dellemc.openmanage.ome_application_network_time: |
| 71 |
hostname: "192.168.0.1" |
| 72 |
username: "username" |
| 73 |
password: "password" |
| 74 |
ca_path: "/path/to/ca_cert.pem" |
| 75 |
enable_ntp: false |
| 76 |
system_time: "2020-03-31 21:35:18" |
| 77 |
time_zone: "TZ_ID_11" |
| 78 |
|
| 79 |
- name: Configure NTP server for time synchronization |
| 80 |
dellemc.openmanage.ome_application_network_time: |
| 81 |
hostname: "192.168.0.1" |
| 82 |
username: "username" |
| 83 |
password: "password" |
| 84 |
ca_path: "/path/to/ca_cert.pem" |
| 85 |
enable_ntp: true |
| 86 |
time_zone: "TZ_ID_66" |
| 87 |
primary_ntp_address: "192.168.0.2" |
| 88 |
secondary_ntp_address1: "192.168.0.2" |
| 89 |
secondary_ntp_address2: "192.168.0.4" |
| 90 |
''' |
| 91 |
|
| 92 |
RETURN = r''' |
| 93 |
--- |
| 94 |
msg: |
| 95 |
type: str |
| 96 |
description: Overall status of the network time configuration change. |
| 97 |
returned: always |
| 98 |
sample: "Successfully configured network time." |
| 99 |
proxy_configuration: |
| 100 |
type: dict |
| 101 |
description: Updated application network time configuration. |
| 102 |
returned: success |
| 103 |
sample: { |
| 104 |
"EnableNTP": false, |
| 105 |
"JobId": null, |
| 106 |
"PrimaryNTPAddress": null, |
| 107 |
"SecondaryNTPAddress1": null, |
| 108 |
"SecondaryNTPAddress2": null, |
| 109 |
"SystemTime": null, |
| 110 |
"TimeSource": "Local Clock", |
| 111 |
"TimeZone": "TZ_ID_1", |
| 112 |
"TimeZoneIdLinux": null, |
| 113 |
"TimeZoneIdWindows": null, |
| 114 |
"UtcTime": null |
| 115 |
} |
| 116 |
error_info: |
| 117 |
description: Details of the HTTP error. |
| 118 |
returned: on HTTP error |
| 119 |
type: dict |
| 120 |
sample: { |
| 121 |
"error": { |
| 122 |
"@Message.ExtendedInfo": [ |
| 123 |
{ |
| 124 |
"Message": "Unable to complete the request because the input value |
| 125 |
for SystemTime is missing or an invalid value is entered.", |
| 126 |
"MessageArgs": [ |
| 127 |
"SystemTime" |
| 128 |
], |
| 129 |
"MessageId": "CGEN6002", |
| 130 |
"RelatedProperties": [], |
| 131 |
"Resolution": "Enter a valid value and retry the operation.", |
| 132 |
"Severity": "Critical" |
| 133 |
} |
| 134 |
], |
| 135 |
"code": "Base.1.0.GeneralError", |
| 136 |
"message": "A general error has occurred. See ExtendedInfo for more information." |
| 137 |
} |
| 138 |
} |
| 139 |
''' |
| 140 |
|
| 141 |
import json |
| 142 |
from ssl import SSLError |
| 143 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 144 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 145 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 146 |
|
| 147 |
TIME_CONFIG = "ApplicationService/Network/TimeConfiguration" |
| 148 |
TIME_ZONE = "ApplicationService/Network/TimeZones" |
| 149 |
|
| 150 |
|
| 151 |
def remove_unwanted_keys(key_list, payload): |
| 152 |
[payload.pop(key) for key in key_list if key in payload] |
| 153 |
|
| 154 |
|
| 155 |
def get_payload(module): |
| 156 |
params = module.params |
| 157 |
proxy_payload_map = { |
| 158 |
"enable_ntp": "EnableNTP", |
| 159 |
"time_zone": "TimeZone", |
| 160 |
"system_time": "SystemTime", |
| 161 |
"primary_ntp_address": "PrimaryNTPAddress", |
| 162 |
"secondary_ntp_address1": "SecondaryNTPAddress1", |
| 163 |
"secondary_ntp_address2": "SecondaryNTPAddress2" |
| 164 |
} |
| 165 |
backup_params = params.copy() |
| 166 |
remove_keys = ["hostname", "username", "password", "port", "ca_path", "validate_certs", "timeout"] |
| 167 |
remove_unwanted_keys(remove_keys, backup_params) |
| 168 |
payload = dict([(proxy_payload_map[key], val) for key, val in backup_params.items() if val is not None]) |
| 169 |
return payload |
| 170 |
|
| 171 |
|
| 172 |
def update_time_config_output(back_up_settings): |
| 173 |
remove_keys = ["@odata.context", "@odata.type", "@odata.id"] |
| 174 |
remove_unwanted_keys(remove_keys, back_up_settings) |
| 175 |
back_up_settings.update({"JobId": None}) |
| 176 |
|
| 177 |
|
| 178 |
def get_updated_payload(rest_obj, module, payload): |
| 179 |
remove_keys = ["@odata.context", "@odata.type", "@odata.id", "TimeZoneIdLinux", "TimeZoneIdWindows", "TimeSource", "UtcTime"] |
| 180 |
resp = rest_obj.invoke_request("GET", TIME_CONFIG, api_timeout=150) |
| 181 |
current_setting = resp.json_data |
| 182 |
back_up_settings = current_setting.copy() |
| 183 |
remove_unwanted_keys(remove_keys, current_setting) |
| 184 |
diff = any(key in current_setting and val != current_setting[key] for key, val in payload.items()) |
| 185 |
if module.check_mode: |
| 186 |
if diff: |
| 187 |
module.exit_json(changed=True, msg="Changes found to be applied to the time configuration.") |
| 188 |
else: |
| 189 |
module.exit_json(changed=False, msg="No changes found to be applied to the time configuration.") |
| 190 |
else: |
| 191 |
if diff: |
| 192 |
current_setting.update(payload) |
| 193 |
else: |
| 194 |
update_time_config_output(back_up_settings) |
| 195 |
module.exit_json(changed=False, msg="No changes made to the time configuration as the entered" |
| 196 |
" values are the same as the current configuration.", time_configuration=back_up_settings) |
| 197 |
return current_setting |
| 198 |
|
| 199 |
|
| 200 |
def validate_time_zone(module, rest_obj): |
| 201 |
params = module.params |
| 202 |
time_zone = params.get("time_zone", None) |
| 203 |
if time_zone is not None: |
| 204 |
time_zone_resp = rest_obj.invoke_request("GET", TIME_ZONE) |
| 205 |
time_zone_val = time_zone_resp.json_data["value"] |
| 206 |
time_id_list = [time_dict["Id"] for time_dict in time_zone_val] |
| 207 |
if time_zone not in time_id_list: |
| 208 |
sorted_time_id_list = sorted(time_id_list, key=lambda time_id: [int(i) for i in time_id.split("_") if i.isdigit()]) |
| 209 |
module.exit_json(msg="Provide valid time zone.Choices are {0}".format(",".join(sorted_time_id_list)), failed=True) |
| 210 |
|
| 211 |
|
| 212 |
def validate_input(module): |
| 213 |
system_time = module.params.get("system_time") |
| 214 |
enable_ntp = module.params["enable_ntp"] |
| 215 |
primary_ntp_address = module.params.get("primary_ntp_address") |
| 216 |
secondary_ntp_address1 = module.params.get("secondary_ntp_address1") |
| 217 |
secondary_ntp_address2 = module.params.get("secondary_ntp_address2") |
| 218 |
if enable_ntp is True and system_time is not None: |
| 219 |
module.exit_json(msg="When enable NTP is true,the option system time is not accepted.", failed=True) |
| 220 |
if enable_ntp is False and any([primary_ntp_address, secondary_ntp_address1, secondary_ntp_address2]): |
| 221 |
module.exit_json(msg="When enable NTP is false,the option(s) primary_ntp_address, secondary_ntp_address1 and secondary_ntp_address2 is not accepted.", |
| 222 |
failed=True) |
| 223 |
|
| 224 |
|
| 225 |
def main(): |
| 226 |
specs = { |
| 227 |
"enable_ntp": {"required": True, "type": "bool"}, |
| 228 |
"time_zone": {"required": False, "type": "str"}, |
| 229 |
"system_time": {"required": False, "type": "str"}, |
| 230 |
"primary_ntp_address": {"required": False, "type": "str"}, |
| 231 |
"secondary_ntp_address1": {"required": False, "type": "str"}, |
| 232 |
"secondary_ntp_address2": {"required": False, "type": "str"}, |
| 233 |
} |
| 234 |
|
| 235 |
module = OmeAnsibleModule( |
| 236 |
argument_spec=specs, |
| 237 |
required_if=[['enable_ntp', False, ('time_zone', 'system_time',), True], |
| 238 |
['enable_ntp', True, ('time_zone', 'primary_ntp_address', |
| 239 |
'secondary_ntp_address1', 'secondary_ntp_address2'), True]], |
| 240 |
mutually_exclusive=[['system_time', 'primary_ntp_address'], |
| 241 |
['system_time', 'secondary_ntp_address1'], |
| 242 |
['system_time', 'secondary_ntp_address2']], |
| 243 |
supports_check_mode=True, |
| 244 |
) |
| 245 |
try: |
| 246 |
validate_input(module) |
| 247 |
with RestOME(module.params, req_session=False) as rest_obj: |
| 248 |
validate_time_zone(module, rest_obj) |
| 249 |
payload = get_payload(module) |
| 250 |
updated_payload = get_updated_payload(rest_obj, module, payload) |
| 251 |
resp = rest_obj.invoke_request("PUT", TIME_CONFIG, data=updated_payload, api_timeout=150) |
| 252 |
module.exit_json(msg="Successfully configured network time.", time_configuration=resp.json_data, changed=True) |
| 253 |
except HTTPError as err: |
| 254 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 255 |
except URLError as err: |
| 256 |
module.exit_json(msg=str(err), unreachable=True) |
| 257 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError, OSError) as err: |
| 258 |
module.exit_json(msg=str(err), failed=True) |
| 259 |
|
| 260 |
|
| 261 |
if __name__ == "__main__": |
| 262 |
main() |