| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2016, NetApp, Inc |
| 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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['deprecated'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
--- |
| 16 |
module: netapp_e_auth |
| 17 |
short_description: NetApp E-Series set or update the password for a storage array. |
| 18 |
description: |
| 19 |
- Sets or updates the password for a storage array. When the password is updated on the storage array, it must be updated on the SANtricity Web |
| 20 |
Services proxy. Note, all storage arrays do not have a Monitor or RO role. |
| 21 |
version_added: "2.2.0" |
| 22 |
author: Kevin Hulquest (@hulquest) |
| 23 |
options: |
| 24 |
validate_certs: |
| 25 |
required: false |
| 26 |
default: true |
| 27 |
description: |
| 28 |
- Should https certificates be validated? |
| 29 |
type: bool |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the storage array. Note that if more than one storage array with this name is detected, the task will fail and you'll have to use |
| 33 |
the ID instead. |
| 34 |
required: False |
| 35 |
type: str |
| 36 |
ssid: |
| 37 |
description: |
| 38 |
- the identifier of the storage array in the Web Services Proxy. |
| 39 |
required: False |
| 40 |
type: str |
| 41 |
set_admin: |
| 42 |
description: |
| 43 |
- Boolean value on whether to update the admin password. If set to false then the RO account is updated. |
| 44 |
required: False |
| 45 |
type: bool |
| 46 |
default: False |
| 47 |
current_password: |
| 48 |
description: |
| 49 |
- The current admin password. This is not required if the password hasn't been set before. |
| 50 |
required: False |
| 51 |
type: str |
| 52 |
new_password: |
| 53 |
description: |
| 54 |
- The password you would like to set. Cannot be more than 30 characters. |
| 55 |
required: True |
| 56 |
type: str |
| 57 |
api_url: |
| 58 |
description: |
| 59 |
- The full API url. |
| 60 |
- "Example: http://ENDPOINT:8080/devmgr/v2" |
| 61 |
- This can optionally be set via an environment variable, API_URL |
| 62 |
required: False |
| 63 |
type: str |
| 64 |
api_username: |
| 65 |
description: |
| 66 |
- The username used to authenticate against the API |
| 67 |
- This can optionally be set via an environment variable, API_USERNAME |
| 68 |
required: False |
| 69 |
type: str |
| 70 |
api_password: |
| 71 |
description: |
| 72 |
- The password used to authenticate against the API |
| 73 |
- This can optionally be set via an environment variable, API_PASSWORD |
| 74 |
required: False |
| 75 |
type: str |
| 76 |
''' |
| 77 |
|
| 78 |
EXAMPLES = ''' |
| 79 |
- name: Test module |
| 80 |
netapp_e_auth: |
| 81 |
name: trex |
| 82 |
current_password: OldPasswd |
| 83 |
new_password: NewPasswd |
| 84 |
set_admin: yes |
| 85 |
api_url: '{{ netapp_api_url }}' |
| 86 |
api_username: '{{ netapp_api_username }}' |
| 87 |
api_password: '{{ netapp_api_password }}' |
| 88 |
''' |
| 89 |
|
| 90 |
RETURN = ''' |
| 91 |
msg: |
| 92 |
description: Success message |
| 93 |
returned: success |
| 94 |
type: str |
| 95 |
sample: "Password Updated Successfully" |
| 96 |
''' |
| 97 |
import json |
| 98 |
import traceback |
| 99 |
|
| 100 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 101 |
from ansible.module_utils.basic import AnsibleModule |
| 102 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 103 |
from ansible.module_utils._text import to_native |
| 104 |
from ansible.module_utils.urls import open_url |
| 105 |
|
| 106 |
HEADERS = { |
| 107 |
"Content-Type": "application/json", |
| 108 |
"Accept": "application/json", |
| 109 |
"x-netapp-password-validate-method": "none" |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
def request(url, data=None, headers=None, method='GET', use_proxy=True, |
| 115 |
force=False, last_mod_time=None, timeout=10, validate_certs=True, |
| 116 |
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): |
| 117 |
try: |
| 118 |
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, |
| 119 |
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, |
| 120 |
url_username=url_username, url_password=url_password, http_agent=http_agent, |
| 121 |
force_basic_auth=force_basic_auth) |
| 122 |
except HTTPError as e: |
| 123 |
r = e.fp |
| 124 |
|
| 125 |
try: |
| 126 |
raw_data = r.read() |
| 127 |
if raw_data: |
| 128 |
data = json.loads(raw_data) |
| 129 |
else: |
| 130 |
raw_data = None |
| 131 |
except Exception: |
| 132 |
if ignore_errors: |
| 133 |
pass |
| 134 |
else: |
| 135 |
raise Exception(raw_data) |
| 136 |
|
| 137 |
resp_code = r.getcode() |
| 138 |
|
| 139 |
if resp_code >= 400 and not ignore_errors: |
| 140 |
raise Exception(resp_code, data) |
| 141 |
else: |
| 142 |
return resp_code, data |
| 143 |
|
| 144 |
|
| 145 |
def get_ssid(module, name, api_url, user, pwd): |
| 146 |
count = 0 |
| 147 |
ssid = None |
| 148 |
all_systems = 'storage-systems' |
| 149 |
systems_url = api_url + all_systems |
| 150 |
rc, data = request(systems_url, headers=HEADERS, url_username=user, url_password=pwd, |
| 151 |
validate_certs=module.validate_certs) |
| 152 |
for system in data: |
| 153 |
if system['name'] == name: |
| 154 |
count += 1 |
| 155 |
if count > 1: |
| 156 |
module.fail_json( |
| 157 |
msg="You supplied a name for the Storage Array but more than 1 array was found with that name. " + |
| 158 |
"Use the id instead") |
| 159 |
else: |
| 160 |
ssid = system['id'] |
| 161 |
else: |
| 162 |
continue |
| 163 |
|
| 164 |
if count == 0: |
| 165 |
module.fail_json(msg="No storage array with the name %s was found" % name) |
| 166 |
|
| 167 |
else: |
| 168 |
return ssid |
| 169 |
|
| 170 |
|
| 171 |
def get_pwd_status(module, ssid, api_url, user, pwd): |
| 172 |
pwd_status = "storage-systems/%s/passwords" % ssid |
| 173 |
url = api_url + pwd_status |
| 174 |
try: |
| 175 |
rc, data = request(url, headers=HEADERS, url_username=user, url_password=pwd, |
| 176 |
validate_certs=module.validate_certs) |
| 177 |
return data['readOnlyPasswordSet'], data['adminPasswordSet'] |
| 178 |
except HTTPError as e: |
| 179 |
module.fail_json(msg="There was an issue with connecting, please check that your " |
| 180 |
"endpoint is properly defined and your credentials are correct: %s" % to_native(e)) |
| 181 |
|
| 182 |
|
| 183 |
def update_storage_system_pwd(module, ssid, pwd, api_url, api_usr, api_pwd): |
| 184 |
"""Update the stored storage-system password""" |
| 185 |
update_pwd = 'storage-systems/%s' % ssid |
| 186 |
url = api_url + update_pwd |
| 187 |
post_body = json.dumps(dict(storedPassword=pwd)) |
| 188 |
try: |
| 189 |
rc, data = request(url, data=post_body, method='POST', headers=HEADERS, url_username=api_usr, |
| 190 |
url_password=api_pwd, validate_certs=module.validate_certs) |
| 191 |
return rc, data |
| 192 |
except Exception as e: |
| 193 |
module.fail_json(msg="Failed to update system password. Id [%s]. Error [%s]" % (ssid, to_native(e))) |
| 194 |
|
| 195 |
|
| 196 |
def set_password(module, ssid, api_url, user, pwd, current_password=None, new_password=None, set_admin=False): |
| 197 |
"""Set the storage-system password""" |
| 198 |
set_pass = "storage-systems/%s/passwords" % ssid |
| 199 |
url = api_url + set_pass |
| 200 |
|
| 201 |
if not current_password: |
| 202 |
current_password = "" |
| 203 |
|
| 204 |
post_body = json.dumps( |
| 205 |
dict(currentAdminPassword=current_password, adminPassword=set_admin, newPassword=new_password)) |
| 206 |
|
| 207 |
try: |
| 208 |
rc, data = request(url, method='POST', data=post_body, headers=HEADERS, url_username=user, url_password=pwd, |
| 209 |
ignore_errors=True, validate_certs=module.validate_certs) |
| 210 |
except Exception as e: |
| 211 |
module.fail_json(msg="Failed to set system password. Id [%s]. Error [%s]" % (ssid, to_native(e)), |
| 212 |
exception=traceback.format_exc()) |
| 213 |
|
| 214 |
if rc == 422: |
| 215 |
post_body = json.dumps(dict(currentAdminPassword='', adminPassword=set_admin, newPassword=new_password)) |
| 216 |
try: |
| 217 |
rc, data = request(url, method='POST', data=post_body, headers=HEADERS, url_username=user, url_password=pwd, |
| 218 |
validate_certs=module.validate_certs) |
| 219 |
except Exception: |
| 220 |
# TODO(lorenp): Resolve ignored rc, data |
| 221 |
module.fail_json(msg="Wrong or no admin password supplied. Please update your playbook and try again") |
| 222 |
|
| 223 |
if int(rc) >= 300: |
| 224 |
module.fail_json(msg="Failed to set system password. Id [%s] Code [%s]. Error [%s]" % (ssid, rc, data)) |
| 225 |
|
| 226 |
rc, update_data = update_storage_system_pwd(module, ssid, new_password, api_url, user, pwd) |
| 227 |
|
| 228 |
if int(rc) < 300: |
| 229 |
return update_data |
| 230 |
else: |
| 231 |
module.fail_json(msg="%s:%s" % (rc, update_data)) |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
argument_spec = basic_auth_argument_spec() |
| 236 |
argument_spec.update(dict( |
| 237 |
name=dict(required=False, type='str'), |
| 238 |
ssid=dict(required=False, type='str'), |
| 239 |
current_password=dict(required=False, no_log=True), |
| 240 |
new_password=dict(required=True, no_log=True), |
| 241 |
set_admin=dict(required=False, type='bool', default=False), |
| 242 |
api_url=dict(required=False), |
| 243 |
api_username=dict(required=False), |
| 244 |
api_password=dict(required=False, no_log=True) |
| 245 |
) |
| 246 |
) |
| 247 |
module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[['name', 'ssid']], |
| 248 |
required_one_of=[['name', 'ssid']]) |
| 249 |
|
| 250 |
name = module.params['name'] |
| 251 |
ssid = module.params['ssid'] |
| 252 |
current_password = module.params['current_password'] |
| 253 |
new_password = module.params['new_password'] |
| 254 |
set_admin = module.params['set_admin'] |
| 255 |
user = module.params['api_username'] |
| 256 |
pwd = module.params['api_password'] |
| 257 |
api_url = module.params['api_url'] |
| 258 |
module.validate_certs = module.params['validate_certs'] |
| 259 |
|
| 260 |
if not api_url.endswith('/'): |
| 261 |
api_url += '/' |
| 262 |
|
| 263 |
if name: |
| 264 |
ssid = get_ssid(module, name, api_url, user, pwd) |
| 265 |
|
| 266 |
ro_pwd, admin_pwd = get_pwd_status(module, ssid, api_url, user, pwd) |
| 267 |
|
| 268 |
if admin_pwd and not current_password: |
| 269 |
module.fail_json( |
| 270 |
msg="Admin account has a password set. " + |
| 271 |
"You must supply current_password in order to update the RO or Admin passwords") |
| 272 |
|
| 273 |
if len(new_password) > 30: |
| 274 |
module.fail_json(msg="Passwords must not be greater than 30 characters in length") |
| 275 |
|
| 276 |
result = set_password(module, ssid, api_url, user, pwd, current_password=current_password, |
| 277 |
new_password=new_password, set_admin=set_admin) |
| 278 |
|
| 279 |
module.exit_json(changed=True, msg="Password Updated Successfully", |
| 280 |
password_set=result['passwordSet'], |
| 281 |
password_status=result['passwordStatus']) |
| 282 |
|
| 283 |
|
| 284 |
if __name__ == '__main__': |
| 285 |
main() |