| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-2026, 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 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: na_ontap_local_hosts |
| 12 |
short_description: NetApp ONTAP local hosts |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: 22.0.0 |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Create or delete or modify local hosts in ONTAP. |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Whether the specified local hosts should exist or not. |
| 23 |
choices: ['present', 'absent'] |
| 24 |
type: str |
| 25 |
default: 'present' |
| 26 |
owner: |
| 27 |
description: |
| 28 |
- Name of the data SVM or cluster. |
| 29 |
required: True |
| 30 |
type: str |
| 31 |
aliases: |
| 32 |
description: |
| 33 |
- The list of aliases. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
host: |
| 37 |
description: |
| 38 |
- Canonical hostname. |
| 39 |
- minimum length is 1 and maximum length is 255. |
| 40 |
type: str |
| 41 |
address: |
| 42 |
description: |
| 43 |
- IPv4/IPv6 address in dotted form. |
| 44 |
required: True |
| 45 |
type: str |
| 46 |
lambda_config: |
| 47 |
description: |
| 48 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 49 |
- These option and suboptions are only supported with REST. |
| 50 |
type: dict |
| 51 |
version_added: 23.5.0 |
| 52 |
suboptions: |
| 53 |
function_name: |
| 54 |
description: |
| 55 |
- The name of the AWS Lambda function to invoke. |
| 56 |
type: str |
| 57 |
required: true |
| 58 |
aws_region: |
| 59 |
description: |
| 60 |
- The name of the AWS region. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
aws_profile: |
| 64 |
description: |
| 65 |
- The name of the AWS profile to use for authentication. |
| 66 |
type: str |
| 67 |
|
| 68 |
notes: |
| 69 |
- Supports AWS Lambda proxy functionality. See the README file for examples. |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = """ |
| 73 |
- name: Create IP to host mapping |
| 74 |
netapp.ontap.na_ontap_local_hosts: |
| 75 |
state: present |
| 76 |
address: 10.10.10.10 |
| 77 |
host: example.com |
| 78 |
aliases: ['ex1.com', 'ex2.com'] |
| 79 |
owner: svm1 |
| 80 |
hostname: "{{ netapp_hostname }}" |
| 81 |
username: "{{ netapp_username }}" |
| 82 |
password: "{{ netapp_password }}" |
| 83 |
|
| 84 |
- name: Modify IP to host mapping |
| 85 |
netapp.ontap.na_ontap_local_hosts: |
| 86 |
state: present |
| 87 |
address: 10.10.10.10 |
| 88 |
owner: svm1 |
| 89 |
host: example1.com |
| 90 |
aliases: ['ex1.com', 'ex2.com', 'ex3.com'] |
| 91 |
hostname: "{{ netapp_hostname }}" |
| 92 |
username: "{{ netapp_username }}" |
| 93 |
password: "{{ netapp_password }}" |
| 94 |
|
| 95 |
- name: Delete host object |
| 96 |
netapp.ontap.na_ontap_local_hosts: |
| 97 |
state: absent |
| 98 |
address: 10.10.10.10 |
| 99 |
owner: svm1 |
| 100 |
hostname: "{{ netapp_hostname }}" |
| 101 |
username: "{{ netapp_username }}" |
| 102 |
password: "{{ netapp_password }}" |
| 103 |
""" |
| 104 |
|
| 105 |
RETURN = """ |
| 106 |
""" |
| 107 |
import traceback |
| 108 |
|
| 109 |
from ansible.module_utils.basic import AnsibleModule |
| 110 |
from ansible.module_utils._text import to_native |
| 111 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 112 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 113 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, netapp_ipaddress |
| 114 |
|
| 115 |
|
| 116 |
class NetAppOntapLocalHosts: |
| 117 |
""" object initialize and class methods """ |
| 118 |
def __init__(self): |
| 119 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 120 |
self.argument_spec.update(dict( |
| 121 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 122 |
owner=dict(required=True, type='str'), |
| 123 |
address=dict(required=True, type='str'), |
| 124 |
aliases=dict(required=False, type='list', elements='str'), |
| 125 |
host=dict(required=False, type='str'), |
| 126 |
)) |
| 127 |
|
| 128 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 129 |
|
| 130 |
self.module = AnsibleModule( |
| 131 |
argument_spec=self.argument_spec, |
| 132 |
supports_check_mode=True, |
| 133 |
required_if=[ |
| 134 |
('use_lambda', True, ['lambda_config']) |
| 135 |
] |
| 136 |
) |
| 137 |
self.na_helper = NetAppModule() |
| 138 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 139 |
self.parameters['address'] = netapp_ipaddress.validate_and_compress_ip_address(self.parameters['address'], self.module) |
| 140 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 141 |
self.owner_uuid = None |
| 142 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_local_hosts', 9, 10, 1) |
| 143 |
|
| 144 |
def get_local_host_rest(self): |
| 145 |
''' |
| 146 |
Retrieves IP to hostname mapping for SVM of the cluster. |
| 147 |
''' |
| 148 |
api = 'name-services/local-hosts' |
| 149 |
query = {'owner.name': self.parameters['owner'], |
| 150 |
'address': self.parameters['address'], |
| 151 |
'fields': 'address,hostname,owner.name,owner.uuid,aliases'} |
| 152 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 153 |
if error: |
| 154 |
self.module.fail_json(msg='Error fetching IP to hostname mappings for %s: %s' % (self.parameters['owner'], to_native(error)), |
| 155 |
exception=traceback.format_exc()) |
| 156 |
if record: |
| 157 |
self.owner_uuid = record['owner']['uuid'] |
| 158 |
return { |
| 159 |
'address': self.na_helper.safe_get(record, ['address']), |
| 160 |
'host': self.na_helper.safe_get(record, ['hostname']), |
| 161 |
'aliases': self.na_helper.safe_get(record, ['aliases']) |
| 162 |
} |
| 163 |
return record |
| 164 |
|
| 165 |
def create_local_host_rest(self): |
| 166 |
''' |
| 167 |
Creates a new IP to hostname mapping. |
| 168 |
''' |
| 169 |
api = 'name-services/local-hosts' |
| 170 |
body = {'owner.name': self.parameters.get('owner'), |
| 171 |
'address': self.parameters.get('address'), |
| 172 |
'hostname': self.parameters.get('host')} |
| 173 |
if 'aliases' in self.parameters: |
| 174 |
body['aliases'] = self.parameters.get('aliases') |
| 175 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 176 |
if error: |
| 177 |
self.module.fail_json(msg='Error creating IP to hostname mappings for %s: %s' % (self.parameters['owner'], to_native(error)), |
| 178 |
exception=traceback.format_exc()) |
| 179 |
|
| 180 |
def modify_local_host_rest(self, modify): |
| 181 |
''' |
| 182 |
For a specified SVM and IP address, modifies the corresponding IP to hostname mapping. |
| 183 |
''' |
| 184 |
body = {} |
| 185 |
if 'aliases' in modify: |
| 186 |
body['aliases'] = self.parameters['aliases'] |
| 187 |
if 'host' in modify: |
| 188 |
body['hostname'] = self.parameters['host'] |
| 189 |
api = 'name-services/local-hosts/%s/%s' % (self.owner_uuid, self.parameters['address']) |
| 190 |
if body: |
| 191 |
dummy, error = rest_generic.patch_async(self.rest_api, api, None, body) |
| 192 |
if error: |
| 193 |
self.module.fail_json(msg='Error updating IP to hostname mappings for %s: %s' % (self.parameters['owner'], to_native(error)), |
| 194 |
exception=traceback.format_exc()) |
| 195 |
|
| 196 |
def delete_local_host_rest(self): |
| 197 |
''' |
| 198 |
vserver services name-service dns hosts delete. |
| 199 |
''' |
| 200 |
api = 'name-services/local-hosts/%s/%s' % (self.owner_uuid, self.parameters['address']) |
| 201 |
dummy, error = rest_generic.delete_async(self.rest_api, api, None) |
| 202 |
if error: |
| 203 |
self.module.fail_json(msg='Error deleting IP to hostname mappings for %s: %s' % (self.parameters['owner'], to_native(error)), |
| 204 |
exception=traceback.format_exc()) |
| 205 |
|
| 206 |
def apply(self): |
| 207 |
cd_action = None |
| 208 |
current = self.get_local_host_rest() |
| 209 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 210 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None |
| 211 |
if self.na_helper.changed and not self.module.check_mode: |
| 212 |
if cd_action == 'create': |
| 213 |
self.create_local_host_rest() |
| 214 |
elif cd_action == 'delete': |
| 215 |
self.delete_local_host_rest() |
| 216 |
elif modify: |
| 217 |
self.modify_local_host_rest(modify) |
| 218 |
self.module.exit_json(changed=self.na_helper.changed) |
| 219 |
|
| 220 |
|
| 221 |
def main(): |
| 222 |
""" Create object and call apply """ |
| 223 |
hosts_obj = NetAppOntapLocalHosts() |
| 224 |
hosts_obj.apply() |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == '__main__': |
| 228 |
main() |