| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 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 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: intersight_iscsi_static_target_policy |
| 16 |
short_description: Manage iSCSI Static Target Policies for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete iSCSI Static Target Policies on Cisco Intersight. |
| 19 |
- iSCSI static target policy enables you to configure the iSCSI targets for iSCSI vNIC interfaces. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/vnic/IscsiStaticTargetPolicy/get/). |
| 21 |
extends_documentation_fragment: intersight |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- If C(present), will verify the resource is present and will create if needed. |
| 26 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 27 |
type: str |
| 28 |
choices: [present, absent] |
| 29 |
default: present |
| 30 |
organization: |
| 31 |
description: |
| 32 |
- The name of the Organization this resource is assigned to. |
| 33 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 34 |
- Use 'default' for the default organization. |
| 35 |
type: str |
| 36 |
default: default |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name assigned to the iSCSI Static Target Policy. |
| 40 |
- Must be unique within the organization. |
| 41 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
description: |
| 45 |
description: |
| 46 |
- The user-defined description for the iSCSI Static Target Policy. |
| 47 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 48 |
type: str |
| 49 |
aliases: [descr] |
| 50 |
tags: |
| 51 |
description: |
| 52 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 53 |
type: list |
| 54 |
elements: dict |
| 55 |
target_name: |
| 56 |
description: |
| 57 |
- Qualified Name (IQN) or Extended Unique Identifier (EUI) name of the iSCSI target. |
| 58 |
- This is the name that uniquely identifies the target in the iSCSI network. |
| 59 |
- Example IQN format - iqn.1991-05.com.microsoft:winclient1 |
| 60 |
- Example EUI format - eui.02004567A425678D |
| 61 |
- Required when state is present. |
| 62 |
type: str |
| 63 |
port: |
| 64 |
description: |
| 65 |
- The port associated with the iSCSI target. |
| 66 |
- Valid range is 1-65535. |
| 67 |
- Common iSCSI port is 3260. |
| 68 |
- Required when state is present. |
| 69 |
type: int |
| 70 |
lun_id: |
| 71 |
description: |
| 72 |
- The Logical Unit Number (LUN) Identifier for the iSCSI target. |
| 73 |
- Valid values are 0 or greater (typically 0-255). |
| 74 |
- Required when state is present. |
| 75 |
type: int |
| 76 |
ip_protocol: |
| 77 |
description: |
| 78 |
- Type of the IP address requested for iSCSI vNIC. |
| 79 |
- IPv4 or IPv6. |
| 80 |
- Required when state is present. |
| 81 |
type: str |
| 82 |
choices: ['IPv4', 'IPv6'] |
| 83 |
ipv4_address: |
| 84 |
description: |
| 85 |
- The IPv4 address assigned to the iSCSI target. |
| 86 |
- Required when ip_protocol is IPv4 and state is present. |
| 87 |
type: str |
| 88 |
ipv6_address: |
| 89 |
description: |
| 90 |
- The IPv6 address assigned to the iSCSI target. |
| 91 |
- Required when ip_protocol is IPv6 and state is present. |
| 92 |
type: str |
| 93 |
author: |
| 94 |
- Ron Gershburg (@rgershbu) |
| 95 |
''' |
| 96 |
|
| 97 |
EXAMPLES = r''' |
| 98 |
- name: Create an iSCSI Static Target Policy with IPv4 |
| 99 |
cisco.intersight.intersight_iscsi_static_target_policy: |
| 100 |
api_private_key: "{{ api_private_key }}" |
| 101 |
api_key_id: "{{ api_key_id }}" |
| 102 |
organization: "default" |
| 103 |
name: "iscsi-static-target-policy-01" |
| 104 |
description: "iSCSI static target policy for production servers" |
| 105 |
target_name: "iqn.1991-05.com.microsoft:winclient1" |
| 106 |
port: 3260 |
| 107 |
lun_id: 0 |
| 108 |
ip_protocol: "IPv4" |
| 109 |
ipv4_address: "192.168.10.100" |
| 110 |
tags: |
| 111 |
- Key: "Environment" |
| 112 |
Value: "Production" |
| 113 |
- Key: "Owner" |
| 114 |
Value: "Storage-Team" |
| 115 |
state: present |
| 116 |
|
| 117 |
- name: Create an iSCSI Static Target Policy with IPv6 |
| 118 |
cisco.intersight.intersight_iscsi_static_target_policy: |
| 119 |
api_private_key: "{{ api_private_key }}" |
| 120 |
api_key_id: "{{ api_key_id }}" |
| 121 |
organization: "default" |
| 122 |
name: "iscsi-static-target-policy-ipv6" |
| 123 |
description: "iSCSI static target policy with IPv6 addressing" |
| 124 |
target_name: "iqn.2001-04.com.example:storage.disk2" |
| 125 |
port: 3260 |
| 126 |
lun_id: 1 |
| 127 |
ip_protocol: "IPv6" |
| 128 |
ipv6_address: "2001:0db8:85a3:0000:0000:8a2e:0370:7334" |
| 129 |
state: present |
| 130 |
|
| 131 |
- name: Update an existing iSCSI Static Target Policy |
| 132 |
cisco.intersight.intersight_iscsi_static_target_policy: |
| 133 |
api_private_key: "{{ api_private_key }}" |
| 134 |
api_key_id: "{{ api_key_id }}" |
| 135 |
name: "iscsi-static-target-policy-01" |
| 136 |
description: "Updated description for iSCSI target policy" |
| 137 |
target_name: "iqn.1991-05.com.microsoft:winclient1" |
| 138 |
port: 3260 |
| 139 |
lun_id: 2 |
| 140 |
ip_protocol: "IPv4" |
| 141 |
ipv4_address: "192.168.10.101" |
| 142 |
state: present |
| 143 |
|
| 144 |
- name: Delete an iSCSI Static Target Policy |
| 145 |
cisco.intersight.intersight_iscsi_static_target_policy: |
| 146 |
api_private_key: "{{ api_private_key }}" |
| 147 |
api_key_id: "{{ api_key_id }}" |
| 148 |
name: "iscsi-static-target-policy-01" |
| 149 |
state: absent |
| 150 |
''' |
| 151 |
|
| 152 |
RETURN = r''' |
| 153 |
api_response: |
| 154 |
description: The API response output returned by the specified resource. |
| 155 |
returned: always |
| 156 |
type: dict |
| 157 |
sample: |
| 158 |
"api_response": { |
| 159 |
"Name": "iscsi-static-target-policy-01", |
| 160 |
"ObjectType": "vnic.IscsiStaticTargetPolicy", |
| 161 |
"Moid": "1234567890abcdef12345678", |
| 162 |
"TargetName": "iqn.1991-05.com.microsoft:winclient1", |
| 163 |
"Port": 3260, |
| 164 |
"Lun": { |
| 165 |
"LunId": 0 |
| 166 |
}, |
| 167 |
"IscsiIpType": "IPv4", |
| 168 |
"IpAddress": "192.168.10.100", |
| 169 |
"Organization": { |
| 170 |
"Moid": "abcdef1234567890abcdef12", |
| 171 |
"ObjectType": "organization.Organization" |
| 172 |
}, |
| 173 |
"Tags": [ |
| 174 |
{ |
| 175 |
"Key": "Environment", |
| 176 |
"Value": "Production" |
| 177 |
} |
| 178 |
] |
| 179 |
} |
| 180 |
''' |
| 181 |
|
| 182 |
|
| 183 |
from ansible.module_utils.basic import AnsibleModule |
| 184 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 185 |
|
| 186 |
|
| 187 |
def main(): |
| 188 |
argument_spec = intersight_argument_spec.copy() |
| 189 |
argument_spec.update( |
| 190 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 191 |
organization=dict(type='str', default='default'), |
| 192 |
name=dict(type='str', required=True), |
| 193 |
description=dict(type='str', aliases=['descr']), |
| 194 |
tags=dict(type='list', elements='dict'), |
| 195 |
target_name=dict(type='str'), |
| 196 |
port=dict(type='int'), |
| 197 |
lun_id=dict(type='int'), |
| 198 |
ip_protocol=dict(type='str', choices=['IPv4', 'IPv6']), |
| 199 |
ipv4_address=dict(type='str'), |
| 200 |
ipv6_address=dict(type='str') |
| 201 |
) |
| 202 |
module = AnsibleModule( |
| 203 |
argument_spec, |
| 204 |
supports_check_mode=True, |
| 205 |
required_if=[ |
| 206 |
['state', 'present', ['target_name', 'port', 'lun_id', 'ip_protocol']], |
| 207 |
['ip_protocol', 'IPv4', ['ipv4_address']], |
| 208 |
['ip_protocol', 'IPv6', ['ipv6_address']] |
| 209 |
], |
| 210 |
) |
| 211 |
|
| 212 |
intersight = IntersightModule(module) |
| 213 |
intersight.result['api_response'] = {} |
| 214 |
intersight.result['trace_id'] = '' |
| 215 |
|
| 216 |
# Validate port range (1-65535) |
| 217 |
if module.params['state'] == 'present' and module.params.get('port'): |
| 218 |
port = module.params['port'] |
| 219 |
if port < 1 or port > 65535: |
| 220 |
module.fail_json(msg=f"Port must be between 1 and 65535. Provided value: {port}") |
| 221 |
|
| 222 |
# Validate LUN ID (>= 0) |
| 223 |
if module.params['state'] == 'present' and module.params.get('lun_id') is not None: |
| 224 |
lun_id = module.params['lun_id'] |
| 225 |
if lun_id < 0: |
| 226 |
module.fail_json(msg=f"LUN ID must be 0 or greater. Provided value: {lun_id}") |
| 227 |
|
| 228 |
# Resource path used to configure policy |
| 229 |
resource_path = '/vnic/IscsiStaticTargetPolicies' |
| 230 |
|
| 231 |
# Define API body used in compares or create |
| 232 |
intersight.api_body = { |
| 233 |
'Organization': { |
| 234 |
'Name': intersight.module.params['organization'], |
| 235 |
}, |
| 236 |
'Name': intersight.module.params['name'] |
| 237 |
} |
| 238 |
|
| 239 |
# Add fields for present state |
| 240 |
if intersight.module.params['state'] == 'present': |
| 241 |
intersight.api_body['TargetName'] = intersight.module.params['target_name'] |
| 242 |
intersight.api_body['Port'] = intersight.module.params['port'] |
| 243 |
intersight.api_body['Lun'] = { |
| 244 |
'LunId': intersight.module.params['lun_id'] |
| 245 |
} |
| 246 |
intersight.api_body['IscsiIpType'] = intersight.module.params['ip_protocol'] |
| 247 |
|
| 248 |
# Add IP address based on protocol type |
| 249 |
if intersight.module.params['ip_protocol'] == 'IPv4': |
| 250 |
intersight.api_body['IpAddress'] = intersight.module.params['ipv4_address'] |
| 251 |
elif intersight.module.params['ip_protocol'] == 'IPv6': |
| 252 |
intersight.api_body['IpAddress'] = intersight.module.params['ipv6_address'] |
| 253 |
|
| 254 |
intersight.set_tags_and_description() |
| 255 |
|
| 256 |
# Configure the policy |
| 257 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 258 |
|
| 259 |
module.exit_json(**intersight.result) |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == '__main__': |
| 263 |
main() |