| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
''' |
| 8 |
na_ontap_login_messages |
| 9 |
''' |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
|
| 15 |
DOCUMENTATION = ''' |
| 16 |
module: na_ontap_login_messages |
| 17 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp.ontap.netapp.na_ontap |
| 20 |
version_added: '20.1.0' |
| 21 |
short_description: Setup login banner and message of the day |
| 22 |
description: |
| 23 |
- This module allows you to manipulate login banner and motd for a vserver |
| 24 |
options: |
| 25 |
banner: |
| 26 |
description: |
| 27 |
- Login banner Text message. |
| 28 |
type: str |
| 29 |
vserver: |
| 30 |
description: |
| 31 |
- The name of the SVM login messages should be set for. |
| 32 |
- With ZAPI, this option is required. This a cluster or data SVM. |
| 33 |
- With REST, this is a data SVM. |
| 34 |
- With REST, cluster scope is assumed when this option is absent. |
| 35 |
type: str |
| 36 |
motd_message: |
| 37 |
description: |
| 38 |
- MOTD Text message. |
| 39 |
- message is deprecated and will be removed to avoid a conflict with an Ansible internal variable. |
| 40 |
type: str |
| 41 |
aliases: |
| 42 |
- message |
| 43 |
show_cluster_motd: |
| 44 |
description: |
| 45 |
- Set to I(false) if Cluster-level Message of the Day should not be shown |
| 46 |
type: bool |
| 47 |
default: True |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = """ |
| 51 |
- name: Modify vserver banner |
| 52 |
netapp.ontap.na_ontap_login_messages: |
| 53 |
vserver: trident_svm |
| 54 |
banner: this is trident vserver |
| 55 |
hostname: "{{ netapp_hostname }}" |
| 56 |
username: "{{ netapp_username }}" |
| 57 |
password: "{{ netapp_password }}" |
| 58 |
|
| 59 |
- name: Modify vserver motd |
| 60 |
netapp.ontap.na_ontap_login_messages: |
| 61 |
vserver: trident_svm |
| 62 |
motd_message: this is trident vserver |
| 63 |
show_cluster_motd: true |
| 64 |
hostname: "{{ netapp_hostname }}" |
| 65 |
username: "{{ netapp_username }}" |
| 66 |
password: "{{ netapp_password }}" |
| 67 |
|
| 68 |
- name: Modify motd cluster - REST |
| 69 |
netapp.ontap.na_ontap_login_messages: |
| 70 |
motd_message: this is a cluster motd with REST |
| 71 |
show_cluster_motd: true |
| 72 |
hostname: "{{ netapp_hostname }}" |
| 73 |
username: "{{ netapp_username }}" |
| 74 |
password: "{{ netapp_password }}" |
| 75 |
""" |
| 76 |
|
| 77 |
RETURN = """ |
| 78 |
""" |
| 79 |
|
| 80 |
import traceback |
| 81 |
from ansible.module_utils.basic import AnsibleModule |
| 82 |
from ansible.module_utils._text import to_native |
| 83 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 84 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 85 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 86 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 87 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver |
| 88 |
|
| 89 |
|
| 90 |
class NetAppOntapLoginMessages: |
| 91 |
""" |
| 92 |
modify and delete login banner and motd |
| 93 |
""" |
| 94 |
|
| 95 |
def __init__(self): |
| 96 |
self.use_rest = False |
| 97 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 98 |
self.argument_spec.update(dict( |
| 99 |
vserver=dict(type='str'), |
| 100 |
banner=dict(type='str'), |
| 101 |
motd_message=dict(type='str', aliases=['message']), |
| 102 |
show_cluster_motd=dict(default=True, type='bool') |
| 103 |
)) |
| 104 |
|
| 105 |
self.module = AnsibleModule( |
| 106 |
argument_spec=self.argument_spec, |
| 107 |
supports_check_mode=True, |
| 108 |
required_one_of=[['show_cluster_motd', 'banner', 'motd_message']] |
| 109 |
) |
| 110 |
|
| 111 |
self.na_helper = NetAppModule() |
| 112 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 113 |
|
| 114 |
self.rest_api = OntapRestAPI(self.module) |
| 115 |
if self.rest_api.is_rest(): |
| 116 |
self.use_rest = True |
| 117 |
else: |
| 118 |
if not netapp_utils.has_netapp_lib(): |
| 119 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 120 |
if not self.parameters.get('vserver'): |
| 121 |
self.module.fail_json(msg="Error: vserver is a required parameter when using ZAPI.") |
| 122 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 123 |
if 'message' in self.parameters: |
| 124 |
self.module.warn('Error: "message" option conflicts with Ansible internal variable - please use "motd_message".') |
| 125 |
|
| 126 |
def get_banner_motd(self): |
| 127 |
if self.use_rest: |
| 128 |
api = 'security/login/messages' |
| 129 |
query = { |
| 130 |
'fields': 'banner,message,show_cluster_message,uuid', |
| 131 |
'scope': 'cluster' |
| 132 |
} |
| 133 |
vserver = self.parameters.get('vserver') |
| 134 |
if vserver: |
| 135 |
query['scope'] = 'svm' |
| 136 |
query['svm.name'] = vserver |
| 137 |
|
| 138 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 139 |
if error: |
| 140 |
self.module.fail_json(msg='Error fetching login_banner info: %s' % error) |
| 141 |
if record is None and vserver is None: |
| 142 |
self.module.fail_json(msg='Error fetching login_banner info for cluster - no data.') |
| 143 |
return self.form_current(record) |
| 144 |
|
| 145 |
# ZAPI |
| 146 |
motd, show_cluster_motd = self.get_motd_zapi() |
| 147 |
return { |
| 148 |
'banner': self.get_login_banner_zapi(), |
| 149 |
'motd_message': motd, |
| 150 |
'show_cluster_motd': show_cluster_motd |
| 151 |
} |
| 152 |
|
| 153 |
def form_current(self, record): |
| 154 |
show_cluster_motd = True |
| 155 |
if record and record.get('show_cluster_message') is not None: |
| 156 |
show_cluster_motd = record.get('show_cluster_message') |
| 157 |
return_result = { |
| 158 |
'banner': '', |
| 159 |
'motd_message': '', |
| 160 |
# we need the SVM UUID to add banner or motd if they are not present |
| 161 |
'uuid': record['uuid'] if record else self.get_svm_uuid(self.parameters.get('vserver')), |
| 162 |
'show_cluster_motd': show_cluster_motd |
| 163 |
} |
| 164 |
# by default REST adds a trailing \n if no trailing \n set in desired message/banner. |
| 165 |
# rstip \n only when desired message/banner does not have trailing \n to preserve idempotency. |
| 166 |
if record and record.get('banner'): |
| 167 |
if self.parameters.get('banner', '').endswith('\n'): |
| 168 |
return_result['banner'] = record['banner'] |
| 169 |
else: |
| 170 |
return_result['banner'] = record['banner'].rstrip('\n') |
| 171 |
if record and record.get('message'): |
| 172 |
if self.parameters.get('motd_message', '').endswith('\n'): |
| 173 |
return_result['motd_message'] = record['message'] |
| 174 |
else: |
| 175 |
return_result['motd_message'] = record['message'].rstrip('\n') |
| 176 |
return return_result |
| 177 |
|
| 178 |
def get_login_banner_zapi(self): |
| 179 |
login_banner_get_iter = netapp_utils.zapi.NaElement('vserver-login-banner-get-iter') |
| 180 |
query = netapp_utils.zapi.NaElement('query') |
| 181 |
login_banner_info = netapp_utils.zapi.NaElement('vserver-login-banner-info') |
| 182 |
login_banner_info.add_new_child('vserver', self.parameters['vserver']) |
| 183 |
query.add_child_elem(login_banner_info) |
| 184 |
login_banner_get_iter.add_child_elem(query) |
| 185 |
try: |
| 186 |
result = self.server.invoke_successfully(login_banner_get_iter, enable_tunneling=True) |
| 187 |
except netapp_utils.zapi.NaApiError as error: |
| 188 |
self.module.fail_json(msg='Error fetching login_banner info: %s' % to_native(error), |
| 189 |
exception=traceback.format_exc()) |
| 190 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) > 0: |
| 191 |
login_banner_info = result.get_child_by_name('attributes-list').get_child_by_name( |
| 192 |
'vserver-login-banner-info') |
| 193 |
banner = login_banner_info.get_child_content('message') |
| 194 |
banner = str(banner).rstrip() |
| 195 |
# if the message is '-' that means the banner doesn't exist. |
| 196 |
if banner in ('-', 'None'): |
| 197 |
banner = '' |
| 198 |
return banner |
| 199 |
return None |
| 200 |
|
| 201 |
def get_motd_zapi(self): |
| 202 |
motd_get_iter = netapp_utils.zapi.NaElement('vserver-motd-get-iter') |
| 203 |
query = netapp_utils.zapi.NaElement('query') |
| 204 |
motd_info = netapp_utils.zapi.NaElement('vserver-motd-info') |
| 205 |
motd_info.add_new_child('vserver', self.parameters['vserver']) |
| 206 |
query.add_child_elem(motd_info) |
| 207 |
motd_get_iter.add_child_elem(query) |
| 208 |
try: |
| 209 |
result = self.server.invoke_successfully(motd_get_iter, enable_tunneling=True) |
| 210 |
except netapp_utils.zapi.NaApiError as error: |
| 211 |
self.module.fail_json(msg='Error fetching motd info: %s' % to_native(error), |
| 212 |
exception=traceback.format_exc()) |
| 213 |
if result.get_child_by_name('num-records') and \ |
| 214 |
int(result.get_child_content('num-records')) > 0: |
| 215 |
motd_info = result.get_child_by_name('attributes-list').get_child_by_name( |
| 216 |
'vserver-motd-info') |
| 217 |
motd_message = motd_info.get_child_content('message') |
| 218 |
motd_message = str(motd_message).rstrip() |
| 219 |
if motd_message == 'None': |
| 220 |
motd_message = '' |
| 221 |
show_cluster_motd = motd_info.get_child_content('is-cluster-message-enabled') == 'true' |
| 222 |
return motd_message, show_cluster_motd |
| 223 |
return '', False |
| 224 |
|
| 225 |
def modify_rest(self, modify, uuid): |
| 226 |
body = { |
| 227 |
} |
| 228 |
if 'banner' in modify: |
| 229 |
body['banner'] = modify['banner'] |
| 230 |
if 'motd_message' in modify: |
| 231 |
body['message'] = modify['motd_message'] |
| 232 |
if modify.get('show_cluster_motd') is not None: |
| 233 |
body['show_cluster_message'] = modify['show_cluster_motd'] |
| 234 |
if body: |
| 235 |
api = 'security/login/messages' |
| 236 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 237 |
if error: |
| 238 |
keys = list(body.keys()) |
| 239 |
self.module.fail_json(msg='Error modifying %s: %s' % (', '.join(keys), error)) |
| 240 |
|
| 241 |
def modify_banner(self, modify): |
| 242 |
login_banner_modify = netapp_utils.zapi.NaElement('vserver-login-banner-modify-iter') |
| 243 |
login_banner_modify.add_new_child('message', modify['banner']) |
| 244 |
query = netapp_utils.zapi.NaElement('query') |
| 245 |
login_banner_info = netapp_utils.zapi.NaElement('vserver-login-banner-info') |
| 246 |
login_banner_info.add_new_child('vserver', self.parameters['vserver']) |
| 247 |
query.add_child_elem(login_banner_info) |
| 248 |
login_banner_modify.add_child_elem(query) |
| 249 |
try: |
| 250 |
self.server.invoke_successfully(login_banner_modify, enable_tunneling=False) |
| 251 |
except netapp_utils.zapi.NaApiError as err: |
| 252 |
self.module.fail_json(msg="Error modifying login_banner: %s" % (to_native(err)), |
| 253 |
exception=traceback.format_exc()) |
| 254 |
|
| 255 |
def modify_motd(self, modify): |
| 256 |
motd_create = netapp_utils.zapi.NaElement('vserver-motd-modify-iter') |
| 257 |
if modify.get('motd_message') is not None: |
| 258 |
motd_create.add_new_child('message', modify['motd_message']) |
| 259 |
if modify.get('show_cluster_motd') is not None: |
| 260 |
motd_create.add_new_child('is-cluster-message-enabled', 'true' if modify['show_cluster_motd'] is True else 'false') |
| 261 |
query = netapp_utils.zapi.NaElement('query') |
| 262 |
motd_info = netapp_utils.zapi.NaElement('vserver-motd-info') |
| 263 |
motd_info.add_new_child('vserver', self.parameters['vserver']) |
| 264 |
query.add_child_elem(motd_info) |
| 265 |
motd_create.add_child_elem(query) |
| 266 |
try: |
| 267 |
self.server.invoke_successfully(motd_create, enable_tunneling=False) |
| 268 |
except netapp_utils.zapi.NaApiError as err: |
| 269 |
self.module.fail_json(msg="Error modifying motd: %s" % (to_native(err)), |
| 270 |
exception=traceback.format_exc()) |
| 271 |
|
| 272 |
def get_svm_uuid(self, vserver): |
| 273 |
""" |
| 274 |
Get a svm's uuid |
| 275 |
:return: uuid of the svm |
| 276 |
""" |
| 277 |
uuid, error = rest_vserver.get_vserver_uuid(self.rest_api, vserver) |
| 278 |
if error is not None: |
| 279 |
self.module.fail_json(msg="Error fetching vserver %s: %s" % (vserver, error)) |
| 280 |
if uuid is None: |
| 281 |
self.module.fail_json(msg="Error fetching vserver %s. Please make sure vserver name is correct. For cluster vserver, don't set vserver." |
| 282 |
% vserver) |
| 283 |
return uuid |
| 284 |
|
| 285 |
def apply(self): |
| 286 |
current = self.get_banner_motd() |
| 287 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 288 |
if self.na_helper.changed and not self.module.check_mode: |
| 289 |
if self.use_rest: |
| 290 |
self.modify_rest(modify, current['uuid']) |
| 291 |
else: |
| 292 |
if modify.get('banner') is not None: |
| 293 |
self.modify_banner(modify) |
| 294 |
if modify.get('show_cluster_motd') is not None or modify.get('motd_message') is not None: |
| 295 |
self.modify_motd(modify) |
| 296 |
result = netapp_utils.generate_result(self.na_helper.changed, modify=modify) |
| 297 |
self.module.exit_json(**result) |
| 298 |
|
| 299 |
|
| 300 |
def main(): |
| 301 |
'''Execute action from playbook''' |
| 302 |
messages_obj = NetAppOntapLoginMessages() |
| 303 |
messages_obj.apply() |
| 304 |
|
| 305 |
|
| 306 |
if __name__ == '__main__': |
| 307 |
main() |