| 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_link_control_policy |
| 16 |
short_description: Link Control Policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Manages Link Control Policy configuration on Cisco Intersight. |
| 19 |
- A policy to configure UDLD (UniDirectional Link Detection) settings on Cisco Intersight managed devices. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/fabric/LinkControlPolicy/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 |
- Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 34 |
type: str |
| 35 |
default: default |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name assigned to the Link Control Policy. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
description: |
| 43 |
description: |
| 44 |
- The user-defined description for the Link Control Policy. |
| 45 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 46 |
type: str |
| 47 |
aliases: [descr] |
| 48 |
tags: |
| 49 |
description: |
| 50 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 51 |
type: list |
| 52 |
elements: dict |
| 53 |
udld_admin_state: |
| 54 |
description: |
| 55 |
- UDLD Admin configured Link State for this port. |
| 56 |
type: str |
| 57 |
choices: ['Enabled', 'Disabled'] |
| 58 |
default: 'Enabled' |
| 59 |
udld_mode: |
| 60 |
description: |
| 61 |
- UDLD Admin configured Mode for this port. |
| 62 |
- Cannot be set to 'aggressive' when udld_admin_state is 'Disabled'. |
| 63 |
type: str |
| 64 |
choices: ['normal', 'aggressive'] |
| 65 |
default: 'normal' |
| 66 |
author: |
| 67 |
- Ron Gershburg (@rgershbu) |
| 68 |
''' |
| 69 |
|
| 70 |
EXAMPLES = r''' |
| 71 |
- name: Configure Link Control Policy with UDLD enabled in aggressive mode |
| 72 |
cisco.intersight.intersight_link_control_policy: |
| 73 |
api_private_key: "{{ api_private_key }}" |
| 74 |
api_key_id: "{{ api_key_id }}" |
| 75 |
name: "test-link-aggressive" |
| 76 |
description: "Link Control Policy with aggressive UDLD" |
| 77 |
udld_admin_state: "Enabled" |
| 78 |
udld_mode: "aggressive" |
| 79 |
tags: |
| 80 |
- Key: Site |
| 81 |
Value: DataCenter-A |
| 82 |
|
| 83 |
- name: Configure Link Control Policy with UDLD disabled |
| 84 |
cisco.intersight.intersight_link_control_policy: |
| 85 |
api_private_key: "{{ api_private_key }}" |
| 86 |
api_key_id: "{{ api_key_id }}" |
| 87 |
name: "test-link-disabled" |
| 88 |
description: "Link Control Policy with UDLD disabled" |
| 89 |
udld_admin_state: "Disabled" |
| 90 |
udld_mode: "normal" |
| 91 |
|
| 92 |
- name: Delete Link Control Policy |
| 93 |
cisco.intersight.intersight_link_control_policy: |
| 94 |
api_private_key: "{{ api_private_key }}" |
| 95 |
api_key_id: "{{ api_key_id }}" |
| 96 |
name: "test-link-policy" |
| 97 |
state: absent |
| 98 |
''' |
| 99 |
|
| 100 |
RETURN = r''' |
| 101 |
api_response: |
| 102 |
description: The API response output returned by the specified resource. |
| 103 |
returned: always |
| 104 |
type: dict |
| 105 |
sample: |
| 106 |
"api_response": { |
| 107 |
"Name": "test-link-policy", |
| 108 |
"ObjectType": "fabric.LinkControlPolicy", |
| 109 |
"UdldSettings": { |
| 110 |
"AdminState": "Enabled", |
| 111 |
"Mode": "aggressive", |
| 112 |
"ClassId": "fabric.UdldSettings", |
| 113 |
"ObjectType": "fabric.UdldSettings" |
| 114 |
}, |
| 115 |
"Tags": [ |
| 116 |
{ |
| 117 |
"Key": "Site", |
| 118 |
"Value": "DataCenter-A" |
| 119 |
} |
| 120 |
] |
| 121 |
} |
| 122 |
''' |
| 123 |
|
| 124 |
|
| 125 |
from ansible.module_utils.basic import AnsibleModule |
| 126 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 127 |
|
| 128 |
|
| 129 |
def validate_udld_configuration(module): |
| 130 |
""" |
| 131 |
Validate UDLD configuration to ensure aggressive mode is not used when admin state is disabled. |
| 132 |
""" |
| 133 |
udld_admin_state = module.params['udld_admin_state'] |
| 134 |
udld_mode = module.params['udld_mode'] |
| 135 |
|
| 136 |
if udld_admin_state == 'Disabled' and udld_mode == 'aggressive': |
| 137 |
module.fail_json( |
| 138 |
msg="Cannot configure the link control policy. Mode should not be 'aggressive' when the policy is disabled." |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
argument_spec = intersight_argument_spec.copy() |
| 144 |
argument_spec.update( |
| 145 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 146 |
organization=dict(type='str', default='default'), |
| 147 |
name=dict(type='str', required=True), |
| 148 |
description=dict(type='str', aliases=['descr']), |
| 149 |
tags=dict(type='list', elements='dict'), |
| 150 |
udld_admin_state=dict(type='str', choices=['Enabled', 'Disabled'], default='Enabled'), |
| 151 |
udld_mode=dict(type='str', choices=['normal', 'aggressive'], default='normal') |
| 152 |
) |
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec, |
| 155 |
supports_check_mode=True, |
| 156 |
) |
| 157 |
|
| 158 |
# Validate UDLD configuration |
| 159 |
validate_udld_configuration(module) |
| 160 |
|
| 161 |
intersight = IntersightModule(module) |
| 162 |
intersight.result['api_response'] = {} |
| 163 |
intersight.result['trace_id'] = '' |
| 164 |
|
| 165 |
# Resource path used to configure policy |
| 166 |
resource_path = '/fabric/LinkControlPolicies' |
| 167 |
# Define API body used in compares or create |
| 168 |
intersight.api_body = { |
| 169 |
'Organization': { |
| 170 |
'Name': intersight.module.params['organization'], |
| 171 |
}, |
| 172 |
'Name': intersight.module.params['name'] |
| 173 |
} |
| 174 |
|
| 175 |
if intersight.module.params['state'] == 'present': |
| 176 |
intersight.set_tags_and_description() |
| 177 |
intersight.api_body['UdldSettings'] = { |
| 178 |
'AdminState': intersight.module.params['udld_admin_state'], |
| 179 |
'Mode': intersight.module.params['udld_mode'] |
| 180 |
} |
| 181 |
|
| 182 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 183 |
|
| 184 |
module.exit_json(**intersight.result) |
| 185 |
|
| 186 |
|
| 187 |
if __name__ == '__main__': |
| 188 |
main() |