| 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_server_profile |
| 16 |
short_description: Server Profile configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Server Profile configuration for Cisco Intersight. |
| 19 |
- Used to configure Server Profiles with assigned servers and server policies. |
| 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 and Policies 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 Server Profile. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
target_platform: |
| 43 |
description: |
| 44 |
- The platform for which the server profile is applicable. |
| 45 |
- Can either be a server that is operating in Standalone mode or which is attached to a Fabric Interconnect (FIAttached) managed by Intersight. |
| 46 |
type: str |
| 47 |
choices: [Standalone, FIAttached] |
| 48 |
default: Standalone |
| 49 |
tags: |
| 50 |
description: |
| 51 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
default: [] |
| 55 |
description: |
| 56 |
description: |
| 57 |
- The user-defined description of the Server Profile. |
| 58 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 59 |
type: str |
| 60 |
aliases: [descr] |
| 61 |
default: '' |
| 62 |
assigned_server: |
| 63 |
description: |
| 64 |
- Managed Object ID (MOID) of assigned server. |
| 65 |
- Option can be omitted if user wishes to assign server later. |
| 66 |
- Mutually exclusive with C(assigned_server_serial). |
| 67 |
type: str |
| 68 |
assigned_server_serial: |
| 69 |
description: |
| 70 |
- Serial number of the server to assign. |
| 71 |
- The module will resolve the serial number to a MOID via the Intersight API. |
| 72 |
- Option can be omitted if user wishes to assign server later. |
| 73 |
- Mutually exclusive with C(assigned_server). |
| 74 |
type: str |
| 75 |
adapter_policy: |
| 76 |
description: |
| 77 |
- Name of Adapter Policy to associate with this profile. |
| 78 |
type: str |
| 79 |
bios_policy: |
| 80 |
description: |
| 81 |
- Name of BIOS Policy to associate with this profile. |
| 82 |
type: str |
| 83 |
boot_order_policy: |
| 84 |
description: |
| 85 |
- Name of Boot Order Policy to associate with this profile. |
| 86 |
type: str |
| 87 |
certificate_policy: |
| 88 |
description: |
| 89 |
- Name of Certificate Policy to associate with this profile. |
| 90 |
type: str |
| 91 |
drive_security_policy: |
| 92 |
description: |
| 93 |
- Name of Drive Security Policy to associate with this profile. |
| 94 |
type: str |
| 95 |
firmware_policy: |
| 96 |
description: |
| 97 |
- Name of Firmware Policy to associate with this profile. |
| 98 |
type: str |
| 99 |
imc_access_policy: |
| 100 |
description: |
| 101 |
- Name of IMC Access Policy to associate with this profile. |
| 102 |
type: str |
| 103 |
ipmi_over_lan_policy: |
| 104 |
description: |
| 105 |
- Name of IPMI over LAN Policy to associate with this profile. |
| 106 |
type: str |
| 107 |
lan_connectivity_policy: |
| 108 |
description: |
| 109 |
- Name of LAN Connectivity Policy to associate with this profile. |
| 110 |
type: str |
| 111 |
ldap_policy: |
| 112 |
description: |
| 113 |
- Name of LDAP Policy to associate with this profile. |
| 114 |
type: str |
| 115 |
local_user_policy: |
| 116 |
description: |
| 117 |
- Name of Local User Policy to associate with this profile. |
| 118 |
type: str |
| 119 |
network_connectivity_policy: |
| 120 |
description: |
| 121 |
- Name of Network Connectivity Policy to associate with this profile. |
| 122 |
type: str |
| 123 |
ntp_policy: |
| 124 |
description: |
| 125 |
- Name of NTP Policy to associate with this profile. |
| 126 |
type: str |
| 127 |
power_policy: |
| 128 |
description: |
| 129 |
- Name of Power Policy to associated with this profile. |
| 130 |
type: str |
| 131 |
san_connectivity_policy: |
| 132 |
description: |
| 133 |
- Name of SAN Connectivity Policy to associate with this profile. |
| 134 |
type: str |
| 135 |
sd_card_policy: |
| 136 |
description: |
| 137 |
- Name of SD Card Policy to associate with this profile. |
| 138 |
type: str |
| 139 |
serial_over_lan_policy: |
| 140 |
description: |
| 141 |
- Name of Serial over LAN Policy to associate with this profile. |
| 142 |
type: str |
| 143 |
smtp_policy: |
| 144 |
description: |
| 145 |
- Name of SMTP Policy to associate with this profile. |
| 146 |
type: str |
| 147 |
snmp_policy: |
| 148 |
description: |
| 149 |
- Name of SNMP Policy to associate with this profile. |
| 150 |
type: str |
| 151 |
ssh_policy: |
| 152 |
description: |
| 153 |
- Name of SSH Policy to associate with this profile. |
| 154 |
type: str |
| 155 |
storage_policy: |
| 156 |
description: |
| 157 |
- Name of Storage Policy to associate with this profile. |
| 158 |
type: str |
| 159 |
syslog_policy: |
| 160 |
description: |
| 161 |
- Name of Syslog Policy to associate with this profile. |
| 162 |
type: str |
| 163 |
thermal_policy: |
| 164 |
description: |
| 165 |
- Name of Thermal Policy to associate with this profile. |
| 166 |
type: str |
| 167 |
virtual_kvm_policy: |
| 168 |
description: |
| 169 |
- Name of Virtual KVM Policy to associate with this profile. |
| 170 |
type: str |
| 171 |
virtual_media_policy: |
| 172 |
description: |
| 173 |
- Name of Virtual Media Policy to associate with this profile. |
| 174 |
type: str |
| 175 |
uuid_address_type: |
| 176 |
description: |
| 177 |
- UUID address allocation type for the server. |
| 178 |
- C(pool) to assign UUID from a UUID pool. |
| 179 |
- C(static) to assign a static UUID address. |
| 180 |
type: str |
| 181 |
choices: [pool, static] |
| 182 |
uuid_pool: |
| 183 |
description: |
| 184 |
- Name of the UUID pool to assign UUID from. |
| 185 |
- Required when C(uuid_address_type) is C(pool). |
| 186 |
type: str |
| 187 |
static_uuid_address: |
| 188 |
description: |
| 189 |
- Static UUID address to assign to the server. |
| 190 |
- Must include UUID prefix xxxxxxxx-xxxx-xxxx along with the UUID suffix of format xxxx-xxxxxxxxxxxx. |
| 191 |
- Example format 550e8400-e29b-41d4-a716-446655440000. |
| 192 |
- Required when C(uuid_address_type) is C(static). |
| 193 |
type: str |
| 194 |
author: |
| 195 |
- David Soper (@dsoper2) |
| 196 |
- Sid Nath (@SidNath21) |
| 197 |
- Tse Kai "Kevin" Chan (@BrightScale) |
| 198 |
- Soma Tummala (@SOMATUMMALA21) |
| 199 |
''' |
| 200 |
|
| 201 |
EXAMPLES = r''' |
| 202 |
- name: Configure Server Profile |
| 203 |
cisco.intersight.intersight_server_profile: |
| 204 |
api_private_key: "{{ api_private_key }}" |
| 205 |
api_key_id: "{{ api_key_id }}" |
| 206 |
name: SP-Server1 |
| 207 |
target_platform: FIAttached |
| 208 |
tags: |
| 209 |
- Key: Site |
| 210 |
Value: SJC02 |
| 211 |
description: Profile for Server1 |
| 212 |
assigned_server: 5e3b517d6176752d319a9999 |
| 213 |
boot_order_policy: COS-Boot |
| 214 |
imc_access_policy: sjc02-d23-access |
| 215 |
lan_connectivity_policy: sjc02-d23-lan |
| 216 |
local_user_policy: guest-admin |
| 217 |
ntp_policy: lab-ntp |
| 218 |
storage_policy: storage |
| 219 |
virtual_media_policy: COS-VM |
| 220 |
|
| 221 |
- name: Configure Server Profile with UUID from Pool |
| 222 |
cisco.intersight.intersight_server_profile: |
| 223 |
api_private_key: "{{ api_private_key }}" |
| 224 |
api_key_id: "{{ api_key_id }}" |
| 225 |
name: SP-Server2 |
| 226 |
target_platform: FIAttached |
| 227 |
description: Profile with UUID from pool |
| 228 |
assigned_server: 5e3b517d6176752d319a9998 |
| 229 |
uuid_address_type: pool |
| 230 |
uuid_pool: UUID-Pool-01 |
| 231 |
boot_order_policy: COS-Boot |
| 232 |
|
| 233 |
- name: Configure Server Profile with Static UUID |
| 234 |
cisco.intersight.intersight_server_profile: |
| 235 |
api_private_key: "{{ api_private_key }}" |
| 236 |
api_key_id: "{{ api_key_id }}" |
| 237 |
name: SP-Server3 |
| 238 |
target_platform: Standalone |
| 239 |
description: Profile with static UUID |
| 240 |
assigned_server: 5e3b517d6176752d319a9997 |
| 241 |
uuid_address_type: static |
| 242 |
static_uuid_address: 550e8400-e29b-41d4-a716-446655440000 |
| 243 |
|
| 244 |
- name: Configure Server Profile with server serial number |
| 245 |
cisco.intersight.intersight_server_profile: |
| 246 |
api_private_key: "{{ api_private_key }}" |
| 247 |
api_key_id: "{{ api_key_id }}" |
| 248 |
name: SP-Server4 |
| 249 |
target_platform: FIAttached |
| 250 |
description: Profile assigned by serial number |
| 251 |
assigned_server_serial: FCH2149V0GN |
| 252 |
boot_order_policy: COS-Boot |
| 253 |
|
| 254 |
- name: Delete Server Profile |
| 255 |
cisco.intersight.intersight_server_profile: |
| 256 |
api_private_key: "{{ api_private_key }}" |
| 257 |
api_key_id: "{{ api_key_id }}" |
| 258 |
name: SP-Server1 |
| 259 |
state: absent |
| 260 |
''' |
| 261 |
|
| 262 |
RETURN = r''' |
| 263 |
api_response: |
| 264 |
description: The API response output returned by the specified resource. |
| 265 |
returned: always |
| 266 |
type: dict |
| 267 |
sample: |
| 268 |
"api_response": { |
| 269 |
"AssignedServer": { |
| 270 |
"Moid": "5e3b517d6176752d319a0881", |
| 271 |
"ObjectType": "compute.Blade", |
| 272 |
}, |
| 273 |
"Name": "SP-IMM-6454-D23-1-1", |
| 274 |
"ObjectType": "server.Profile", |
| 275 |
"Tags": [ |
| 276 |
{ |
| 277 |
"Key": "Site", |
| 278 |
"Value": "SJC02" |
| 279 |
} |
| 280 |
], |
| 281 |
"TargetPlatform": "FIAttached", |
| 282 |
"Type": "instance" |
| 283 |
} |
| 284 |
''' |
| 285 |
|
| 286 |
|
| 287 |
from ansible.module_utils.basic import AnsibleModule |
| 288 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 289 |
|
| 290 |
|
| 291 |
# When adding new policy parameters, update this dict with their respective resource path |
| 292 |
policy_resource_path = { |
| 293 |
'adapter_policy': '/adapter/ConfigPolicies', |
| 294 |
'bios_policy': '/bios/Policies', |
| 295 |
'boot_order_policy': '/boot/PrecisionPolicies', |
| 296 |
'certificate_policy': '/certificatemanagement/Policies', |
| 297 |
'drive_security_policy': '/storage/DriveSecurityPolicies', |
| 298 |
'firmware_policy': '/firmware/Policies', |
| 299 |
'imc_access_policy': '/access/Policies', |
| 300 |
'ipmi_over_lan_policy': '/ipmioverlan/Policies', |
| 301 |
'lan_connectivity_policy': '/vnic/LanConnectivityPolicies', |
| 302 |
'local_user_policy': '/iam/EndPointUserPolicies', |
| 303 |
'ldap_policy': '/iam/LdapPolicies', |
| 304 |
'network_connectivity_policy': '/networkconfig/Policies', |
| 305 |
'ntp_policy': '/ntp/Policies', |
| 306 |
'power_policy': '/power/Policies', |
| 307 |
'san_connectivity_policy': '/vnic/SanConnectivityPolicies', |
| 308 |
'sd_card_policy': '/sdcard/Policies', |
| 309 |
'serial_over_lan_policy': '/sol/Policies', |
| 310 |
'smtp_policy': '/smtp/Policies', |
| 311 |
'snmp_policy': '/snmp/Policies', |
| 312 |
'ssh_policy': '/ssh/Policies', |
| 313 |
'storage_policy': '/storage/StoragePolicies', |
| 314 |
'syslog_policy': '/syslog/Policies', |
| 315 |
'thermal_policy': '/thermal/Policies', |
| 316 |
'virtual_kvm_policy': '/kvm/Policies', |
| 317 |
'virtual_media_policy': '/vmedia/Policies', |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
def post_profile_to_policy(intersight, moid, resource_path, policy_name): |
| 322 |
options = { |
| 323 |
'http_method': 'get', |
| 324 |
'resource_path': resource_path, |
| 325 |
'query_params': { |
| 326 |
'$filter': "Name eq '" + policy_name + "'", |
| 327 |
}, |
| 328 |
} |
| 329 |
response = intersight.call_api(**options) |
| 330 |
if response.get('Results'): |
| 331 |
# get expected policy moid from 1st list element |
| 332 |
expected_policy_moid = response['Results'][0]['Moid'] |
| 333 |
actual_policy_moid = '' |
| 334 |
# check any current profiles and delete if needed |
| 335 |
options = { |
| 336 |
'http_method': 'get', |
| 337 |
'resource_path': resource_path, |
| 338 |
'query_params': { |
| 339 |
'$filter': "Profiles/any(t: t/Moid eq '" + moid + "')", |
| 340 |
}, |
| 341 |
} |
| 342 |
response = intersight.call_api(**options) |
| 343 |
if response.get('Results'): |
| 344 |
# get actual moid from 1st list element |
| 345 |
actual_policy_moid = response['Results'][0]['Moid'] |
| 346 |
if actual_policy_moid != expected_policy_moid: |
| 347 |
if not intersight.module.check_mode: |
| 348 |
# delete the actual policy |
| 349 |
options = { |
| 350 |
'http_method': 'delete', |
| 351 |
'resource_path': resource_path + '/' + actual_policy_moid + '/Profiles', |
| 352 |
'moid': moid, |
| 353 |
} |
| 354 |
intersight.call_api(**options) |
| 355 |
actual_policy_moid = '' |
| 356 |
if not actual_policy_moid: |
| 357 |
if not intersight.module.check_mode: |
| 358 |
# post profile to the expected policy |
| 359 |
options = { |
| 360 |
'http_method': 'post', |
| 361 |
'resource_path': resource_path + '/' + expected_policy_moid + '/Profiles', |
| 362 |
'body': [ |
| 363 |
{ |
| 364 |
'ObjectType': 'server.Profile', |
| 365 |
'Moid': moid, |
| 366 |
} |
| 367 |
] |
| 368 |
} |
| 369 |
intersight.call_api(**options) |
| 370 |
intersight.result['changed'] = True |
| 371 |
|
| 372 |
|
| 373 |
def main(): |
| 374 |
argument_spec = intersight_argument_spec.copy() |
| 375 |
argument_spec.update( |
| 376 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 377 |
organization=dict(type='str', default='default'), |
| 378 |
name=dict(type='str', required=True), |
| 379 |
target_platform=dict(type='str', choices=['Standalone', 'FIAttached'], default='Standalone'), |
| 380 |
tags=dict(type='list', elements='dict', default=[]), |
| 381 |
description=dict(type='str', aliases=['descr'], default=''), |
| 382 |
assigned_server=dict(type='str'), |
| 383 |
assigned_server_serial=dict(type='str'), |
| 384 |
adapter_policy=dict(type='str'), |
| 385 |
bios_policy=dict(type='str'), |
| 386 |
boot_order_policy=dict(type='str'), |
| 387 |
certificate_policy=dict(type='str'), |
| 388 |
drive_security_policy=dict(type='str'), |
| 389 |
firmware_policy=dict(type='str'), |
| 390 |
imc_access_policy=dict(type='str'), |
| 391 |
ipmi_over_lan_policy=dict(type='str'), |
| 392 |
lan_connectivity_policy=dict(type='str'), |
| 393 |
ldap_policy=dict(type='str'), |
| 394 |
local_user_policy=dict(type='str'), |
| 395 |
network_connectivity_policy=dict(type='str'), |
| 396 |
ntp_policy=dict(type='str'), |
| 397 |
power_policy=dict(type='str'), |
| 398 |
san_connectivity_policy=dict(type='str'), |
| 399 |
sd_card_policy=dict(type='str'), |
| 400 |
serial_over_lan_policy=dict(type='str'), |
| 401 |
smtp_policy=dict(type='str'), |
| 402 |
snmp_policy=dict(type='str'), |
| 403 |
ssh_policy=dict(type='str'), |
| 404 |
storage_policy=dict(type='str'), |
| 405 |
syslog_policy=dict(type='str'), |
| 406 |
thermal_policy=dict(type='str'), |
| 407 |
virtual_kvm_policy=dict(type='str'), |
| 408 |
virtual_media_policy=dict(type='str'), |
| 409 |
uuid_address_type=dict(type='str', choices=['pool', 'static']), |
| 410 |
uuid_pool=dict(type='str'), |
| 411 |
static_uuid_address=dict(type='str'), |
| 412 |
) |
| 413 |
|
| 414 |
module = AnsibleModule( |
| 415 |
argument_spec, |
| 416 |
supports_check_mode=True, |
| 417 |
mutually_exclusive=[ |
| 418 |
['assigned_server', 'assigned_server_serial'], |
| 419 |
], |
| 420 |
required_if=[ |
| 421 |
['uuid_address_type', 'pool', ['uuid_pool']], |
| 422 |
['uuid_address_type', 'static', ['static_uuid_address']], |
| 423 |
], |
| 424 |
) |
| 425 |
|
| 426 |
intersight = IntersightModule(module) |
| 427 |
intersight.result['api_response'] = {} |
| 428 |
intersight.result['trace_id'] = '' |
| 429 |
|
| 430 |
resource_path = '/server/Profiles' |
| 431 |
# Define API body used in compares or create |
| 432 |
intersight.api_body = { |
| 433 |
'Organization': { |
| 434 |
'Name': intersight.module.params['organization'], |
| 435 |
}, |
| 436 |
'Name': intersight.module.params['name'], |
| 437 |
|
| 438 |
} |
| 439 |
intersight.set_tags_and_description() |
| 440 |
intersight.result['api_response'] = {} |
| 441 |
# Get assigned server information (if defined) |
| 442 |
if intersight.module.params['assigned_server'] or intersight.module.params['assigned_server_serial']: |
| 443 |
if intersight.module.params['assigned_server_serial']: |
| 444 |
server_filter = "Serial eq '" + intersight.module.params['assigned_server_serial'] + "'" |
| 445 |
else: |
| 446 |
server_filter = "Moid eq '" + intersight.module.params['assigned_server'] + "'" |
| 447 |
intersight.get_resource( |
| 448 |
resource_path='/compute/PhysicalSummaries', |
| 449 |
query_params={ |
| 450 |
'$filter': server_filter, |
| 451 |
} |
| 452 |
) |
| 453 |
if intersight.module.params['assigned_server_serial'] and not intersight.result['api_response'].get('Moid'): |
| 454 |
module.fail_json( |
| 455 |
msg=f"Server with serial number '{intersight.module.params['assigned_server_serial']}' not found in Intersight." |
| 456 |
) |
| 457 |
source_object_type = None |
| 458 |
if intersight.result['api_response'].get('SourceObjectType'): |
| 459 |
source_object_type = intersight.result['api_response']['SourceObjectType'] |
| 460 |
if intersight.module.params['assigned_server']: |
| 461 |
server_moid = intersight.module.params['assigned_server'] |
| 462 |
else: |
| 463 |
server_moid = intersight.result['api_response']['Moid'] |
| 464 |
intersight.api_body['AssignedServer'] = { |
| 465 |
'Moid': server_moid, |
| 466 |
'ObjectType': source_object_type, |
| 467 |
} |
| 468 |
if intersight.module.params['target_platform'] == 'FIAttached': |
| 469 |
intersight.api_body['TargetPlatform'] = intersight.module.params['target_platform'] |
| 470 |
if intersight.module.params.get('uuid_address_type'): |
| 471 |
intersight.api_body['UuidAddressType'] = intersight.module.params['uuid_address_type'].upper() |
| 472 |
if intersight.module.params['uuid_address_type'] == 'pool': |
| 473 |
uuid_pool_moid = intersight.get_moid_by_name( |
| 474 |
resource_path='/uuidpool/Pools', |
| 475 |
resource_name=intersight.module.params['uuid_pool'] |
| 476 |
) |
| 477 |
if not uuid_pool_moid: |
| 478 |
module.fail_json(msg=f"UUID Pool '{intersight.module.params['uuid_pool']}' not found") |
| 479 |
intersight.api_body['UuidPool'] = { |
| 480 |
'Moid': uuid_pool_moid, |
| 481 |
'ObjectType': 'uuidpool.Pool' |
| 482 |
} |
| 483 |
intersight.api_body['StaticUuidAddress'] = '' |
| 484 |
elif intersight.module.params['uuid_address_type'] == 'static': |
| 485 |
intersight.api_body['StaticUuidAddress'] = intersight.module.params['static_uuid_address'] |
| 486 |
intersight.api_body['UuidPool'] = None |
| 487 |
|
| 488 |
# Configure the profile |
| 489 |
moid = intersight.configure_policy_or_profile(resource_path=resource_path) |
| 490 |
|
| 491 |
if moid: |
| 492 |
for k, v in policy_resource_path.items(): |
| 493 |
if intersight.module.params[k]: |
| 494 |
post_profile_to_policy(intersight, moid, resource_path=v, policy_name=intersight.module.params[k]) |
| 495 |
|
| 496 |
module.exit_json(**intersight.result) |
| 497 |
|
| 498 |
|
| 499 |
if __name__ == '__main__': |
| 500 |
main() |