| 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: ucs_managed_objects |
| 16 |
short_description: Configures Managed Objects on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configures Managed Objects on Cisco UCS Manager. |
| 19 |
- The Python SDK module, Python class within the module (UCSM Class), and all properties must be directly specified. |
| 20 |
- More information on the UCSM Python SDK and how to directly configure Managed Objects is available at L(UCSM Python SDK,http://ucsmsdk.readthedocs.io/). |
| 21 |
extends_documentation_fragment: cisco.ucs.ucs |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- If C(present), will verify that the Managed Objects are present and will create if needed. |
| 26 |
- If C(absent), will verify that the Managed Objects are absent and will delete if needed. |
| 27 |
choices: [ absent, present ] |
| 28 |
default: present |
| 29 |
type: str |
| 30 |
objects: |
| 31 |
description: |
| 32 |
- List of managed objects to configure. Each managed object has suboptions the specify the Python SDK module, class, and properties to configure. |
| 33 |
type: list |
| 34 |
elements: dict |
| 35 |
suboptions: |
| 36 |
module: |
| 37 |
description: |
| 38 |
- Name of the Python SDK module implementing the required class. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
class_name: |
| 42 |
description: |
| 43 |
- Name of the Python class that will be used to configure the Managed Object. |
| 44 |
required: true |
| 45 |
type: str |
| 46 |
aliases: [class] |
| 47 |
properties: |
| 48 |
description: |
| 49 |
- List of properties to configure on the Managed Object. See the UCSM Python SDK for information on properties for each class. |
| 50 |
required: true |
| 51 |
type: dict |
| 52 |
children: |
| 53 |
description: |
| 54 |
- Optional list of child objects. Each child has its own module, class, and properties suboptions. |
| 55 |
- The parent_mo_or_dn property for child objects is automatically set as the list of children is configured. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
required: true |
| 59 |
requirements: |
| 60 |
- ucsmsdk |
| 61 |
author: |
| 62 |
- David Soper (@dsoper2) |
| 63 |
- CiscoUcs (@CiscoUcs) |
| 64 |
''' |
| 65 |
|
| 66 |
EXAMPLES = r''' |
| 67 |
- name: Configure Network Control Policy |
| 68 |
cisco.ucs.ucs_managed_objects: |
| 69 |
hostname: 172.16.143.150 |
| 70 |
username: admin |
| 71 |
password: password |
| 72 |
objects: |
| 73 |
- module: ucsmsdk.mometa.nwctrl.NwctrlDefinition |
| 74 |
class: NwctrlDefinition |
| 75 |
properties: |
| 76 |
parent_mo_or_dn: org-root |
| 77 |
cdp: enabled |
| 78 |
descr: '' |
| 79 |
lldp_receive: enabled |
| 80 |
lldp_transmit: enabled |
| 81 |
name: Enable-CDP-LLDP |
| 82 |
|
| 83 |
- name: Remove Network Control Policy |
| 84 |
cisco.ucs.ucs_managed_objects: |
| 85 |
hostname: 172.16.143.150 |
| 86 |
username: admin |
| 87 |
password: password |
| 88 |
objects: |
| 89 |
- module: ucsmsdk.mometa.nwctrl.NwctrlDefinition |
| 90 |
class: NwctrlDefinition |
| 91 |
properties: |
| 92 |
parent_mo_or_dn: org-root |
| 93 |
name: Enable-CDP-LLDP |
| 94 |
state: absent |
| 95 |
|
| 96 |
- name: Configure Boot Policy Using JSON objects list with children |
| 97 |
cisco.ucs.ucs_managed_objects: |
| 98 |
hostname: 172.16.143.150 |
| 99 |
username: admin |
| 100 |
password: password |
| 101 |
objects: |
| 102 |
- { |
| 103 |
"module": "ucsmsdk.mometa.lsboot.LsbootPolicy", |
| 104 |
"class": "LsbootPolicy", |
| 105 |
"properties": { |
| 106 |
"parent_mo_or_dn": "org-root", |
| 107 |
"name": "Python_SDS", |
| 108 |
"enforce_vnic_name": "yes", |
| 109 |
"boot_mode": "legacy", |
| 110 |
"reboot_on_update": "no" |
| 111 |
}, |
| 112 |
"children": [ |
| 113 |
{ |
| 114 |
"module": "ucsmsdk.mometa.lsboot.LsbootVirtualMedia", |
| 115 |
"class": "LsbootVirtualMedia", |
| 116 |
"properties": { |
| 117 |
"access": "read-only-local", |
| 118 |
"lun_id": "0", |
| 119 |
"order": "2" |
| 120 |
} |
| 121 |
}, |
| 122 |
{ |
| 123 |
"module": "ucsmsdk.mometa.lsboot.LsbootStorage", |
| 124 |
"class": "LsbootStorage", |
| 125 |
"properties": { |
| 126 |
"order": "1" |
| 127 |
}, |
| 128 |
"children": [ |
| 129 |
{ |
| 130 |
"module": "ucsmsdk.mometa.lsboot.LsbootLocalStorage", |
| 131 |
"class": "LsbootLocalStorage", |
| 132 |
"properties": {}, |
| 133 |
"children": [ |
| 134 |
{ |
| 135 |
"module": "ucsmsdk.mometa.lsboot.LsbootDefaultLocalImage", |
| 136 |
"class": "LsbootDefaultLocalImage", |
| 137 |
"properties": { |
| 138 |
"order": "1" |
| 139 |
} |
| 140 |
} |
| 141 |
] |
| 142 |
} |
| 143 |
] |
| 144 |
} |
| 145 |
] |
| 146 |
} |
| 147 |
|
| 148 |
- name: Remove Boot Policy Using JSON objects list |
| 149 |
cisco.ucs.ucs_managed_objects: |
| 150 |
hostname: 172.16.143.150 |
| 151 |
username: admin |
| 152 |
password: password |
| 153 |
objects: |
| 154 |
- { |
| 155 |
"module": "ucsmsdk.mometa.lsboot.LsbootPolicy", |
| 156 |
"class": "LsbootPolicy", |
| 157 |
"properties": { |
| 158 |
"parent_mo_or_dn": "org-root", |
| 159 |
"name": "Python_SDS" |
| 160 |
} |
| 161 |
} |
| 162 |
state: absent |
| 163 |
|
| 164 |
|
| 165 |
''' |
| 166 |
|
| 167 |
RETURN = r''' |
| 168 |
# |
| 169 |
''' |
| 170 |
|
| 171 |
try: |
| 172 |
from importlib import import_module |
| 173 |
HAS_IMPORT_MODULE = True |
| 174 |
except ImportError: |
| 175 |
HAS_IMPORT_MODULE = False |
| 176 |
|
| 177 |
from copy import deepcopy |
| 178 |
from ansible.module_utils.basic import AnsibleModule |
| 179 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 180 |
|
| 181 |
|
| 182 |
def traverse_objects(module, ucs, managed_object, mo=''): |
| 183 |
props_match = False |
| 184 |
|
| 185 |
mo_module = import_module(managed_object['module']) |
| 186 |
mo_class = getattr(mo_module, managed_object['class']) |
| 187 |
|
| 188 |
if not managed_object['properties'].get('parent_mo_or_dn'): |
| 189 |
managed_object['properties']['parent_mo_or_dn'] = mo |
| 190 |
|
| 191 |
mo = mo_class(**managed_object['properties']) |
| 192 |
|
| 193 |
existing_mo = ucs.login_handle.query_dn(mo.dn) |
| 194 |
|
| 195 |
if module.params['state'] == 'absent': |
| 196 |
# mo must exist, but all properties do not have to match |
| 197 |
if existing_mo: |
| 198 |
if not module.check_mode: |
| 199 |
ucs.login_handle.remove_mo(existing_mo) |
| 200 |
ucs.result['changed'] = True |
| 201 |
else: |
| 202 |
if existing_mo: |
| 203 |
# check mo props |
| 204 |
kwargs = dict(managed_object['properties']) |
| 205 |
# remove parent info and passwords because those aren't presented in the actual props |
| 206 |
kwargs.pop('parent_mo_or_dn', None) |
| 207 |
kwargs.pop('pwd', None) |
| 208 |
kwargs.pop('password', None) |
| 209 |
if existing_mo.check_prop_match(**kwargs): |
| 210 |
props_match = True |
| 211 |
|
| 212 |
if not props_match: |
| 213 |
if not module.check_mode: |
| 214 |
ucs.login_handle.add_mo(mo, modify_present=True) |
| 215 |
ucs.result['changed'] = True |
| 216 |
|
| 217 |
if managed_object.get('children'): |
| 218 |
for child in managed_object['children']: |
| 219 |
# explicit deep copy of child object since traverse_objects may modify parent mo information |
| 220 |
copy_of_child = deepcopy(child) |
| 221 |
traverse_objects(module, ucs, copy_of_child, mo) |
| 222 |
|
| 223 |
|
| 224 |
def main(): |
| 225 |
object_dict = dict( |
| 226 |
module=dict(type='str', required=True), |
| 227 |
class_name=dict(type='str', aliases=['class'], required=True), |
| 228 |
properties=dict(type='dict', required=True), |
| 229 |
children=dict(type='list', elements='dict'), |
| 230 |
) |
| 231 |
argument_spec = ucs_argument_spec.copy() |
| 232 |
argument_spec.update( |
| 233 |
objects=dict(type='list', elements='dict', options=object_dict, required=True), |
| 234 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 235 |
) |
| 236 |
|
| 237 |
module = AnsibleModule( |
| 238 |
argument_spec, |
| 239 |
supports_check_mode=True, |
| 240 |
) |
| 241 |
|
| 242 |
if not HAS_IMPORT_MODULE: |
| 243 |
module.fail_json(msg='import_module is required for this module') |
| 244 |
ucs = UCSModule(module) |
| 245 |
|
| 246 |
# note that all objects specified in the object list report a single result (including a single changed). |
| 247 |
ucs.result['changed'] = False |
| 248 |
|
| 249 |
for managed_object in module.params['objects']: |
| 250 |
traverse_objects(module, ucs, managed_object) |
| 251 |
# single commit for object and any children |
| 252 |
if not module.check_mode and ucs.result['changed']: |
| 253 |
try: |
| 254 |
ucs.login_handle.commit() |
| 255 |
except Exception as e: |
| 256 |
# generic Exception because UCSM can throw a variety of exceptions |
| 257 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 258 |
module.fail_json(**ucs.result) |
| 259 |
|
| 260 |
module.exit_json(**ucs.result) |
| 261 |
|
| 262 |
|
| 263 |
if __name__ == '__main__': |
| 264 |
main() |