| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# (c) 2018 Piotr Olczak <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
module: na_ontap_motd |
| 13 |
author: |
| 14 |
- Piotr Olczak (@dprts) <[email protected]> |
| 15 |
- NetApp Ansible Team (@carchi8py) <[email protected]> |
| 16 |
extends_documentation_fragment: |
| 17 |
- netapp.ontap.netapp.na_ontap_zapi |
| 18 |
short_description: Setup motd |
| 19 |
description: |
| 20 |
- This module allows you to manipulate motd for a vserver |
| 21 |
- It also allows to manipulate motd at the cluster level by using the cluster vserver (cserver) |
| 22 |
version_added: 2.7.0 |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(state=present) sets MOTD given in I(message) C(state=absent) removes it. |
| 27 |
choices: ['present', 'absent'] |
| 28 |
type: str |
| 29 |
default: present |
| 30 |
motd_message: |
| 31 |
description: |
| 32 |
- MOTD Text message. |
| 33 |
- message is deprecated and will be removed to avoid a conflict with an Ansible internal variable. |
| 34 |
type: str |
| 35 |
default: '' |
| 36 |
aliases: |
| 37 |
- message |
| 38 |
vserver: |
| 39 |
description: |
| 40 |
- The name of the SVM motd should be set for. |
| 41 |
required: true |
| 42 |
type: str |
| 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 |
|
| 51 |
EXAMPLES = ''' |
| 52 |
- name: Set Cluster-Level MOTD |
| 53 |
netapp.ontap.na_ontap_motd: |
| 54 |
vserver: my_ontap_cluster |
| 55 |
motd_message: "Cluster wide MOTD" |
| 56 |
hostname: "{{ netapp_hostname }}" |
| 57 |
username: "{{ netapp_username }}" |
| 58 |
password: "{{ netapp_password }}" |
| 59 |
state: present |
| 60 |
https: true |
| 61 |
|
| 62 |
- name: Set MOTD for I(rhev_nfs_krb) SVM, do not show Cluster-Level MOTD |
| 63 |
netapp.ontap.na_ontap_motd: |
| 64 |
vserver: rhev_nfs_krb |
| 65 |
motd_message: "Access to rhev_nfs_krb is also restricted" |
| 66 |
hostname: "{{ netapp_hostname }}" |
| 67 |
username: "{{ netapp_username }}" |
| 68 |
password: "{{ netapp_password }}" |
| 69 |
state: present |
| 70 |
show_cluster_motd: false |
| 71 |
https: true |
| 72 |
|
| 73 |
- name: Remove Cluster-Level MOTD |
| 74 |
netapp.ontap.na_ontap_motd: |
| 75 |
vserver: my_ontap_cluster |
| 76 |
hostname: "{{ netapp_hostname }}" |
| 77 |
username: "{{ netapp_username }}" |
| 78 |
password: "{{ netapp_password }}" |
| 79 |
state: absent |
| 80 |
https: true |
| 81 |
''' |
| 82 |
|
| 83 |
RETURN = ''' |
| 84 |
|
| 85 |
''' |
| 86 |
|
| 87 |
import traceback |
| 88 |
from ansible.module_utils.basic import AnsibleModule |
| 89 |
from ansible.module_utils._text import to_native |
| 90 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 91 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 92 |
|
| 93 |
|
| 94 |
class NetAppONTAPMotd: |
| 95 |
|
| 96 |
def __init__(self): |
| 97 |
argument_spec = netapp_utils.na_ontap_zapi_only_spec() |
| 98 |
argument_spec.update(dict( |
| 99 |
state=dict(required=False, type='str', default='present', choices=['present', 'absent']), |
| 100 |
vserver=dict(required=True, type='str'), |
| 101 |
motd_message=dict(default='', type='str', aliases=['message']), |
| 102 |
show_cluster_motd=dict(default=True, type='bool') |
| 103 |
)) |
| 104 |
|
| 105 |
self.module = AnsibleModule( |
| 106 |
argument_spec=argument_spec, |
| 107 |
supports_check_mode=True |
| 108 |
) |
| 109 |
|
| 110 |
self.na_helper = NetAppModule() |
| 111 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 112 |
self.na_helper.module_replaces('na_ontap_login_messages', self.module) |
| 113 |
|
| 114 |
msg = 'The module only supports ZAPI; refer to netapp.ontap.na_ontap_login_messages module for RESTful equivalent.' |
| 115 |
if self.parameters['use_rest'].lower() == 'never': |
| 116 |
self.module.warn(msg) |
| 117 |
else: |
| 118 |
self.na_helper.fall_back_to_zapi(self.module, msg, self.parameters) |
| 119 |
if 'message' in self.parameters: |
| 120 |
self.module.warn('Error: "message" option conflicts with Ansible internal variable - please use "motd_message".') |
| 121 |
|
| 122 |
if not netapp_utils.has_netapp_lib(): |
| 123 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 124 |
|
| 125 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 126 |
|
| 127 |
def motd_get_iter(self): |
| 128 |
""" |
| 129 |
Compose NaElement object to query current motd |
| 130 |
:return: NaElement object for vserver-motd-get-iter |
| 131 |
""" |
| 132 |
motd_get_iter = netapp_utils.zapi.NaElement('vserver-motd-get-iter') |
| 133 |
query = netapp_utils.zapi.NaElement('query') |
| 134 |
motd_info = netapp_utils.zapi.NaElement('vserver-motd-info') |
| 135 |
motd_info.add_new_child('is-cluster-message-enabled', str(self.parameters['show_cluster_motd'])) |
| 136 |
motd_info.add_new_child('vserver', self.parameters['vserver']) |
| 137 |
query.add_child_elem(motd_info) |
| 138 |
motd_get_iter.add_child_elem(query) |
| 139 |
return motd_get_iter |
| 140 |
|
| 141 |
def motd_get(self): |
| 142 |
""" |
| 143 |
Get current motd |
| 144 |
:return: Dictionary of current motd details if query successful, else None |
| 145 |
""" |
| 146 |
motd_get_iter = self.motd_get_iter() |
| 147 |
motd_result = {} |
| 148 |
try: |
| 149 |
result = self.server.invoke_successfully(motd_get_iter, enable_tunneling=True) |
| 150 |
except netapp_utils.zapi.NaApiError as error: |
| 151 |
self.module.fail_json(msg='Error fetching motd info: %s' % to_native(error), |
| 152 |
exception=traceback.format_exc()) |
| 153 |
if result.get_child_by_name('num-records') and \ |
| 154 |
int(result.get_child_content('num-records')) > 0: |
| 155 |
motd_info = result.get_child_by_name('attributes-list').get_child_by_name( |
| 156 |
'vserver-motd-info') |
| 157 |
motd_result['motd_message'] = motd_info.get_child_content('message') |
| 158 |
motd_result['motd_message'] = str(motd_result['motd_message']).rstrip() |
| 159 |
motd_result['show_cluster_motd'] = motd_info.get_child_content('is-cluster-message-enabled') == 'true' |
| 160 |
motd_result['vserver'] = motd_info.get_child_content('vserver') |
| 161 |
return motd_result |
| 162 |
return None |
| 163 |
|
| 164 |
def modify_motd(self): |
| 165 |
motd_create = netapp_utils.zapi.NaElement('vserver-motd-modify-iter') |
| 166 |
motd_create.add_new_child('message', self.parameters['motd_message']) |
| 167 |
motd_create.add_new_child( |
| 168 |
'is-cluster-message-enabled', 'true' if self.parameters['show_cluster_motd'] is True else 'false') |
| 169 |
query = netapp_utils.zapi.NaElement('query') |
| 170 |
motd_info = netapp_utils.zapi.NaElement('vserver-motd-info') |
| 171 |
motd_info.add_new_child('vserver', self.parameters['vserver']) |
| 172 |
query.add_child_elem(motd_info) |
| 173 |
motd_create.add_child_elem(query) |
| 174 |
try: |
| 175 |
self.server.invoke_successfully(motd_create, enable_tunneling=False) |
| 176 |
except netapp_utils.zapi.NaApiError as err: |
| 177 |
self.module.fail_json(msg="Error creating motd: %s" % (to_native(err)), exception=traceback.format_exc()) |
| 178 |
return motd_create |
| 179 |
|
| 180 |
def apply(self): |
| 181 |
""" |
| 182 |
Applies action from playbook |
| 183 |
""" |
| 184 |
current = self.motd_get() |
| 185 |
if self.parameters['state'] == 'absent': |
| 186 |
# Just make sure it is empty |
| 187 |
self.parameters['motd_message'] = '' |
| 188 |
if current and current['motd_message'] == 'None': |
| 189 |
current = None |
| 190 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 191 |
if cd_action is None and self.parameters['state'] == 'present': |
| 192 |
self.na_helper.get_modified_attributes(current, self.parameters) |
| 193 |
|
| 194 |
if self.na_helper.changed and not self.module.check_mode: |
| 195 |
self.modify_motd() |
| 196 |
self.module.exit_json(changed=self.na_helper.changed) |
| 197 |
|
| 198 |
|
| 199 |
def main(): |
| 200 |
motd_obj = NetAppONTAPMotd() |
| 201 |
motd_obj.apply() |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == '__main__': |
| 205 |
main() |