| 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_mac_pool |
| 16 |
short_description: MAC Pool configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- MAC Pool configuration for Cisco Intersight. |
| 19 |
- Used to configure MAC pools settings on Cisco Intersight managed devices. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 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 MAC Pool. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
tags: |
| 43 |
description: |
| 44 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
description: |
| 48 |
description: |
| 49 |
- The user-defined description of the MAC Pool. |
| 50 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 51 |
type: str |
| 52 |
aliases: [descr] |
| 53 |
assignment_order: |
| 54 |
description: |
| 55 |
- Assignment order decides the order in which the next identifier is allocated. |
| 56 |
- If C(sequential), identifiers are assigned in a sequential order. |
| 57 |
- If C(default), the system determines the assignment order. |
| 58 |
type: str |
| 59 |
choices: [default, sequential] |
| 60 |
default: default |
| 61 |
mac_blocks: |
| 62 |
description: |
| 63 |
- List of the MAC blocks. |
| 64 |
- Should include the address_from and size per block. |
| 65 |
type: list |
| 66 |
elements: dict |
| 67 |
suboptions: |
| 68 |
address_from: |
| 69 |
description: |
| 70 |
- Starting address of the block must be in hexadecimal format xx:xx:xx:xx:xx:xx. |
| 71 |
- To ensure uniqueness of MACs in the LAN fabric, you are strongly encouraged to use the following MAC prefix 00:25:B5:xx:xx:xx. |
| 72 |
type: str |
| 73 |
required: true |
| 74 |
size: |
| 75 |
description: |
| 76 |
- Number of identifiers this block can hold. |
| 77 |
- This value must be an integer between 1 and 1024, inclusive. |
| 78 |
type: int |
| 79 |
required: true |
| 80 |
author: |
| 81 |
- Ron Gershburg (@rgershbu) |
| 82 |
''' |
| 83 |
|
| 84 |
EXAMPLES = r''' |
| 85 |
- name: Create mac pool |
| 86 |
cisco.intersight.intersight_mac_pool: |
| 87 |
api_private_key: "{{ api_private_key }}" |
| 88 |
api_key_id: "{{ api_key_id }}" |
| 89 |
name: mac_pool_1 |
| 90 |
description: "Test mac pool description" |
| 91 |
assignment_order: sequential |
| 92 |
tags: |
| 93 |
- "Key": "Site" |
| 94 |
"Value": "tag1" |
| 95 |
- "Key": "Site2" |
| 96 |
"Value": "tag2" |
| 97 |
mac_blocks: |
| 98 |
- "address_from": "00:25:B5:00:00:00" |
| 99 |
"size": 20 |
| 100 |
- name: Delete mac pool |
| 101 |
cisco.intersight.intersight_mac_pool: |
| 102 |
api_private_key: "{{ api_private_key }}" |
| 103 |
api_key_id: "{{ api_key_id }}" |
| 104 |
name: mac_pool_1 |
| 105 |
state: absent |
| 106 |
''' |
| 107 |
|
| 108 |
RETURN = r''' |
| 109 |
api_response: |
| 110 |
description: The API response output returned by the specified resource. |
| 111 |
returned: always |
| 112 |
type: dict |
| 113 |
sample: |
| 114 |
"api_response": { |
| 115 |
"Name": "mac_pool_1", |
| 116 |
"ObjectType": "macpool.Pool", |
| 117 |
"Tags": [ |
| 118 |
{ |
| 119 |
"Key": "Site", |
| 120 |
"Value": "tag1" |
| 121 |
}, |
| 122 |
{ |
| 123 |
"Key": "Site2", |
| 124 |
"Value": "tag2" |
| 125 |
} |
| 126 |
] |
| 127 |
} |
| 128 |
''' |
| 129 |
|
| 130 |
|
| 131 |
from ansible.module_utils.basic import AnsibleModule |
| 132 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 133 |
|
| 134 |
|
| 135 |
def main(): |
| 136 |
argument_spec = intersight_argument_spec.copy() |
| 137 |
argument_spec.update( |
| 138 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 139 |
organization=dict(type='str', default='default'), |
| 140 |
name=dict(type='str', required=True), |
| 141 |
description=dict(type='str', aliases=['descr']), |
| 142 |
tags=dict(type='list', elements='dict'), |
| 143 |
assignment_order=dict(type='str', choices=['default', 'sequential'], default='default'), |
| 144 |
mac_blocks=dict( |
| 145 |
type='list', |
| 146 |
elements='dict', |
| 147 |
options=dict( |
| 148 |
address_from=dict(type='str', required=True), |
| 149 |
size=dict(type='int', required=True) |
| 150 |
) |
| 151 |
) |
| 152 |
) |
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec, |
| 155 |
supports_check_mode=True, |
| 156 |
) |
| 157 |
|
| 158 |
intersight = IntersightModule(module) |
| 159 |
intersight.result['api_response'] = {} |
| 160 |
intersight.result['trace_id'] = '' |
| 161 |
|
| 162 |
# Resource path used to configure policy |
| 163 |
resource_path = '/macpool/Pools' |
| 164 |
# Define API body used in compares or create |
| 165 |
intersight.api_body = { |
| 166 |
'Organization': { |
| 167 |
'Name': intersight.module.params['organization'], |
| 168 |
}, |
| 169 |
'Name': intersight.module.params['name'], |
| 170 |
} |
| 171 |
mac_blocks_dict = [] |
| 172 |
if module.params['state'] == 'present': |
| 173 |
intersight.set_tags_and_description() |
| 174 |
intersight.api_body['AssignmentOrder'] = intersight.module.params['assignment_order'] |
| 175 |
|
| 176 |
# Validate that mac_blocks was passed. We don't mark it as required in order to support absent. |
| 177 |
if not intersight.module.params['mac_blocks']: |
| 178 |
module.fail_json(msg="mac_blocks parameter must be provided and contain at least one block when state is 'present'.") |
| 179 |
|
| 180 |
for mac_block in intersight.module.params['mac_blocks']: |
| 181 |
mac_blocks_dict.append({ |
| 182 |
"From": mac_block['address_from'], |
| 183 |
"Size": mac_block['size'] |
| 184 |
}) |
| 185 |
intersight.api_body['MacBlocks'] = mac_blocks_dict |
| 186 |
|
| 187 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 188 |
|
| 189 |
module.exit_json(**intersight.result) |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == '__main__': |
| 193 |
main() |