| 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': 'certified'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: ucs_uuid_pool |
| 16 |
short_description: Configures server UUID pools on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configures server UUID pools and UUID blocks on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: cisco.ucs.ucs |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- If C(present), will verify UUID pool is present and will create if needed. |
| 24 |
- If C(absent), will verify UUID pool is absent and will delete if needed. |
| 25 |
choices: [present, absent] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the UUID pool. |
| 31 |
- This name can be between 1 and 32 alphanumeric characters. |
| 32 |
- "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." |
| 33 |
- You cannot change this name after the UUID pool is created. |
| 34 |
required: yes |
| 35 |
type: str |
| 36 |
description: |
| 37 |
description: |
| 38 |
- "The user-defined description of the UUID pool." |
| 39 |
- Enter up to 256 characters. |
| 40 |
- "You can use any characters or spaces except the following:" |
| 41 |
- "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)." |
| 42 |
aliases: [ descr ] |
| 43 |
type: str |
| 44 |
prefix: |
| 45 |
description: |
| 46 |
- UUID prefix used for the range of server UUIDs. |
| 47 |
- "If no value is provided, the system derived prefix will be used (equivalent to selecting 'derived' option in UI)." |
| 48 |
- "If the user provides a value, the user provided prefix will be used (equivalent to selecting 'other' option in UI)." |
| 49 |
- A user provided value should be in the format XXXXXXXX-XXXX-XXXX. |
| 50 |
type: str |
| 51 |
order: |
| 52 |
description: |
| 53 |
- The Assignment Order field. |
| 54 |
- "This can be one of the following:" |
| 55 |
- "default - Cisco UCS Manager selects a random identity from the pool." |
| 56 |
- "sequential - Cisco UCS Manager selects the lowest available identity from the pool." |
| 57 |
choices: [default, sequential] |
| 58 |
default: default |
| 59 |
type: str |
| 60 |
first_uuid: |
| 61 |
description: |
| 62 |
- The first UUID in the block of UUIDs. |
| 63 |
- This is the From field in the UCS Manager UUID Blocks menu. |
| 64 |
type: str |
| 65 |
last_uuid: |
| 66 |
description: |
| 67 |
- The last UUID in the block of UUIDs. |
| 68 |
- This is the To field in the UCS Manager Add UUID Blocks menu. |
| 69 |
type: str |
| 70 |
org_dn: |
| 71 |
description: |
| 72 |
- The distinguished name (dn) of the organization where the resource is assigned. |
| 73 |
default: org-root |
| 74 |
type: str |
| 75 |
requirements: |
| 76 |
- ucsmsdk |
| 77 |
author: |
| 78 |
- David Soper (@dsoper2) |
| 79 |
- CiscoUcs (@CiscoUcs) |
| 80 |
''' |
| 81 |
|
| 82 |
EXAMPLES = r''' |
| 83 |
- name: Configure UUID address pool |
| 84 |
cisco.ucs.ucs_uuid_pool: |
| 85 |
hostname: 172.16.143.150 |
| 86 |
username: admin |
| 87 |
password: password |
| 88 |
name: UUID-Pool |
| 89 |
order: sequential |
| 90 |
first_uuid: 0000-000000000001 |
| 91 |
last_uuid: 0000-000000000078 |
| 92 |
|
| 93 |
- name: Remove UUID address pool |
| 94 |
cisco.ucs.ucs_uuid_pool: |
| 95 |
hostname: 172.16.143.150 |
| 96 |
username: admin |
| 97 |
password: password |
| 98 |
name: UUID-Pool |
| 99 |
state: absent |
| 100 |
''' |
| 101 |
|
| 102 |
RETURN = r''' |
| 103 |
# |
| 104 |
''' |
| 105 |
|
| 106 |
from ansible.module_utils.basic import AnsibleModule |
| 107 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 108 |
|
| 109 |
|
| 110 |
def main(): |
| 111 |
argument_spec = ucs_argument_spec.copy() |
| 112 |
argument_spec.update( |
| 113 |
org_dn=dict(type='str', default='org-root'), |
| 114 |
name=dict(type='str', required=True), |
| 115 |
description=dict(type='str', aliases=['descr']), |
| 116 |
order=dict(type='str', default='default', choices=['default', 'sequential']), |
| 117 |
prefix=dict(type='str'), |
| 118 |
first_uuid=dict(type='str'), |
| 119 |
last_uuid=dict(type='str'), |
| 120 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 121 |
) |
| 122 |
module = AnsibleModule( |
| 123 |
argument_spec, |
| 124 |
supports_check_mode=True, |
| 125 |
) |
| 126 |
# UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. |
| 127 |
ucs = UCSModule(module) |
| 128 |
|
| 129 |
err = False |
| 130 |
|
| 131 |
from ucsmsdk.mometa.uuidpool.UuidpoolPool import UuidpoolPool |
| 132 |
from ucsmsdk.mometa.uuidpool.UuidpoolBlock import UuidpoolBlock |
| 133 |
|
| 134 |
ucs.result['changed'] = False |
| 135 |
try: |
| 136 |
mo_exists = False |
| 137 |
props_match = False |
| 138 |
# dn is <org_dn>/uuid-pool-<name> |
| 139 |
dn = module.params['org_dn'] + '/uuid-pool-' + module.params['name'] |
| 140 |
mo = ucs.login_handle.query_dn(dn) |
| 141 |
if mo: |
| 142 |
mo_exists = True |
| 143 |
|
| 144 |
if module.params['state'] == 'absent': |
| 145 |
if mo_exists: |
| 146 |
if not module.check_mode: |
| 147 |
ucs.login_handle.remove_mo(mo) |
| 148 |
ucs.login_handle.commit() |
| 149 |
ucs.result['changed'] = True |
| 150 |
else: |
| 151 |
if mo_exists: |
| 152 |
# check top-level mo props |
| 153 |
kwargs = dict(assignment_order=module.params['order']) |
| 154 |
kwargs['descr'] = module.params['description'] |
| 155 |
if module.params['prefix']: |
| 156 |
kwargs['prefix'] = module.params['prefix'] |
| 157 |
if mo.check_prop_match(**kwargs): |
| 158 |
# top-level props match, check next level mo/props |
| 159 |
if module.params['last_uuid'] and module.params['first_uuid']: |
| 160 |
# uuid address block specified, check properties |
| 161 |
block_dn = dn + '/block-from-' + module.params['first_uuid'].upper() + '-to-' + module.params['last_uuid'].upper() |
| 162 |
mo_1 = ucs.login_handle.query_dn(block_dn) |
| 163 |
if mo_1: |
| 164 |
props_match = True |
| 165 |
else: |
| 166 |
# no UUID address block specified, but top-level props matched |
| 167 |
props_match = True |
| 168 |
|
| 169 |
if not props_match: |
| 170 |
if not module.check_mode: |
| 171 |
# create if mo does not already exist |
| 172 |
if not module.params['prefix']: |
| 173 |
module.params['prefix'] = 'derived' |
| 174 |
mo = UuidpoolPool( |
| 175 |
parent_mo_or_dn=module.params['org_dn'], |
| 176 |
name=module.params['name'], |
| 177 |
descr=module.params['description'], |
| 178 |
assignment_order=module.params['order'], |
| 179 |
prefix=module.params['prefix'] |
| 180 |
) |
| 181 |
|
| 182 |
if module.params['last_uuid'] and module.params['first_uuid']: |
| 183 |
mo_1 = UuidpoolBlock( |
| 184 |
parent_mo_or_dn=mo, |
| 185 |
to=module.params['last_uuid'], |
| 186 |
r_from=module.params['first_uuid'], |
| 187 |
) |
| 188 |
|
| 189 |
ucs.login_handle.add_mo(mo, True) |
| 190 |
ucs.login_handle.commit() |
| 191 |
ucs.result['changed'] = True |
| 192 |
|
| 193 |
except Exception as e: |
| 194 |
err = True |
| 195 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 196 |
|
| 197 |
if err: |
| 198 |
module.fail_json(**ucs.result) |
| 199 |
module.exit_json(**ucs.result) |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == '__main__': |
| 203 |
main() |