| 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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['preview'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: ucs_ip_pool |
| 17 |
short_description: Configures IP address pools on Cisco UCS Manager |
| 18 |
description: |
| 19 |
- Configures IP address pools and blocks of IP addresses on Cisco UCS Manager. |
| 20 |
extends_documentation_fragment: cisco.ucs.ucs |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- If C(present), will verify IP pool is present and will create if needed. |
| 25 |
- If C(absent), will verify IP pool is absent and will delete if needed. |
| 26 |
choices: [present, absent] |
| 27 |
default: present |
| 28 |
type: str |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The name of the IP address pool. |
| 32 |
- This name can be between 1 and 32 alphanumeric characters. |
| 33 |
- "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." |
| 34 |
- You cannot change this name after the IP address pool is created. |
| 35 |
required: yes |
| 36 |
type: str |
| 37 |
description: |
| 38 |
description: |
| 39 |
- The user-defined description of the IP address pool. |
| 40 |
- Enter up to 256 characters. |
| 41 |
- "You can use any characters or spaces except the following:" |
| 42 |
- "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)." |
| 43 |
aliases: [ descr ] |
| 44 |
type: str |
| 45 |
order: |
| 46 |
description: |
| 47 |
- The Assignment Order field. |
| 48 |
- "This can be one of the following:" |
| 49 |
- "default - Cisco UCS Manager selects a random identity from the pool." |
| 50 |
- "sequential - Cisco UCS Manager selects the lowest available identity from the pool." |
| 51 |
choices: [default, sequential] |
| 52 |
default: default |
| 53 |
type: str |
| 54 |
ipv4_blocks: |
| 55 |
description: |
| 56 |
- List of IPv4 blocks used by the IP Pool. |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
suboptions: |
| 60 |
first_addr: |
| 61 |
description: |
| 62 |
- The first IPv4 address in the IPv4 addresses block. |
| 63 |
- This is the From field in the UCS Manager Add IPv4 Blocks menu. |
| 64 |
type: str |
| 65 |
last_addr: |
| 66 |
description: |
| 67 |
- The last IPv4 address in the IPv4 addresses block. |
| 68 |
- This is the To field in the UCS Manager Add IPv4 Blocks menu. |
| 69 |
type: str |
| 70 |
subnet_mask: |
| 71 |
description: |
| 72 |
- The subnet mask associated with the IPv4 addresses in the block. |
| 73 |
default: 255.255.255.0 |
| 74 |
type: str |
| 75 |
default_gw: |
| 76 |
description: |
| 77 |
- The default gateway associated with the IPv4 addresses in the block. |
| 78 |
default: 0.0.0.0 |
| 79 |
type: str |
| 80 |
primary_dns: |
| 81 |
description: |
| 82 |
- The primary DNS server that this block of IPv4 addresses should access. |
| 83 |
default: 0.0.0.0 |
| 84 |
type: str |
| 85 |
secondary_dns: |
| 86 |
description: |
| 87 |
- The secondary DNS server that this block of IPv4 addresses should access. |
| 88 |
default: 0.0.0.0 |
| 89 |
type: str |
| 90 |
state: |
| 91 |
description: |
| 92 |
- If C(present), will verify IP block is present and will create if needed. |
| 93 |
- If C(absent), will verify IP block is absent and will delete if needed. |
| 94 |
choices: [present, absent] |
| 95 |
default: present |
| 96 |
type: str |
| 97 |
ipv6_blocks: |
| 98 |
description: |
| 99 |
- List of IPv6 blocks used by the IP Pool. |
| 100 |
type: list |
| 101 |
elements: dict |
| 102 |
suboptions: |
| 103 |
ipv6_first_addr: |
| 104 |
description: |
| 105 |
- The first IPv6 address in the IPv6 addresses block. |
| 106 |
- This is the From field in the UCS Manager Add IPv6 Blocks menu. |
| 107 |
type: str |
| 108 |
ipv6_last_addr: |
| 109 |
description: |
| 110 |
- The last IPv6 address in the IPv6 addresses block. |
| 111 |
- This is the To field in the UCS Manager Add IPv6 Blocks menu. |
| 112 |
type: str |
| 113 |
ipv6_prefix: |
| 114 |
description: |
| 115 |
- The network address prefix associated with the IPv6 addresses in the block. |
| 116 |
default: '64' |
| 117 |
type: str |
| 118 |
ipv6_default_gw: |
| 119 |
description: |
| 120 |
- The default gateway associated with the IPv6 addresses in the block. |
| 121 |
default: '::' |
| 122 |
type: str |
| 123 |
ipv6_primary_dns: |
| 124 |
description: |
| 125 |
- The primary DNS server that this block of IPv6 addresses should access. |
| 126 |
default: '::' |
| 127 |
type: str |
| 128 |
ipv6_secondary_dns: |
| 129 |
description: |
| 130 |
- The secondary DNS server that this block of IPv6 addresses should access. |
| 131 |
default: '::' |
| 132 |
type: str |
| 133 |
state: |
| 134 |
description: |
| 135 |
- If C(present), will verify IP block is present and will create if needed. |
| 136 |
- If C(absent), will verify IP block is absent and will delete if needed. |
| 137 |
choices: [present, absent] |
| 138 |
default: present |
| 139 |
type: str |
| 140 |
org_dn: |
| 141 |
description: |
| 142 |
- Org dn (distinguished name) |
| 143 |
default: org-root |
| 144 |
type: str |
| 145 |
requirements: |
| 146 |
- ucsmsdk |
| 147 |
author: |
| 148 |
- Brett Johnson (@sdbrett) |
| 149 |
- David Soper (@dsoper2) |
| 150 |
- John McDonough (@movinalot) |
| 151 |
- CiscoUcs (@CiscoUcs) |
| 152 |
''' |
| 153 |
|
| 154 |
EXAMPLES = r''' |
| 155 |
- name: Configure IPv4 and IPv6 address pool |
| 156 |
cisco.ucs.ucs_ip_pool: |
| 157 |
hostname: "{{ ucs_hostname }}" |
| 158 |
username: "{{ ucs_username }}" |
| 159 |
password: "{{ ucs_password }}" |
| 160 |
name: ip-pool-01 |
| 161 |
org_dn: org-root/org-level1 |
| 162 |
ipv4_blocks: |
| 163 |
- first_addr: 192.168.10.1 |
| 164 |
last_addr: 192.168.10.20 |
| 165 |
subnet_mask: 255.255.255.128 |
| 166 |
default_gw: 192.168.10.2 |
| 167 |
- first_addr: 192.168.11.1 |
| 168 |
last_addr: 192.168.11.20 |
| 169 |
subnet_mask: 255.255.255.128 |
| 170 |
default_gw: 192.168.11.2 |
| 171 |
ipv6_blocks: |
| 172 |
- ipv6_first_addr: fe80::1cae:7992:d7a1:ed07 |
| 173 |
ipv6_last_addr: fe80::1cae:7992:d7a1:edfe |
| 174 |
ipv6_default_gw: fe80::1cae:7992:d7a1:ecff |
| 175 |
- ipv6_first_addr: fe80::1cae:7992:d7a1:ec07 |
| 176 |
ipv6_last_addr: fe80::1cae:7992:d7a1:ecfe |
| 177 |
ipv6_default_gw: fe80::1cae:7992:d7a1:ecff |
| 178 |
|
| 179 |
- name: Delete IPv4 and IPv6 address pool blocks |
| 180 |
cisco.ucs.ucs_ip_pool: |
| 181 |
hostname: "{{ ucs_hostname }}" |
| 182 |
username: "{{ ucs_username }}" |
| 183 |
password: "{{ ucs_password }}" |
| 184 |
name: ip-pool-01 |
| 185 |
org_dn: org-root/org-level1 |
| 186 |
ipv4_blocks: |
| 187 |
- first_addr: 192.168.10.1 |
| 188 |
last_addr: 192.168.10.20 |
| 189 |
state: absent |
| 190 |
ipv6_blocks: |
| 191 |
- ipv6_first_addr: fe80::1cae:7992:d7a1:ec07 |
| 192 |
ipv6_last_addr: fe80::1cae:7992:d7a1:ecfe |
| 193 |
state: absent |
| 194 |
|
| 195 |
- name: Remove IPv4 and IPv6 address pool |
| 196 |
cisco.ucs.ucs_ip_pool: |
| 197 |
hostname: "{{ ucs_hostname }}" |
| 198 |
username: "{{ ucs_username }}" |
| 199 |
password: "{{ ucs_password }}" |
| 200 |
name: ip-pool-01 |
| 201 |
state: absent |
| 202 |
''' |
| 203 |
|
| 204 |
RETURN = r''' |
| 205 |
# |
| 206 |
''' |
| 207 |
|
| 208 |
|
| 209 |
def update_ip_pool(ucs, module): |
| 210 |
from ucsmsdk.mometa.ippool.IppoolPool import IppoolPool |
| 211 |
|
| 212 |
mo = IppoolPool( |
| 213 |
parent_mo_or_dn=module.params['org_dn'], |
| 214 |
name=module.params['name'], |
| 215 |
descr=module.params['descr'], |
| 216 |
assignment_order=module.params['order'], |
| 217 |
) |
| 218 |
ucs.login_handle.add_mo(mo, True) |
| 219 |
ucs.login_handle.commit() |
| 220 |
|
| 221 |
return mo |
| 222 |
|
| 223 |
|
| 224 |
def match_existing_ipv4_block(ucs, dn, ipv4_block): |
| 225 |
# ipv4 block specified, check properties |
| 226 |
mo_1 = get_ip_block(ucs, dn, ipv4_block['first_addr'], ipv4_block['last_addr'], 'v4') |
| 227 |
if not mo_1: |
| 228 |
if ipv4_block['state'] == 'absent': |
| 229 |
return True |
| 230 |
return False |
| 231 |
else: |
| 232 |
if ipv4_block['state'] == 'absent': |
| 233 |
return False |
| 234 |
kwargs = dict(subnet=ipv4_block['subnet_mask']) |
| 235 |
kwargs['def_gw'] = ipv4_block['default_gw'] |
| 236 |
kwargs['prim_dns'] = ipv4_block['primary_dns'] |
| 237 |
kwargs['sec_dns'] = ipv4_block['secondary_dns'] |
| 238 |
return mo_1.check_prop_match(**kwargs) |
| 239 |
|
| 240 |
|
| 241 |
def match_existing_ipv6_block(ucs, dn, ipv6_block): |
| 242 |
# ipv6 block specified, check properties |
| 243 |
mo_1 = get_ip_block(ucs, dn, ipv6_block['ipv6_first_addr'], ipv6_block['ipv6_last_addr'], 'v6') |
| 244 |
if not mo_1: |
| 245 |
if ipv6_block['state'] == 'absent': |
| 246 |
return True |
| 247 |
return False |
| 248 |
else: |
| 249 |
if ipv6_block['state'] == 'absent': |
| 250 |
return False |
| 251 |
kwargs = dict(prefix=ipv6_block['ipv6_prefix']) |
| 252 |
kwargs['def_gw'] = ipv6_block['ipv6_default_gw'] |
| 253 |
kwargs['prim_dns'] = ipv6_block['ipv6_primary_dns'] |
| 254 |
kwargs['sec_dns'] = ipv6_block['ipv6_secondary_dns'] |
| 255 |
return mo_1.check_prop_match(**kwargs) |
| 256 |
|
| 257 |
|
| 258 |
def remove_ip_block(ucs, dn, ip_block, ip_version): |
| 259 |
if ip_version == 'v6': |
| 260 |
first_addr = ip_block['ipv6_first_addr'] |
| 261 |
last_addr = ip_block['ipv6_last_addr'] |
| 262 |
else: |
| 263 |
first_addr = ip_block['first_addr'] |
| 264 |
last_addr = ip_block['last_addr'] |
| 265 |
|
| 266 |
mo_1 = get_ip_block(ucs, dn, first_addr, last_addr, ip_version) |
| 267 |
if mo_1: |
| 268 |
ucs.login_handle.remove_mo(mo_1) |
| 269 |
ucs.login_handle.commit() |
| 270 |
|
| 271 |
|
| 272 |
def update_ip_block(ucs, mo, ip_block, ip_version): |
| 273 |
|
| 274 |
remove_ip_block(ucs, mo.dn, ip_block, ip_version) |
| 275 |
if not ip_block['state'] == 'absent': |
| 276 |
if ip_version == 'v6': |
| 277 |
from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block |
| 278 |
IppoolIpV6Block( |
| 279 |
parent_mo_or_dn=mo, |
| 280 |
to=ip_block['ipv6_last_addr'], |
| 281 |
r_from=ip_block['ipv6_first_addr'], |
| 282 |
prefix=ip_block['ipv6_prefix'], |
| 283 |
def_gw=ip_block['ipv6_default_gw'], |
| 284 |
prim_dns=ip_block['ipv6_primary_dns'], |
| 285 |
sec_dns=ip_block['ipv6_secondary_dns'] |
| 286 |
) |
| 287 |
ucs.login_handle.add_mo(mo, True) |
| 288 |
ucs.login_handle.commit() |
| 289 |
else: |
| 290 |
from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock |
| 291 |
IppoolBlock( |
| 292 |
parent_mo_or_dn=mo, |
| 293 |
to=ip_block['last_addr'], |
| 294 |
r_from=ip_block['first_addr'], |
| 295 |
subnet=ip_block['subnet_mask'], |
| 296 |
def_gw=ip_block['default_gw'], |
| 297 |
prim_dns=ip_block['primary_dns'], |
| 298 |
sec_dns=ip_block['secondary_dns'] |
| 299 |
) |
| 300 |
ucs.login_handle.add_mo(mo, True) |
| 301 |
ucs.login_handle.commit() |
| 302 |
|
| 303 |
|
| 304 |
def get_ip_block(ucs, pool_dn, first_addr, last_addr, ip_version): |
| 305 |
if ip_version == 'v6': |
| 306 |
dn_type = '/v6block-' |
| 307 |
else: |
| 308 |
dn_type = '/block-' |
| 309 |
|
| 310 |
block_dn = pool_dn + dn_type + first_addr + '-' + last_addr |
| 311 |
return ucs.login_handle.query_dn(block_dn) |
| 312 |
|
| 313 |
|
| 314 |
def main(): |
| 315 |
from ansible.module_utils.basic import AnsibleModule |
| 316 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 317 |
|
| 318 |
ipv4_configuration_spec = dict( |
| 319 |
first_addr=dict(type='str'), |
| 320 |
last_addr=dict(type='str'), |
| 321 |
subnet_mask=dict(type='str', default='255.255.255.0'), |
| 322 |
default_gw=dict(type='str', default='0.0.0.0'), |
| 323 |
primary_dns=dict(type='str', default='0.0.0.0'), |
| 324 |
secondary_dns=dict(type='str', default='0.0.0.0'), |
| 325 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 326 |
) |
| 327 |
ipv6_configuration_spec = dict( |
| 328 |
ipv6_first_addr=dict(type='str'), |
| 329 |
ipv6_last_addr=dict(type='str'), |
| 330 |
ipv6_prefix=dict(type='str', default='64'), |
| 331 |
ipv6_default_gw=dict(type='str', default='::'), |
| 332 |
ipv6_primary_dns=dict(type='str', default='::'), |
| 333 |
ipv6_secondary_dns=dict(type='str', default='::'), |
| 334 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 335 |
) |
| 336 |
|
| 337 |
argument_spec = ucs_argument_spec.copy() |
| 338 |
argument_spec.update( |
| 339 |
org_dn=dict(type='str', default='org-root'), |
| 340 |
name=dict(type='str', required=True), |
| 341 |
descr=dict(type='str', aliases=['description']), |
| 342 |
order=dict(type='str', default='default', choices=['default', 'sequential']), |
| 343 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 344 |
ipv4_blocks=dict(type='list', default=None, elements='dict', options=ipv4_configuration_spec), |
| 345 |
ipv6_blocks=dict(type='list', default=None, elements='dict', options=ipv6_configuration_spec), |
| 346 |
) |
| 347 |
|
| 348 |
module = AnsibleModule( |
| 349 |
argument_spec, |
| 350 |
supports_check_mode=True, |
| 351 |
) |
| 352 |
# UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. |
| 353 |
ucs = UCSModule(module) |
| 354 |
|
| 355 |
err = False |
| 356 |
|
| 357 |
from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock |
| 358 |
from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block |
| 359 |
|
| 360 |
changed = False |
| 361 |
try: |
| 362 |
mo_exists = False |
| 363 |
ipv4_props_match = True |
| 364 |
ipv6_props_match = True |
| 365 |
# dn is <org_dn>/ip-pool-<name> |
| 366 |
dn = module.params['org_dn'] + '/ip-pool-' + module.params['name'] |
| 367 |
|
| 368 |
mo = ucs.login_handle.query_dn(dn) |
| 369 |
if mo: |
| 370 |
mo_exists = True |
| 371 |
if module.params['state'] == 'absent': |
| 372 |
if mo_exists: |
| 373 |
if not module.check_mode: |
| 374 |
ucs.login_handle.remove_mo(mo) |
| 375 |
ucs.login_handle.commit() |
| 376 |
changed = True |
| 377 |
else: |
| 378 |
if not mo_exists: |
| 379 |
if not module.check_mode: |
| 380 |
mo = update_ip_pool(ucs, module) |
| 381 |
changed = True |
| 382 |
if mo_exists: |
| 383 |
# check top-level mo props |
| 384 |
kwargs = dict(assignment_order=module.params['order']) |
| 385 |
kwargs['descr'] = module.params['descr'] |
| 386 |
if not mo.check_prop_match(**kwargs): |
| 387 |
if not module.check_mode: |
| 388 |
mo = update_ip_pool(ucs, module) |
| 389 |
changed = True |
| 390 |
# top-level props match, check next level mo/props |
| 391 |
if module.params['ipv4_blocks']: |
| 392 |
for ipv4_block in module.params['ipv4_blocks']: |
| 393 |
if not match_existing_ipv4_block(ucs, dn, ipv4_block): |
| 394 |
if not module.check_mode: |
| 395 |
update_ip_block(ucs, mo, ipv4_block, 'v4') |
| 396 |
changed = True |
| 397 |
|
| 398 |
# only check ipv6 props if the top-level and ipv4 props matched |
| 399 |
if module.params['ipv6_blocks']: |
| 400 |
for ipv6_block in module.params['ipv6_blocks']: |
| 401 |
if not match_existing_ipv6_block(ucs, dn, ipv6_block): |
| 402 |
if not module.check_mode: |
| 403 |
update_ip_block(ucs, mo, ipv6_block, 'v6') |
| 404 |
changed = True |
| 405 |
|
| 406 |
if not module.check_mode: |
| 407 |
ucs.login_handle.add_mo(mo, True) |
| 408 |
ucs.login_handle.commit() |
| 409 |
|
| 410 |
except Exception as e: |
| 411 |
err = True |
| 412 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 413 |
|
| 414 |
ucs.result['changed'] = changed |
| 415 |
if err: |
| 416 |
module.fail_json(**ucs.result) |
| 417 |
module.exit_json(**ucs.result) |
| 418 |
|
| 419 |
|
| 420 |
if __name__ == '__main__': |
| 421 |
main() |