| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020, NetApp Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
"""NetApp StorageGRID - Manage Grid NTP Servers""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.1", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_ntp |
| 22 |
short_description: NetApp StorageGRID manage external NTP servers for the grid. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '20.6.0' |
| 26 |
author: NetApp Ansible Team (@jkandati) <[email protected]> |
| 27 |
description: |
| 28 |
- Update NTP server on NetApp StorageGRID. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the specified user should exist or not. |
| 33 |
type: str |
| 34 |
choices: ['present'] |
| 35 |
default: present |
| 36 |
ntp_servers: |
| 37 |
description: |
| 38 |
- List of comma separated NTP server address. |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
required: true |
| 42 |
passphrase: |
| 43 |
description: |
| 44 |
- passphrase for GRID. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = """ |
| 50 |
- name: update NTP servers |
| 51 |
netapp.storagegrid.na_sg_grid_ntp: |
| 52 |
api_url: "https://<storagegrid-endpoint-url>" |
| 53 |
auth_token: "storagegrid-auth-token" |
| 54 |
validate_certs: false |
| 55 |
state: present |
| 56 |
passphrase: "{{ grid_pass }}" |
| 57 |
ntp_servers: "x.x.x.x,xx.x.xx.x" |
| 58 |
""" |
| 59 |
|
| 60 |
RETURN = """ |
| 61 |
resp: |
| 62 |
description: Returns information about the configured NTP servers. |
| 63 |
returned: success |
| 64 |
type: list |
| 65 |
elements: str |
| 66 |
sample: ["10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4"] |
| 67 |
""" |
| 68 |
|
| 69 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 70 |
from ansible.module_utils.basic import AnsibleModule |
| 71 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 72 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 73 |
|
| 74 |
|
| 75 |
class SgGridNtp(object): |
| 76 |
""" |
| 77 |
Create, modify and delete NTP entries for StorageGRID |
| 78 |
""" |
| 79 |
|
| 80 |
def __init__(self): |
| 81 |
""" |
| 82 |
Parse arguments, setup state variables, |
| 83 |
check parameters and ensure request module is installed |
| 84 |
""" |
| 85 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 86 |
self.argument_spec.update( |
| 87 |
dict( |
| 88 |
state=dict(required=False, type="str", choices=["present"], default="present"), |
| 89 |
ntp_servers=dict(required=True, type="list", elements="str"), |
| 90 |
passphrase=dict(required=True, type="str", no_log=True), |
| 91 |
) |
| 92 |
) |
| 93 |
|
| 94 |
self.module = AnsibleModule( |
| 95 |
argument_spec=self.argument_spec, |
| 96 |
# required_if=[("state", "present", ["state", "name", "protocol"])], |
| 97 |
supports_check_mode=True, |
| 98 |
) |
| 99 |
|
| 100 |
self.na_helper = NetAppModule() |
| 101 |
|
| 102 |
# set up state variables |
| 103 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 104 |
# Calling generic SG rest_api class |
| 105 |
self.rest_api = SGRestAPI(self.module) |
| 106 |
# Get API version |
| 107 |
self.rest_api.get_sg_product_version() |
| 108 |
self.api_version = self.rest_api.get_api_version() |
| 109 |
|
| 110 |
# Checking for the parameters passed and create new parameters list |
| 111 |
self.data = self.parameters["ntp_servers"] |
| 112 |
self.passphrase = self.parameters["passphrase"] |
| 113 |
self.ntp_input = {"passphrase": self.passphrase, "servers": self.data} |
| 114 |
|
| 115 |
def get_grid_ntp(self): |
| 116 |
# Check if tenant account exists |
| 117 |
# Return tenant account info if found, or None |
| 118 |
api = "api/%s/grid/ntp-servers" % self.api_version |
| 119 |
|
| 120 |
response, error = self.rest_api.get(api) |
| 121 |
|
| 122 |
if error: |
| 123 |
self.module.fail_json(msg=error) |
| 124 |
|
| 125 |
return response["data"] |
| 126 |
|
| 127 |
def update_grid_ntp(self): |
| 128 |
api = "api/%s/grid/ntp-servers/update" % self.api_version |
| 129 |
|
| 130 |
response, error = self.rest_api.post(api, self.ntp_input) |
| 131 |
|
| 132 |
if error: |
| 133 |
self.module.fail_json(msg=error) |
| 134 |
|
| 135 |
return response["data"] |
| 136 |
|
| 137 |
def apply(self): |
| 138 |
""" |
| 139 |
Perform pre-checks, call functions and exit |
| 140 |
""" |
| 141 |
grid_ntp = self.get_grid_ntp() |
| 142 |
|
| 143 |
cd_action = self.na_helper.get_cd_action(grid_ntp, self.parameters["ntp_servers"]) |
| 144 |
|
| 145 |
if cd_action is None and self.parameters["state"] == "present": |
| 146 |
# let's see if we need to update parameters |
| 147 |
update = False |
| 148 |
|
| 149 |
ntp_diff = [i for i in self.data + grid_ntp if i not in self.data or i not in grid_ntp] |
| 150 |
if ntp_diff: |
| 151 |
update = True |
| 152 |
|
| 153 |
if update: |
| 154 |
self.na_helper.changed = True |
| 155 |
|
| 156 |
result_message = "" |
| 157 |
resp_data = grid_ntp |
| 158 |
if self.na_helper.changed: |
| 159 |
if self.module.check_mode: |
| 160 |
pass |
| 161 |
else: |
| 162 |
resp_data = self.update_grid_ntp() |
| 163 |
result_message = "Grid NTP updated" |
| 164 |
|
| 165 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 166 |
|
| 167 |
|
| 168 |
def main(): |
| 169 |
""" |
| 170 |
Main function |
| 171 |
""" |
| 172 |
na_sg_grid_ntp = SgGridNtp() |
| 173 |
na_sg_grid_ntp.apply() |
| 174 |
|
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
main() |