| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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: purefb_tz |
| 20 |
version_added: '1.10.0' |
| 21 |
short_description: Configure Pure Storage FlashBlade timezone |
| 22 |
description: |
| 23 |
- Configure the timezone for a Pure Storage FlashBlade. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
timezone: |
| 28 |
description: |
| 29 |
- If not provided, the module will attempt to get the current local timezone from the server |
| 30 |
type: str |
| 31 |
extends_documentation_fragment: |
| 32 |
- purestorage.flashblade.purestorage.fb |
| 33 |
""" |
| 34 |
|
| 35 |
EXAMPLES = r""" |
| 36 |
- name: Set FlashBlade Timezone to Americas/Los_Angeles |
| 37 |
purestorage.flashblade.purefb_tz: |
| 38 |
timezone: "America/Los_Angeles" |
| 39 |
fb_url: 10.10.10.2 |
| 40 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 41 |
""" |
| 42 |
|
| 43 |
RETURN = r""" |
| 44 |
""" |
| 45 |
|
| 46 |
HAS_PURESTORAGE = True |
| 47 |
try: |
| 48 |
from pypureclient import flashblade |
| 49 |
except ImportError: |
| 50 |
HAS_PURESTORAGE = False |
| 51 |
|
| 52 |
HAS_PYTZ = True |
| 53 |
try: |
| 54 |
import pytz |
| 55 |
except ImportError: |
| 56 |
HAS_PYTX = False |
| 57 |
|
| 58 |
import os |
| 59 |
import re |
| 60 |
import platform |
| 61 |
|
| 62 |
from ansible.module_utils.common.process import get_bin_path |
| 63 |
from ansible.module_utils.facts.utils import get_file_content |
| 64 |
from ansible.module_utils.basic import AnsibleModule |
| 65 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 66 |
get_system, |
| 67 |
purefb_argument_spec, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def _findstr(text, match): |
| 72 |
for line in text.splitlines(): |
| 73 |
if match in line: |
| 74 |
found = line |
| 75 |
return found |
| 76 |
|
| 77 |
|
| 78 |
def _get_local_tz(module, timezone="UTC"): |
| 79 |
""" |
| 80 |
We will attempt to get the local timezone of the server running the module and use that. |
| 81 |
If we can't get the timezone then we will set the default to be UTC |
| 82 |
|
| 83 |
Linnux has been tested and other opersting systems should be OK. |
| 84 |
Failures cause assumption of UTC |
| 85 |
|
| 86 |
Windows is not supported and will assume UTC |
| 87 |
""" |
| 88 |
if platform.system() == "Linux": |
| 89 |
timedatectl = get_bin_path("timedatectl") |
| 90 |
if timedatectl is not None: |
| 91 |
rcode, stdout, _stderr = module.run_command(timedatectl) |
| 92 |
if rcode == 0 and stdout: |
| 93 |
line = _findstr(stdout, "Time zone") |
| 94 |
full_tz = line.split(":", 1)[1].rstrip() |
| 95 |
timezone = full_tz.split()[0] |
| 96 |
return timezone |
| 97 |
module.warn("Incorrect timedatectl output. Timezone will be set to UTC") |
| 98 |
else: |
| 99 |
if os.path.exists("/etc/timezone"): |
| 100 |
timezone = get_file_content("/etc/timezone") |
| 101 |
else: |
| 102 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 103 |
|
| 104 |
elif platform.system() == "SunOS": |
| 105 |
if os.path.exists("/etc/default/init"): |
| 106 |
for line in get_file_content("/etc/default/init", "").splitlines(): |
| 107 |
if line.startswith("TZ="): |
| 108 |
timezone = line.split("=", 1)[1] |
| 109 |
return timezone |
| 110 |
else: |
| 111 |
module.warn("Could not find /etc/default/init. Assuming UTC") |
| 112 |
|
| 113 |
elif re.match("^Darwin", platform.platform()): |
| 114 |
systemsetup = get_bin_path("systemsetup") |
| 115 |
if systemsetup is not None: |
| 116 |
rcode, stdout, _stderr = module.execute(systemsetup, "-gettimezone") |
| 117 |
if rcode == 0 and stdout: |
| 118 |
timezone = stdout.split(":", 1)[1].lstrip() |
| 119 |
else: |
| 120 |
module.warn("Could not run systemsetup. Assuming UTC") |
| 121 |
else: |
| 122 |
module.warn("Could not find systemsetup. Assuming UTC") |
| 123 |
|
| 124 |
elif re.match("^(Free|Net|Open)BSD", platform.platform()): |
| 125 |
if os.path.exists("/etc/timezone"): |
| 126 |
timezone = get_file_content("/etc/timezone") |
| 127 |
else: |
| 128 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 129 |
|
| 130 |
elif platform.system() == "AIX": |
| 131 |
aix_oslevel = int(platform.version() + platform.release()) |
| 132 |
if aix_oslevel >= 61: |
| 133 |
if os.path.exists("/etc/environment"): |
| 134 |
for line in get_file_content("/etc/environment", "").splitlines(): |
| 135 |
if line.startswith("TZ="): |
| 136 |
timezone = line.split("=", 1)[1] |
| 137 |
return timezone |
| 138 |
else: |
| 139 |
module.warn("Could not find /etc/environment. Assuming UTC") |
| 140 |
else: |
| 141 |
module.warn( |
| 142 |
"Cannot determine timezone when AIX os level < 61. Assuming UTC" |
| 143 |
) |
| 144 |
|
| 145 |
else: |
| 146 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 147 |
|
| 148 |
return timezone |
| 149 |
|
| 150 |
|
| 151 |
def set_timezone(module, blade): |
| 152 |
"""Set timezone""" |
| 153 |
changed = True |
| 154 |
if not module.check_mode: |
| 155 |
res = blade.patch_arrays(flashblade.Array(time_zone=module.params["timezone"])) |
| 156 |
if res.status_code != 200: |
| 157 |
module.fail_json( |
| 158 |
msg="Failed to timezone. Error: {0}".format(res.errors[0].message) |
| 159 |
) |
| 160 |
|
| 161 |
module.exit_json(changed=changed) |
| 162 |
|
| 163 |
|
| 164 |
def main(): |
| 165 |
argument_spec = purefb_argument_spec() |
| 166 |
argument_spec.update( |
| 167 |
dict( |
| 168 |
timezone=dict(type="str"), |
| 169 |
) |
| 170 |
) |
| 171 |
|
| 172 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 173 |
|
| 174 |
if not HAS_PURESTORAGE: |
| 175 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 176 |
if not HAS_PYTZ: |
| 177 |
module.fail_json(msg="pytz is required for this module") |
| 178 |
|
| 179 |
blade = get_system(module) |
| 180 |
|
| 181 |
if not module.params["timezone"]: |
| 182 |
module.params["timezone"] = _get_local_tz(module) |
| 183 |
if module.params["timezone"] not in pytz.all_timezones_set: |
| 184 |
module.fail_json( |
| 185 |
msg="Timezone {0} is not valid".format(module.params["timezone"]) |
| 186 |
) |
| 187 |
|
| 188 |
blade = get_system(module) |
| 189 |
current_tz = list(blade.get_arrays().items)[0].time_zone |
| 190 |
if current_tz != module.params["timezone"]: |
| 191 |
set_timezone(module, blade) |
| 192 |
|
| 193 |
module.exit_json(changed=False) |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |