| 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_info |
| 16 |
short_description: Gather information about iSCSI Static Target Policies in Cisco Intersight |
| 17 |
description: |
| 18 |
- Retrieve information about iSCSI Static Target Policies from L(Cisco Intersight,https://intersight.com). |
| 19 |
- Query policies by organization or policy name. |
| 20 |
- Returns structured data with policy metadata and iSCSI target configuration details. |
| 21 |
- If no filters are provided, all iSCSI Static Target Policies will be returned. |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
organization: |
| 25 |
description: |
| 26 |
- The name of the organization to filter iSCSI Static Target Policies by. |
| 27 |
- Use 'default' for the default organization. |
| 28 |
- When specified, only policies from this organization will be returned. |
| 29 |
type: str |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The exact name of the iSCSI Static Target Policy to retrieve information from. |
| 33 |
- When specified, only the matching policy will be returned. |
| 34 |
type: str |
| 35 |
author: |
| 36 |
- Ron Gershburg (@rgershbu) |
| 37 |
''' |
| 38 |
|
| 39 |
EXAMPLES = r''' |
| 40 |
# Basic Usage Examples |
| 41 |
- name: Fetch all iSCSI Static Target Policies from all organizations |
| 42 |
cisco.intersight.intersight_iscsi_static_target_policy_info: |
| 43 |
api_private_key: "{{ api_private_key }}" |
| 44 |
api_key_id: "{{ api_key_id }}" |
| 45 |
register: all_iscsi_policies |
| 46 |
|
| 47 |
# Organization-specific Examples |
| 48 |
- name: Fetch all iSCSI Static Target Policies from the default organization |
| 49 |
cisco.intersight.intersight_iscsi_static_target_policy_info: |
| 50 |
api_private_key: "{{ api_private_key }}" |
| 51 |
api_key_id: "{{ api_key_id }}" |
| 52 |
organization: "default" |
| 53 |
register: default_org_policies |
| 54 |
|
| 55 |
- name: Fetch all iSCSI Static Target Policies from a custom organization |
| 56 |
cisco.intersight.intersight_iscsi_static_target_policy_info: |
| 57 |
api_private_key: "{{ api_private_key }}" |
| 58 |
api_key_id: "{{ api_key_id }}" |
| 59 |
organization: "Engineering" |
| 60 |
register: engineering_policies |
| 61 |
|
| 62 |
# Specific Policy Examples |
| 63 |
- name: Fetch a specific iSCSI Static Target Policy by name |
| 64 |
cisco.intersight.intersight_iscsi_static_target_policy_info: |
| 65 |
api_private_key: "{{ api_private_key }}" |
| 66 |
api_key_id: "{{ api_key_id }}" |
| 67 |
name: "iscsi-static-target-policy-01" |
| 68 |
register: specific_policy |
| 69 |
|
| 70 |
- name: Fetch a specific policy from a specific organization |
| 71 |
cisco.intersight.intersight_iscsi_static_target_policy_info: |
| 72 |
api_private_key: "{{ api_private_key }}" |
| 73 |
api_key_id: "{{ api_key_id }}" |
| 74 |
organization: "Production" |
| 75 |
name: "iscsi-static-target-policy-prod" |
| 76 |
register: specific_org_policy |
| 77 |
''' |
| 78 |
|
| 79 |
RETURN = r''' |
| 80 |
api_response: |
| 81 |
description: |
| 82 |
- The API response containing iSCSI Static Target Policy information. |
| 83 |
- Returns a dictionary when querying a single policy or no policies found. |
| 84 |
- Returns a list when multiple policies are found. |
| 85 |
returned: always |
| 86 |
type: dict |
| 87 |
sample: |
| 88 |
# Single policy response (when name parameter is used or only one policy found) |
| 89 |
Name: "iscsi-static-target-policy-01" |
| 90 |
ObjectType: "vnic.IscsiStaticTargetPolicy" |
| 91 |
Moid: "1234567890abcdef12345678" |
| 92 |
Description: "iSCSI static target policy for production servers" |
| 93 |
TargetName: "iqn.1991-05.com.microsoft:winclient1" |
| 94 |
Port: 3260 |
| 95 |
Lun: |
| 96 |
LunId: 0 |
| 97 |
IscsiIpType: "IPv4" |
| 98 |
IpAddress: "192.168.10.100" |
| 99 |
Organization: |
| 100 |
Name: "default" |
| 101 |
ObjectType: "organization.Organization" |
| 102 |
Moid: "abcdef1234567890abcdef12" |
| 103 |
Tags: |
| 104 |
- Key: "Environment" |
| 105 |
Value: "Production" |
| 106 |
- Key: "Owner" |
| 107 |
Value: "Storage-Team" |
| 108 |
''' |
| 109 |
|
| 110 |
|
| 111 |
from ansible.module_utils.basic import AnsibleModule |
| 112 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 113 |
|
| 114 |
|
| 115 |
def main(): |
| 116 |
argument_spec = intersight_argument_spec.copy() |
| 117 |
argument_spec.update( |
| 118 |
organization=dict(type='str'), |
| 119 |
name=dict(type='str') |
| 120 |
) |
| 121 |
module = AnsibleModule( |
| 122 |
argument_spec, |
| 123 |
supports_check_mode=True, |
| 124 |
) |
| 125 |
|
| 126 |
intersight = IntersightModule(module) |
| 127 |
|
| 128 |
# Resource path used to fetch policy info |
| 129 |
resource_path = '/vnic/IscsiStaticTargetPolicies' |
| 130 |
|
| 131 |
# Get query parameters for policies |
| 132 |
query_params = intersight.set_query_params() |
| 133 |
|
| 134 |
# Reset api_response before the API call to avoid previous responses |
| 135 |
intersight.result['api_response'] = {} |
| 136 |
|
| 137 |
# Get iSCSI Static Target policies |
| 138 |
intersight.get_resource( |
| 139 |
resource_path=resource_path, |
| 140 |
query_params=query_params, |
| 141 |
return_list=True |
| 142 |
) |
| 143 |
|
| 144 |
policies = intersight.result.get('api_response', []) |
| 145 |
|
| 146 |
# Create final response structure |
| 147 |
final_api_response = None |
| 148 |
|
| 149 |
# Ensure policies is always a list for iteration, even if a single dict is returned |
| 150 |
if isinstance(policies, dict): |
| 151 |
policies = [policies] |
| 152 |
elif not isinstance(policies, list): |
| 153 |
policies = [] |
| 154 |
|
| 155 |
# Set final response based on number of policies found |
| 156 |
if len(policies) == 1: |
| 157 |
# Single policy - return as dict |
| 158 |
final_api_response = policies[0] |
| 159 |
elif len(policies) > 1: |
| 160 |
# Multiple policies - return as list |
| 161 |
final_api_response = policies |
| 162 |
else: |
| 163 |
# No policies found - return empty structure |
| 164 |
final_api_response = {} |
| 165 |
|
| 166 |
# Use intersight.result and update api_response directly |
| 167 |
intersight.result['api_response'] = final_api_response |
| 168 |
|
| 169 |
module.exit_json(**intersight.result) |
| 170 |
|
| 171 |
|
| 172 |
if __name__ == '__main__': |
| 173 |
main() |