| 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_vnic_template_info |
| 16 |
short_description: Gather information about vNIC Templates in Cisco Intersight |
| 17 |
description: |
| 18 |
- Retrieve comprehensive information about vNIC Templates from L(Cisco Intersight,https://intersight.com). |
| 19 |
- Query templates by organization, template name, or other filters. |
| 20 |
- Returns structured data with template metadata and policy associations. |
| 21 |
- If no filters are provided, all vNIC Templates will be returned. |
| 22 |
- vNIC Templates are used to define standardized network interface configurations for FI-Attached deployments. |
| 23 |
extends_documentation_fragment: intersight |
| 24 |
options: |
| 25 |
organization: |
| 26 |
description: |
| 27 |
- The name of the organization to filter vNIC Templates by. |
| 28 |
- Use 'default' for the default organization. |
| 29 |
- When specified, only templates from this organization will be returned. |
| 30 |
type: str |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The exact name of the vNIC Template to retrieve information from. |
| 34 |
- When specified, only the matching template will be returned. |
| 35 |
type: str |
| 36 |
author: |
| 37 |
- Ron Gershburg (@rgershbu) |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = r''' |
| 41 |
# Basic Usage Examples |
| 42 |
- name: Fetch all vNIC Templates from all organizations |
| 43 |
cisco.intersight.intersight_vnic_template_info: |
| 44 |
api_private_key: "{{ api_private_key }}" |
| 45 |
api_key_id: "{{ api_key_id }}" |
| 46 |
register: all_vnic_templates |
| 47 |
|
| 48 |
- name: Display all template names |
| 49 |
debug: |
| 50 |
msg: "Template: {{ item.Name }}" |
| 51 |
loop: "{{ all_vnic_templates.api_response }}" |
| 52 |
when: all_vnic_templates.api_response is iterable |
| 53 |
|
| 54 |
# Organization-specific Examples |
| 55 |
- name: Fetch all vNIC Templates from the default organization |
| 56 |
cisco.intersight.intersight_vnic_template_info: |
| 57 |
api_private_key: "{{ api_private_key }}" |
| 58 |
api_key_id: "{{ api_key_id }}" |
| 59 |
organization: "default" |
| 60 |
register: default_org_templates |
| 61 |
|
| 62 |
- name: Fetch all vNIC Templates from a custom organization |
| 63 |
cisco.intersight.intersight_vnic_template_info: |
| 64 |
api_private_key: "{{ api_private_key }}" |
| 65 |
api_key_id: "{{ api_key_id }}" |
| 66 |
organization: "Engineering" |
| 67 |
register: engineering_templates |
| 68 |
''' |
| 69 |
|
| 70 |
RETURN = r''' |
| 71 |
api_response: |
| 72 |
description: |
| 73 |
- The API response containing vNIC Template information. |
| 74 |
- Returns a dictionary when querying a single template or no templates found. |
| 75 |
- Returns a list when multiple templates are found. |
| 76 |
returned: always |
| 77 |
type: dict |
| 78 |
sample: |
| 79 |
Name: "production-vnic-template" |
| 80 |
ObjectType: "vnic.VnicTemplate" |
| 81 |
Moid: "12345678901234567890abcd" |
| 82 |
Description: "Production vNIC template for FI-attached servers" |
| 83 |
EnableOverride: false |
| 84 |
SwitchId: "A" |
| 85 |
FailoverEnabled: false |
| 86 |
Cdn: |
| 87 |
Source: "vnic" |
| 88 |
MacPool: |
| 89 |
Name: "default-mac-pool" |
| 90 |
ObjectType: "macpool.Pool" |
| 91 |
Moid: "macpool12345678901234567890" |
| 92 |
FabricEthNetworkGroupPolicy: |
| 93 |
- Name: "default-network-group" |
| 94 |
ObjectType: "fabric.EthNetworkGroupPolicy" |
| 95 |
Moid: "netgroup12345678901234567890" |
| 96 |
FabricEthNetworkControlPolicy: |
| 97 |
Name: "default-network-control" |
| 98 |
ObjectType: "fabric.EthNetworkControlPolicy" |
| 99 |
Moid: "netcontrol12345678901234567890" |
| 100 |
EthQosPolicy: |
| 101 |
Name: "default-qos-policy" |
| 102 |
ObjectType: "vnic.EthQosPolicy" |
| 103 |
Moid: "qos12345678901234567890" |
| 104 |
EthAdapterPolicy: |
| 105 |
Name: "default-adapter-policy" |
| 106 |
ObjectType: "vnic.EthAdapterPolicy" |
| 107 |
Moid: "adapter12345678901234567890" |
| 108 |
Organization: |
| 109 |
Name: "default" |
| 110 |
ObjectType: "organization.Organization" |
| 111 |
Moid: "org12345678901234567890" |
| 112 |
Tags: |
| 113 |
- Key: "Environment" |
| 114 |
Value: "Production" |
| 115 |
- Key: "Site" |
| 116 |
Value: "DataCenter-A" |
| 117 |
SriovSettings: |
| 118 |
Enabled: true |
| 119 |
VfCount: 64 |
| 120 |
RxCountPerVf: 4 |
| 121 |
TxCountPerVf: 1 |
| 122 |
CompCountPerVf: 5 |
| 123 |
IntCountPerVf: 8 |
| 124 |
''' |
| 125 |
|
| 126 |
|
| 127 |
from ansible.module_utils.basic import AnsibleModule |
| 128 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 129 |
|
| 130 |
|
| 131 |
def main(): |
| 132 |
argument_spec = intersight_argument_spec.copy() |
| 133 |
argument_spec.update( |
| 134 |
organization=dict(type='str'), |
| 135 |
name=dict(type='str') |
| 136 |
) |
| 137 |
module = AnsibleModule( |
| 138 |
argument_spec, |
| 139 |
supports_check_mode=True, |
| 140 |
) |
| 141 |
|
| 142 |
intersight = IntersightModule(module) |
| 143 |
|
| 144 |
# Resource path used to fetch vNIC Template info |
| 145 |
resource_path = '/vnic/VnicTemplates' |
| 146 |
|
| 147 |
# Get query parameters for templates |
| 148 |
query_params = intersight.set_query_params() |
| 149 |
|
| 150 |
# Reset api_response before the API call to avoid previous responses |
| 151 |
intersight.result['api_response'] = {} |
| 152 |
|
| 153 |
# Get vNIC Templates |
| 154 |
intersight.get_resource( |
| 155 |
resource_path=resource_path, |
| 156 |
query_params=query_params, |
| 157 |
return_list=True |
| 158 |
) |
| 159 |
|
| 160 |
templates = intersight.result.get('api_response', []) |
| 161 |
|
| 162 |
# Create final response structure |
| 163 |
final_api_response = None |
| 164 |
|
| 165 |
# Ensure templates is always a list for checking length, even if a single dict is returned |
| 166 |
if isinstance(templates, dict): |
| 167 |
templates = [templates] |
| 168 |
elif not isinstance(templates, list): |
| 169 |
templates = [] |
| 170 |
|
| 171 |
# Set final response based on number of templates found |
| 172 |
if len(templates) == 1: |
| 173 |
# Single template - return as dict |
| 174 |
final_api_response = templates[0] |
| 175 |
elif len(templates) > 1: |
| 176 |
# Multiple templates - return as list |
| 177 |
final_api_response = templates |
| 178 |
else: |
| 179 |
# No templates found - return empty dict |
| 180 |
final_api_response = {} |
| 181 |
|
| 182 |
# Use intersight.result and update api_response directly |
| 183 |
intersight.result['api_response'] = final_api_response |
| 184 |
final_result = intersight.result |
| 185 |
|
| 186 |
module.exit_json(**final_result) |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == '__main__': |
| 190 |
main() |