| 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_ntp |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Configure Pure Storage FlashBlade NTP settings |
| 22 |
description: |
| 23 |
- Set or erase NTP configuration for Pure Storage FlashBlades. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or delete NTP servers configuration |
| 30 |
type: str |
| 31 |
default: present |
| 32 |
choices: [ absent, present ] |
| 33 |
ntp_servers: |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
description: |
| 37 |
- A list of up to 4 alternate NTP servers. These may include IPv4, |
| 38 |
IPv6 or FQDNs. Invalid IP addresses will cause the module to fail. |
| 39 |
No validation is performed for FQDNs. |
| 40 |
- If more than 4 servers are provided, only the first 4 unique |
| 41 |
nameservers will be used. |
| 42 |
- if no servers are given a default of I(0.pool.ntp.org) will be used. |
| 43 |
extends_documentation_fragment: |
| 44 |
- purestorage.flashblade.purestorage.fb |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
- name: Delete exisitng NTP server entries |
| 49 |
purestorage.flashblade.purefb_ntp: |
| 50 |
state: absent |
| 51 |
fb_url: 10.10.10.2 |
| 52 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 53 |
|
| 54 |
- name: Set array NTP servers |
| 55 |
purestorage.flashblade.purefb_ntp: |
| 56 |
state: present |
| 57 |
ntp_servers: |
| 58 |
- "0.pool.ntp.org" |
| 59 |
- "1.pool.ntp.org" |
| 60 |
- "2.pool.ntp.org" |
| 61 |
- "3.pool.ntp.org" |
| 62 |
fb_url: 10.10.10.2 |
| 63 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 64 |
""" |
| 65 |
|
| 66 |
RETURN = r""" |
| 67 |
""" |
| 68 |
|
| 69 |
HAS_PURITY_FB = True |
| 70 |
try: |
| 71 |
from pypureclient.flashblade import Array |
| 72 |
except ImportError: |
| 73 |
HAS_PURITY_FB = False |
| 74 |
|
| 75 |
|
| 76 |
from ansible.module_utils.basic import AnsibleModule |
| 77 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 78 |
get_system, |
| 79 |
purefb_argument_spec, |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
def remove(duplicate): |
| 84 |
final_list = [] |
| 85 |
for num in duplicate: |
| 86 |
if num not in final_list: |
| 87 |
final_list.append(num) |
| 88 |
return final_list |
| 89 |
|
| 90 |
|
| 91 |
def delete_ntp(module, blade): |
| 92 |
"""Delete NTP Servers""" |
| 93 |
changed = True |
| 94 |
if not module.check_mode: |
| 95 |
if list(blade.get_arrays().items)[0].ntp_servers != []: |
| 96 |
res = blade.patch_arrays(array=Array(ntp_servers=[])) |
| 97 |
if res.status_code != 200: |
| 98 |
module.fail_json( |
| 99 |
msg="Deletion of NTP servers failed. Error: {0}".format( |
| 100 |
res.errors[0].message |
| 101 |
) |
| 102 |
) |
| 103 |
module.exit_json(changed=changed) |
| 104 |
|
| 105 |
|
| 106 |
def create_ntp(module, blade): |
| 107 |
"""Set NTP Servers""" |
| 108 |
changed = True |
| 109 |
if not module.check_mode: |
| 110 |
if not module.params["ntp_servers"]: |
| 111 |
module.params["ntp_servers"] = ["0.pool.ntp.org"] |
| 112 |
res = blade.patch_arrays( |
| 113 |
array=Array(ntp_servers=module.params["ntp_servers"][0:4]) |
| 114 |
) |
| 115 |
if res.status_code != 200: |
| 116 |
module.fail_json( |
| 117 |
msg="Update of NTP servers failed. Error: {0}".format( |
| 118 |
res.errors[0].message |
| 119 |
) |
| 120 |
) |
| 121 |
module.exit_json(changed=changed) |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
argument_spec = purefb_argument_spec() |
| 126 |
argument_spec.update( |
| 127 |
dict( |
| 128 |
ntp_servers=dict(type="list", elements="str"), |
| 129 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 130 |
) |
| 131 |
) |
| 132 |
|
| 133 |
required_if = [["state", "present", ["ntp_servers"]]] |
| 134 |
|
| 135 |
module = AnsibleModule( |
| 136 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 137 |
) |
| 138 |
|
| 139 |
if not HAS_PURITY_FB: |
| 140 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 141 |
|
| 142 |
blade = get_system(module) |
| 143 |
|
| 144 |
if module.params["state"] == "absent": |
| 145 |
delete_ntp(module, blade) |
| 146 |
else: |
| 147 |
module.params["ntp_servers"] = remove(module.params["ntp_servers"]) |
| 148 |
if sorted(list(blade.get_arrays().items)[0].ntp_servers) != sorted( |
| 149 |
module.params["ntp_servers"][0:4] |
| 150 |
): |
| 151 |
create_ntp(module, blade) |
| 152 |
|
| 153 |
module.exit_json(changed=False) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |