| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from __future__ import (absolute_import, division, print_function) |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: ome_profile |
| 17 |
short_description: Create, modify, delete, assign, unassign and migrate a profile on OpenManage Enterprise |
| 18 |
version_added: "3.1.0" |
| 19 |
description: "This module allows to create, modify, delete, assign, unassign, and migrate a profile on OpenManage Enterprise." |
| 20 |
extends_documentation_fragment: |
| 21 |
- dellemc.openmanage.ome_auth_options |
| 22 |
options: |
| 23 |
command: |
| 24 |
description: |
| 25 |
- C(create) creates new profiles. |
| 26 |
- "C(modify) modifies an existing profile. Only I(name), I(description), I(boot_to_network_iso), and I(attributes) |
| 27 |
can be modified." |
| 28 |
- C(delete) deletes an existing profile. |
| 29 |
- C(assign) Deploys an existing profile on a target device and returns a task ID. |
| 30 |
- C(unassign) unassigns a profile from a specified target and returns a task ID. |
| 31 |
- C(migrate) migrates an existing profile and returns a task ID. |
| 32 |
choices: [create, modify, delete, assign, unassign, migrate] |
| 33 |
default: create |
| 34 |
type: str |
| 35 |
name_prefix: |
| 36 |
description: |
| 37 |
- The name provided when creating a profile is used a prefix followed by the number assigned to it by OpenManage Enterprise. |
| 38 |
- This is applicable only for a create operation. |
| 39 |
- This option is mutually exclusive with I(name). |
| 40 |
type: str |
| 41 |
default: Profile |
| 42 |
name: |
| 43 |
description: |
| 44 |
- Name of the profile. |
| 45 |
- This is applicable for modify, delete, assign, unassign, and migrate operations. |
| 46 |
- This option is mutually exclusive with I(name_prefix) and I(number_of_profiles). |
| 47 |
type: str |
| 48 |
new_name: |
| 49 |
description: |
| 50 |
- New name of the profile. |
| 51 |
- Applicable when I(command) is C(modify). |
| 52 |
type: str |
| 53 |
number_of_profiles: |
| 54 |
description: |
| 55 |
- Provide the number of profiles to be created. |
| 56 |
- This is applicable when I(name_prefix) is used with C(create). |
| 57 |
- This option is mutually exclusive with I(name). |
| 58 |
- Openmanage Enterprise can create a maximum of 100 profiles. |
| 59 |
type: int |
| 60 |
default: 1 |
| 61 |
template_name: |
| 62 |
description: |
| 63 |
- Name of the template for creating the profile(s). |
| 64 |
- This is applicable when I(command) is C(create). |
| 65 |
- This option is mutually exclusive with I(template_id). |
| 66 |
type: str |
| 67 |
template_id: |
| 68 |
description: |
| 69 |
- ID of the template. |
| 70 |
- This is applicable when I(command) is C(create). |
| 71 |
- This option is mutually exclusive with I(template_name). |
| 72 |
type: int |
| 73 |
device_id: |
| 74 |
description: |
| 75 |
- ID of the target device. |
| 76 |
- This is applicable when I(command) is C(assign) and C(migrate). |
| 77 |
- This option is mutually exclusive with I(device_service_tag). |
| 78 |
type: int |
| 79 |
device_service_tag: |
| 80 |
description: |
| 81 |
- Identifier of the target device. |
| 82 |
- This is typically 7 to 8 characters in length. |
| 83 |
- Applicable when I(command) is C(assign), and C(migrate). |
| 84 |
- This option is mutually exclusive with I(device_id). |
| 85 |
- If the device does not exist when I(command) is C(assign) then the profile is auto-deployed. |
| 86 |
type: str |
| 87 |
description: |
| 88 |
description: Description of the profile. |
| 89 |
type: str |
| 90 |
boot_to_network_iso: |
| 91 |
description: |
| 92 |
- Details of the Share iso. |
| 93 |
- Applicable when I(command) is C(create), C(assign), and C(modify). |
| 94 |
type: dict |
| 95 |
suboptions: |
| 96 |
boot_to_network: |
| 97 |
description: Enable or disable a network share. |
| 98 |
type: bool |
| 99 |
required: true |
| 100 |
share_type: |
| 101 |
description: Type of network share. |
| 102 |
type: str |
| 103 |
choices: [NFS, CIFS] |
| 104 |
share_ip: |
| 105 |
description: IP address of the network share. |
| 106 |
type: str |
| 107 |
share_user: |
| 108 |
description: User name when I(share_type) is C(CIFS). |
| 109 |
type: str |
| 110 |
share_password: |
| 111 |
description: User password when I(share_type) is C(CIFS). |
| 112 |
type: str |
| 113 |
workgroup: |
| 114 |
description: User workgroup when I(share_type) is C(CIFS). |
| 115 |
type: str |
| 116 |
iso_path: |
| 117 |
description: Specify the full ISO path including the share name. |
| 118 |
type: str |
| 119 |
iso_timeout: |
| 120 |
description: Set the number of hours that the network ISO file will remain mapped to the target device(s). |
| 121 |
type: int |
| 122 |
choices: [1, 2, 4, 8, 16] |
| 123 |
default: 4 |
| 124 |
filters: |
| 125 |
description: |
| 126 |
- Filters the profiles based on selected criteria. |
| 127 |
- This is applicable when I(command) is C(delete) or C(unassign). |
| 128 |
- This supports suboption I(ProfileIds) which takes a list of profile IDs. |
| 129 |
- This also supports OData filter expressions with the suboption I(Filters). |
| 130 |
- See OpenManage Enterprise REST API guide for the filtering options available. |
| 131 |
- I(WARNING) When this option is used in case of C(unassign), task ID is not returned for any of the profiles affected. |
| 132 |
type: dict |
| 133 |
force: |
| 134 |
description: |
| 135 |
- Provides the option to force the migration of a profile even if the source device cannot be contacted. |
| 136 |
- This option is applicable when I(command) is C(migrate). |
| 137 |
type: bool |
| 138 |
default: false |
| 139 |
attributes: |
| 140 |
description: Attributes for C(modify) and C(assign). |
| 141 |
type: dict |
| 142 |
suboptions: |
| 143 |
Attributes: |
| 144 |
description: |
| 145 |
- List of attributes to be modified, when I(command) is C(modify). |
| 146 |
- List of attributes to be overridden when I(command) is C(assign). |
| 147 |
- "Use the I(Id) If the attribute Id is available. If not, use the comma separated I (DisplayName). |
| 148 |
For more details about using the I(DisplayName), see the example provided." |
| 149 |
type: list |
| 150 |
elements: dict |
| 151 |
Options: |
| 152 |
description: |
| 153 |
- Provides the different shut down options. |
| 154 |
- This is applicable when I(command) is C(assign). |
| 155 |
type: dict |
| 156 |
Schedule: |
| 157 |
description: |
| 158 |
- Schedule for profile deployment. |
| 159 |
- This is applicable when I(command) is C(assign). |
| 160 |
type: dict |
| 161 |
requirements: |
| 162 |
- "python >= 3.9.6" |
| 163 |
author: |
| 164 |
- "Jagadeesh N V (@jagadeeshnv)" |
| 165 |
- "Bhavneet Sharma (@Bhavneet-Sharma)" |
| 166 |
notes: |
| 167 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 168 |
- This module supports C(check_mode). |
| 169 |
- C(assign) operation on a already assigned profile will not redeploy. |
| 170 |
''' |
| 171 |
|
| 172 |
EXAMPLES = r''' |
| 173 |
--- |
| 174 |
- name: Create two profiles from a template |
| 175 |
dellemc.openmanage.ome_profile: |
| 176 |
hostname: "192.168.0.1" |
| 177 |
username: "username" |
| 178 |
password: "password" |
| 179 |
ca_path: "/path/to/ca_cert.pem" |
| 180 |
template_name: "template 1" |
| 181 |
name_prefix: "omam_profile" |
| 182 |
number_of_profiles: 2 |
| 183 |
|
| 184 |
- name: Create profile with NFS share |
| 185 |
dellemc.openmanage.ome_profile: |
| 186 |
hostname: "192.168.0.1" |
| 187 |
username: "username" |
| 188 |
password: "password" |
| 189 |
ca_path: "/path/to/ca_cert.pem" |
| 190 |
command: create |
| 191 |
template_name: "template 1" |
| 192 |
name_prefix: "omam_profile" |
| 193 |
number_of_profiles: 1 |
| 194 |
boot_to_network_iso: |
| 195 |
boot_to_network: true |
| 196 |
share_type: NFS |
| 197 |
share_ip: "192.168.0.1" |
| 198 |
iso_path: "path/to/my_iso.iso" |
| 199 |
iso_timeout: 8 |
| 200 |
|
| 201 |
- name: Create profile with CIFS share |
| 202 |
dellemc.openmanage.ome_profile: |
| 203 |
hostname: "192.168.0.1" |
| 204 |
username: "username" |
| 205 |
password: "password" |
| 206 |
ca_path: "/path/to/ca_cert.pem" |
| 207 |
command: create |
| 208 |
template_name: "template 1" |
| 209 |
name_prefix: "omam_profile" |
| 210 |
number_of_profiles: 1 |
| 211 |
boot_to_network_iso: |
| 212 |
boot_to_network: true |
| 213 |
share_type: CIFS |
| 214 |
share_ip: "192.168.0.2" |
| 215 |
share_user: "username" |
| 216 |
share_password: "password" |
| 217 |
workgroup: "workgroup" |
| 218 |
iso_path: "\\path\\to\\my_iso.iso" |
| 219 |
iso_timeout: 8 |
| 220 |
|
| 221 |
- name: Modify profile name with NFS share and attributes |
| 222 |
dellemc.openmanage.ome_profile: |
| 223 |
hostname: "192.168.0.1" |
| 224 |
username: "username" |
| 225 |
password: "password" |
| 226 |
ca_path: "/path/to/ca_cert.pem" |
| 227 |
command: modify |
| 228 |
name: "Profile 00001" |
| 229 |
new_name: "modified profile" |
| 230 |
description: "new description" |
| 231 |
boot_to_network_iso: |
| 232 |
boot_to_network: true |
| 233 |
share_type: NFS |
| 234 |
share_ip: "192.168.0.3" |
| 235 |
iso_path: "path/to/my_iso.iso" |
| 236 |
iso_timeout: 8 |
| 237 |
attributes: |
| 238 |
Attributes: |
| 239 |
- Id: 4506 |
| 240 |
Value: "server attr 1" |
| 241 |
IsIgnored: false |
| 242 |
- Id: 4507 |
| 243 |
Value: "server attr 2" |
| 244 |
IsIgnored: false |
| 245 |
# Enter the comma separated string as appearing in the Detailed view on GUI |
| 246 |
# System -> Server Topology -> ServerTopology 1 Aisle Name |
| 247 |
- DisplayName: 'System, Server Topology, ServerTopology 1 Aisle Name' |
| 248 |
Value: Aisle 5 |
| 249 |
IsIgnored: false |
| 250 |
|
| 251 |
- name: Delete a profile using profile name |
| 252 |
dellemc.openmanage.ome_profile: |
| 253 |
hostname: "192.168.0.1" |
| 254 |
username: "username" |
| 255 |
password: "password" |
| 256 |
ca_path: "/path/to/ca_cert.pem" |
| 257 |
command: "delete" |
| 258 |
name: "Profile 00001" |
| 259 |
|
| 260 |
- name: Delete profiles using filters |
| 261 |
dellemc.openmanage.ome_profile: |
| 262 |
hostname: "192.168.0.1" |
| 263 |
username: "username" |
| 264 |
password: "password" |
| 265 |
ca_path: "/path/to/ca_cert.pem" |
| 266 |
command: "delete" |
| 267 |
filters: |
| 268 |
SelectAll: true |
| 269 |
Filters: =contains(ProfileName,'Profile 00002') |
| 270 |
|
| 271 |
- name: Delete profiles using profile list filter |
| 272 |
dellemc.openmanage.ome_profile: |
| 273 |
hostname: "192.168.0.1" |
| 274 |
username: "username" |
| 275 |
password: "password" |
| 276 |
ca_path: "/path/to/ca_cert.pem" |
| 277 |
command: "delete" |
| 278 |
filters: |
| 279 |
ProfileIds: |
| 280 |
- 17123 |
| 281 |
- 16124 |
| 282 |
|
| 283 |
- name: Assign a profile to target along with network share |
| 284 |
dellemc.openmanage.ome_profile: |
| 285 |
hostname: "192.168.0.1" |
| 286 |
username: "username" |
| 287 |
password: "password" |
| 288 |
ca_path: "/path/to/ca_cert.pem" |
| 289 |
command: assign |
| 290 |
name: "Profile 00001" |
| 291 |
device_id: 12456 |
| 292 |
boot_to_network_iso: |
| 293 |
boot_to_network: true |
| 294 |
share_type: NFS |
| 295 |
share_ip: "192.168.0.1" |
| 296 |
iso_path: "path/to/my_iso.iso" |
| 297 |
iso_timeout: 8 |
| 298 |
attributes: |
| 299 |
Attributes: |
| 300 |
- Id: 4506 |
| 301 |
Value: "server attr 1" |
| 302 |
IsIgnored: true |
| 303 |
Options: |
| 304 |
ShutdownType: 0 |
| 305 |
TimeToWaitBeforeShutdown: 300 |
| 306 |
EndHostPowerState: 1 |
| 307 |
StrictCheckingVlan: true |
| 308 |
Schedule: |
| 309 |
RunNow: true |
| 310 |
RunLater: false |
| 311 |
|
| 312 |
- name: Unassign a profile using profile name |
| 313 |
dellemc.openmanage.ome_profile: |
| 314 |
hostname: "192.168.0.1" |
| 315 |
username: "username" |
| 316 |
password: "password" |
| 317 |
ca_path: "/path/to/ca_cert.pem" |
| 318 |
command: "unassign" |
| 319 |
name: "Profile 00003" |
| 320 |
|
| 321 |
- name: Unassign profiles using filters |
| 322 |
dellemc.openmanage.ome_profile: |
| 323 |
hostname: "192.168.0.1" |
| 324 |
username: "username" |
| 325 |
password: "password" |
| 326 |
ca_path: "/path/to/ca_cert.pem" |
| 327 |
command: "unassign" |
| 328 |
filters: |
| 329 |
SelectAll: true |
| 330 |
Filters: =contains(ProfileName,'Profile 00003') |
| 331 |
|
| 332 |
- name: Unassign profiles using profile list filter |
| 333 |
dellemc.openmanage.ome_profile: |
| 334 |
hostname: "192.168.0.1" |
| 335 |
username: "username" |
| 336 |
password: "password" |
| 337 |
ca_path: "/path/to/ca_cert.pem" |
| 338 |
command: "unassign" |
| 339 |
filters: |
| 340 |
ProfileIds: |
| 341 |
- 17123 |
| 342 |
- 16123 |
| 343 |
|
| 344 |
- name: Migrate a profile |
| 345 |
dellemc.openmanage.ome_profile: |
| 346 |
hostname: "192.168.0.1" |
| 347 |
username: "username" |
| 348 |
password: "password" |
| 349 |
ca_path: "/path/to/ca_cert.pem" |
| 350 |
command: "migrate" |
| 351 |
name: "Profile 00001" |
| 352 |
device_id: 12456 |
| 353 |
''' |
| 354 |
|
| 355 |
RETURN = r''' |
| 356 |
--- |
| 357 |
msg: |
| 358 |
description: Overall status of the profile operation. |
| 359 |
returned: always |
| 360 |
type: str |
| 361 |
sample: "Successfully created 2 profile(s)." |
| 362 |
profile_ids: |
| 363 |
description: IDs of the profiles created. |
| 364 |
returned: when I(command) is C(create) |
| 365 |
type: list |
| 366 |
sample: [1234, 5678] |
| 367 |
job_id: |
| 368 |
description: |
| 369 |
- Task ID created when I(command) is C(assign), C(migrate) or C(unassign). |
| 370 |
- C(assign) and C(unassign) operations do not trigger a task if a profile is auto-deployed. |
| 371 |
returned: when I(command) is C(assign), C(migrate) or C(unassign) |
| 372 |
type: int |
| 373 |
sample: 14123 |
| 374 |
error_info: |
| 375 |
description: Details of the HTTP Error. |
| 376 |
returned: on HTTP error |
| 377 |
type: dict |
| 378 |
sample: { |
| 379 |
"error": { |
| 380 |
"code": "Base.1.0.GeneralError", |
| 381 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 382 |
"@Message.ExtendedInfo": [ |
| 383 |
{ |
| 384 |
"MessageId": "GEN1234", |
| 385 |
"RelatedProperties": [], |
| 386 |
"Message": "Unable to process the request because an error occurred.", |
| 387 |
"MessageArgs": [], |
| 388 |
"Severity": "Critical", |
| 389 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 390 |
} |
| 391 |
] |
| 392 |
} |
| 393 |
} |
| 394 |
''' |
| 395 |
|
| 396 |
import json |
| 397 |
import time |
| 398 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 399 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 400 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 401 |
from ansible.module_utils.common.dict_transformations import recursive_diff |
| 402 |
|
| 403 |
|
| 404 |
PROFILE_VIEW = "ProfileService/Profiles" |
| 405 |
GET_PORFILE_INST = "ProfileService/Profiles({profileId})" |
| 406 |
TEMPLATE_VIEW = "TemplateService/Templates" |
| 407 |
DEVICE_VIEW = "DeviceService/Devices" |
| 408 |
JOB_URI = "JobService/Jobs({job_id})" |
| 409 |
PROFILE_ACTION = "ProfileService/Actions/ProfileService.{action}" |
| 410 |
PROFILE_ATTRIBUTES = "ProfileService/Profiles({profile_id})/AttributeDetails" |
| 411 |
PROFILE_NOT_FOUND = "Profile with the name '{name}' not found." |
| 412 |
CHANGES_MSG = "Changes found to be applied." |
| 413 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 414 |
SEPRTR = ',' |
| 415 |
FILTER_QUERY_PARAM = "$filter" |
| 416 |
|
| 417 |
|
| 418 |
def get_template_details(module, rest_obj): |
| 419 |
template_identifier = module.params.get('template_id') |
| 420 |
query_param = {FILTER_QUERY_PARAM: "Id eq {0}".format(template_identifier)} |
| 421 |
srch = 'Id' |
| 422 |
if not template_identifier: |
| 423 |
template_identifier = module.params.get('template_name') |
| 424 |
query_param = {FILTER_QUERY_PARAM: "Name eq '{0}'".format(template_identifier)} |
| 425 |
srch = 'Name' |
| 426 |
resp = rest_obj.invoke_request('GET', TEMPLATE_VIEW, query_param=query_param) |
| 427 |
|
| 428 |
template_details = match_profile(resp=resp, search_key=srch, identifier_value=template_identifier) |
| 429 |
|
| 430 |
if template_details is not None: |
| 431 |
return template_details |
| 432 |
|
| 433 |
module.exit_json(msg="Template with {0} '{1}' not found.".format(srch, template_identifier), |
| 434 |
failed=True) |
| 435 |
|
| 436 |
|
| 437 |
def get_target_details(module, rest_obj): |
| 438 |
device_identifier = module.params.get('device_id') |
| 439 |
query_param = {FILTER_QUERY_PARAM: "Id eq {0}".format(device_identifier)} |
| 440 |
srch = 'Id' |
| 441 |
if not device_identifier: |
| 442 |
device_identifier = module.params.get('device_service_tag') |
| 443 |
query_param = {FILTER_QUERY_PARAM: "Identifier eq '{0}'".format(device_identifier)} |
| 444 |
srch = 'Identifier' |
| 445 |
resp = rest_obj.invoke_request('GET', DEVICE_VIEW, query_param=query_param) |
| 446 |
device_details = match_profile(resp=resp, search_key=srch, identifier_value=device_identifier) |
| 447 |
if device_details is not None: |
| 448 |
return device_details |
| 449 |
return "Target with {0} '{1}' not found.".format(srch, device_identifier) |
| 450 |
|
| 451 |
|
| 452 |
def match_profile(resp, search_key, identifier_value=None): |
| 453 |
"""get profile/target/device details based on profile name.""" |
| 454 |
if resp.success and resp.json_data.get('value'): |
| 455 |
tlist = resp.json_data.get('value', []) |
| 456 |
for xtype in tlist: |
| 457 |
if (xtype.get(search_key) == identifier_value) or xtype.get('DeploymentTaskId'): |
| 458 |
return xtype |
| 459 |
|
| 460 |
|
| 461 |
def get_profile(rest_obj, module): |
| 462 |
"""Get profile details based on profile ID or profile name from filters.""" |
| 463 |
query_filter = module.params.get("filters", {}) |
| 464 |
profile = None |
| 465 |
srch_key = "ProfileName" |
| 466 |
|
| 467 |
|
| 468 |
profile_ids = query_filter.get("ProfileIds", []) if query_filter is not None else [] |
| 469 |
if profile_ids: |
| 470 |
profile_id = profile_ids[0] |
| 471 |
resp = rest_obj.invoke_request("GET", GET_PORFILE_INST.format(profileId=profile_id)) |
| 472 |
if resp.json_data: |
| 473 |
profile = resp.json_data |
| 474 |
|
| 475 |
|
| 476 |
filter_string = query_filter.get('Filters') if query_filter is not None else None |
| 477 |
if filter_string and 'ProfileName' in filter_string: |
| 478 |
query_param = {FILTER_QUERY_PARAM: filter_string[1:]} |
| 479 |
resp = rest_obj.invoke_request('GET', PROFILE_VIEW, query_param=query_param) |
| 480 |
profile = match_profile(resp=resp, search_key=srch_key) |
| 481 |
|
| 482 |
|
| 483 |
profile_name = module.params.get("name") |
| 484 |
if profile_name: |
| 485 |
query_param = {FILTER_QUERY_PARAM: "ProfileName eq '{0}'".format(profile_name)} |
| 486 |
resp = rest_obj.invoke_request('GET', PROFILE_VIEW, query_param=query_param) |
| 487 |
profile = match_profile(resp=resp, search_key=srch_key, identifier_value=profile_name) |
| 488 |
|
| 489 |
return profile |
| 490 |
|
| 491 |
|
| 492 |
def get_network_iso_payload(module): |
| 493 |
boot_iso_dict = module.params.get("boot_to_network_iso") |
| 494 |
iso_payload = {} |
| 495 |
if boot_iso_dict: |
| 496 |
iso_payload = {"BootToNetwork": False} |
| 497 |
if boot_iso_dict.get("boot_to_network"): |
| 498 |
iso_payload["BootToNetwork"] = True |
| 499 |
share_type = boot_iso_dict.get("share_type") |
| 500 |
iso_payload["ShareType"] = share_type |
| 501 |
share_detail = {} |
| 502 |
sh_ip = boot_iso_dict.get("share_ip") |
| 503 |
share_detail["IpAddress"] = sh_ip |
| 504 |
share_detail["ShareName"] = sh_ip |
| 505 |
share_detail["User"] = boot_iso_dict.get("share_user") |
| 506 |
share_detail["Password"] = boot_iso_dict.get("share_password") |
| 507 |
share_detail["WorkGroup"] = boot_iso_dict.get("workgroup") |
| 508 |
iso_payload["ShareDetail"] = share_detail |
| 509 |
if str(boot_iso_dict.get("iso_path")).lower().endswith('.iso'): |
| 510 |
iso_payload["IsoPath"] = boot_iso_dict.get("iso_path") |
| 511 |
else: |
| 512 |
module.exit_json(msg="ISO path does not have extension '.iso'", failed=True) |
| 513 |
iso_payload["IsoTimeout"] = boot_iso_dict.get("iso_timeout") |
| 514 |
return iso_payload |
| 515 |
|
| 516 |
|
| 517 |
def recurse_subattr_list(subgroup, prefix, attr_detailed, attr_map, adv_list): |
| 518 |
if not isinstance(subgroup, list): |
| 519 |
return |
| 520 |
|
| 521 |
for each_sub in subgroup: |
| 522 |
new_prefix = f"{prefix}{SEPRTR}{each_sub.get('DisplayName')}" |
| 523 |
if each_sub.get("SubAttributeGroups"): |
| 524 |
recurse_subattr_list(each_sub.get("SubAttributeGroups"), |
| 525 |
new_prefix, attr_detailed, |
| 526 |
attr_map, adv_list) |
| 527 |
else: |
| 528 |
for attr in each_sub.get('Attributes'): |
| 529 |
attr['prefix'] = new_prefix |
| 530 |
|
| 531 |
constr = "{0}{1}{2}".format(new_prefix, SEPRTR, attr['DisplayName']) |
| 532 |
if constr in adv_list: |
| 533 |
attr_detailed[constr] = attr['AttributeId'] |
| 534 |
attr_map[attr['AttributeId']] = attr |
| 535 |
|
| 536 |
|
| 537 |
def get_subattr_all(attr_dtls, adv_list): |
| 538 |
attr_detailed = {} |
| 539 |
attr_map = {} |
| 540 |
for each in attr_dtls: |
| 541 |
recurse_subattr_list(each.get('SubAttributeGroups'), each.get('DisplayName'), |
| 542 |
attr_detailed, attr_map, adv_list) |
| 543 |
return attr_detailed, attr_map |
| 544 |
|
| 545 |
|
| 546 |
def normalize_display_names(attributes): |
| 547 |
normalized = [] |
| 548 |
for attr in attributes: |
| 549 |
if attr.get("DisplayName"): |
| 550 |
split_k = str(attr.get("DisplayName")).split(SEPRTR) |
| 551 |
trimmed = map(str.strip, split_k) |
| 552 |
n_k = SEPRTR.join(trimmed) |
| 553 |
normalized.append(n_k) |
| 554 |
return normalized |
| 555 |
|
| 556 |
|
| 557 |
def attributes_check(rest_obj, inp_attr, profile_id): |
| 558 |
diff = 0 |
| 559 |
try: |
| 560 |
resp = rest_obj.invoke_request("GET", PROFILE_ATTRIBUTES.format(profile_id=profile_id)) |
| 561 |
attr_dtls = resp.json_data |
| 562 |
adv_list = normalize_display_names(attributes=inp_attr.get("Attributes", [])) |
| 563 |
attr_detailed, attr_map = get_subattr_all(attr_dtls.get('AttributeGroups'), adv_list) |
| 564 |
payload_attr = inp_attr.get("Attributes", []) |
| 565 |
rem_attrs = [] |
| 566 |
for attr in payload_attr: |
| 567 |
if attr.get("DisplayName"): |
| 568 |
split_k = str(attr.get("DisplayName")).split(SEPRTR) |
| 569 |
trimmed = map(str.strip, split_k) |
| 570 |
n_k = SEPRTR.join(trimmed) |
| 571 |
obj_id = attr_detailed.get(n_k, "") |
| 572 |
attr['Id'] = obj_id |
| 573 |
attr.pop("DisplayName", None) |
| 574 |
else: |
| 575 |
obj_id = attr.get('Id') |
| 576 |
if obj_id: |
| 577 |
ex_val = attr_map.get(obj_id, {}) |
| 578 |
if not ex_val: |
| 579 |
rem_attrs.append(attr) |
| 580 |
continue |
| 581 |
if attr.get('Value') != ex_val.get("Value") or attr.get('IsIgnored') != ex_val.get("IsIgnored"): |
| 582 |
diff = diff + 1 |
| 583 |
for rem in rem_attrs: |
| 584 |
payload_attr.remove(rem) |
| 585 |
except Exception: |
| 586 |
diff = 1 |
| 587 |
return diff |
| 588 |
|
| 589 |
|
| 590 |
def assign_profile(module, rest_obj): |
| 591 |
mparam = module.params |
| 592 |
payload = {} |
| 593 |
prof = prepare_profile_payload(module, rest_obj, mparam, payload) |
| 594 |
|
| 595 |
target = get_target_details(module, rest_obj) |
| 596 |
if isinstance(target, dict): |
| 597 |
payload['TargetId'] = target['Id'] |
| 598 |
_validate_profile_assignment(module=module, payload=payload, prof=prof, target=target) |
| 599 |
|
| 600 |
action = "AssignProfile" |
| 601 |
try: |
| 602 |
resp = rest_obj.invoke_request('POST', PROFILE_ACTION.format(action='GetInvalidTargetsForAssignProfile'), |
| 603 |
data={'Id': prof['Id']}) |
| 604 |
if target['Id'] in list(resp.json_data): |
| 605 |
module.exit_json(msg="The target device is invalid for the given profile.", failed=True) |
| 606 |
except HTTPError: |
| 607 |
resp = None |
| 608 |
ad_opts_list = ['Attributes', 'Options', 'Schedule'] |
| 609 |
else: |
| 610 |
if mparam.get('device_id'): |
| 611 |
module.exit_json(msg=target, failed=True) |
| 612 |
action = "AssignProfileForAutoDeploy" |
| 613 |
payload['Identifier'] = mparam.get('device_service_tag') |
| 614 |
_validate_profile_assignment(module=module, payload=payload, prof=prof) |
| 615 |
|
| 616 |
ad_opts_list = ['Attributes'] |
| 617 |
boot_iso_dict = get_network_iso_payload(module) |
| 618 |
if boot_iso_dict: |
| 619 |
payload["NetworkBootToIso"] = boot_iso_dict |
| 620 |
ad_opts = mparam.get("attributes") |
| 621 |
for opt in ad_opts_list: |
| 622 |
if ad_opts and ad_opts.get(opt): |
| 623 |
attributes_check(rest_obj, ad_opts, prof['Id']) |
| 624 |
payload[opt] = ad_opts.get(opt) |
| 625 |
if module.check_mode: |
| 626 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 627 |
rest_obj.invoke_request('POST', PROFILE_ACTION.format(action=action), data=payload) |
| 628 |
handle_post_assignment(module=module, rest_obj=rest_obj, action=action) |
| 629 |
|
| 630 |
|
| 631 |
def handle_post_assignment(module, rest_obj, action): |
| 632 |
""" |
| 633 |
Handle the post assignment of a profile based on the action type. |
| 634 |
|
| 635 |
Parameters: |
| 636 |
module (AnsibleModule): The Ansible module object. |
| 637 |
rest_obj (REST Object): The REST object used for API calls. |
| 638 |
action (str): The action type, e.g. 'AssignProfile'. |
| 639 |
|
| 640 |
Returns: |
| 641 |
dict: A dictionary containing a success message and a job ID if applicable. |
| 642 |
""" |
| 643 |
|
| 644 |
action_map = { |
| 645 |
"AssignProfile": "assign", |
| 646 |
"UnassignProfiles": "unassign", |
| 647 |
"MigrateProfile": "migrate" |
| 648 |
} |
| 649 |
|
| 650 |
performed_action = action_map.get(action) |
| 651 |
|
| 652 |
if action in ["AssignProfile", "UnassignProfiles", "MigrateProfile"]: |
| 653 |
res_dict = {'msg': f"Successfully applied the {performed_action} operation. No job was triggered.", |
| 654 |
'changed': True} |
| 655 |
try: |
| 656 |
res_prof = get_profile(rest_obj, module) |
| 657 |
time.sleep(5) |
| 658 |
if res_prof.get('DeploymentTaskId'): |
| 659 |
res_dict['job_id'] = res_prof.get('DeploymentTaskId') |
| 660 |
res_dict['msg'] = f"Successfully triggered the job for the {performed_action} operation." |
| 661 |
except HTTPError: |
| 662 |
res_dict['msg'] += " Failed to fetch job details." |
| 663 |
module.exit_json(**res_dict) |
| 664 |
|
| 665 |
|
| 666 |
def prepare_profile_payload(module, rest_obj, mparam, payload): |
| 667 |
""" |
| 668 |
Prepare the profile payload for a given profile name and module. |
| 669 |
|
| 670 |
Parameters: |
| 671 |
module (AnsibleModule): The Ansible module object. |
| 672 |
rest_obj (REST Object): The REST object used for API calls. |
| 673 |
mparam (dict): The module parameters. |
| 674 |
payload (dict): The payload to be prepared. |
| 675 |
|
| 676 |
Returns: |
| 677 |
dict: The prepared profile payload. |
| 678 |
""" |
| 679 |
profile_name = mparam.get('name') |
| 680 |
if profile_name: |
| 681 |
prof = get_profile(rest_obj, module) |
| 682 |
if not prof: |
| 683 |
module.exit_json(msg=PROFILE_NOT_FOUND.format(name=mparam.get('name')), failed=True) |
| 684 |
payload['Id'] = prof['Id'] |
| 685 |
return prof |
| 686 |
|
| 687 |
|
| 688 |
def _validate_profile_assignment(module, payload, prof, target=None): |
| 689 |
""" |
| 690 |
Validates if a profile is assigned to a target based on the profile state. |
| 691 |
|
| 692 |
Parameters: |
| 693 |
module (AnsibleModule): The Ansible module object. |
| 694 |
payload (dict): The payload containing the profile assignment details. |
| 695 |
prof (dict): The profile details. |
| 696 |
target (dict): The target details. Defaults to None. |
| 697 |
|
| 698 |
Returns: |
| 699 |
None |
| 700 |
""" |
| 701 |
if prof['ProfileState'] in [1, 4]: |
| 702 |
target_id = target.get('Id') if prof.get('ProfileState') == 4 else payload.get('Identifier') |
| 703 |
if prof.get('TargetId') == target_id or prof.get('TargetName') == target_id: |
| 704 |
module.exit_json(msg="The profile is assigned to the target {0}.".format(target_id)) |
| 705 |
else: |
| 706 |
module.exit_json( |
| 707 |
msg="The profile is assigned to a different target. Use the migrate command or " |
| 708 |
"unassign the profile and then proceed with assigning the profile to the target.", |
| 709 |
failed=True) |
| 710 |
|
| 711 |
|
| 712 |
def unassign_profile(module, rest_obj): |
| 713 |
mparam = module.params |
| 714 |
prof = {} |
| 715 |
if mparam.get('name'): |
| 716 |
payload = {} |
| 717 |
prof = get_profile(rest_obj, module) |
| 718 |
if prof: |
| 719 |
_validate_profile_deployed(module, prof) |
| 720 |
if prof['DeploymentTaskId']: |
| 721 |
resp = rest_obj.invoke_request('GET', JOB_URI.format(job_id=prof['DeploymentTaskId'])) |
| 722 |
job_dict = resp.json_data |
| 723 |
job_status = job_dict.get('LastRunStatus') |
| 724 |
if job_status.get('Name') == 'Running': |
| 725 |
module.exit_json(msg="Profile deployment task is in progress. Wait for the job to finish.", failed=True) |
| 726 |
payload['ProfileIds'] = [prof['Id']] |
| 727 |
else: |
| 728 |
module.exit_json(msg=PROFILE_NOT_FOUND.format(name=mparam.get('name')), failed=True) |
| 729 |
if mparam.get('filters'): |
| 730 |
payload = mparam.get('filters') |
| 731 |
if module.check_mode: |
| 732 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 733 |
rest_obj.invoke_request('POST', PROFILE_ACTION.format(action="UnassignProfiles"), data=payload) |
| 734 |
handle_post_assignment(module=module, rest_obj=rest_obj, action="UnassignProfiles") |
| 735 |
|
| 736 |
|
| 737 |
def _validate_profile_deployed(module, prof): |
| 738 |
if prof.get('ProfileState') == 0: |
| 739 |
module.exit_json(msg="Profile is in an unassigned state.") |
| 740 |
|
| 741 |
|
| 742 |
def create_profile(module, rest_obj): |
| 743 |
mparam = module.params |
| 744 |
payload = {} |
| 745 |
template = get_template_details(module, rest_obj) |
| 746 |
payload["TemplateId"] = template["Id"] |
| 747 |
payload["NamePrefix"] = mparam.get("name_prefix") |
| 748 |
payload["NumberOfProfilesToCreate"] = mparam["number_of_profiles"] |
| 749 |
if mparam.get("description"): |
| 750 |
payload["Description"] = mparam["description"] |
| 751 |
boot_iso_dict = get_network_iso_payload(module) |
| 752 |
if boot_iso_dict: |
| 753 |
payload["NetworkBootToIso"] = boot_iso_dict |
| 754 |
if module.check_mode: |
| 755 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 756 |
resp = rest_obj.invoke_request('POST', PROFILE_VIEW, data=payload) |
| 757 |
profile_id_list = resp.json_data |
| 758 |
module.exit_json(msg="Successfully created {0} profile(s).".format(len(profile_id_list)), |
| 759 |
changed=True, profile_ids=profile_id_list) |
| 760 |
|
| 761 |
|
| 762 |
def modify_profile(module, rest_obj): |
| 763 |
mparam = module.params |
| 764 |
payload = {} |
| 765 |
prof = get_profile(rest_obj, module) |
| 766 |
if not prof: |
| 767 |
module.exit_json(msg=PROFILE_NOT_FOUND.format(name=mparam.get('name')), failed=True) |
| 768 |
diff = 0 |
| 769 |
diff += _update_name(mparam, payload, prof) |
| 770 |
diff += _update_description(mparam, payload, prof) |
| 771 |
boot_iso_dict = get_network_iso_payload(module) |
| 772 |
rdict = prof.get('NetworkBootToIso') if prof.get('NetworkBootToIso') else {} |
| 773 |
if boot_iso_dict: |
| 774 |
nest_diff = recursive_diff(boot_iso_dict, rdict) |
| 775 |
if nest_diff and nest_diff[0]: |
| 776 |
diff += 1 |
| 777 |
payload["NetworkBootToIso"] = boot_iso_dict |
| 778 |
ad_opts = mparam.get("attributes") |
| 779 |
if ad_opts and ad_opts.get("Attributes"): |
| 780 |
diff = diff + attributes_check(rest_obj, ad_opts, prof['Id']) |
| 781 |
if ad_opts.get("Attributes"): |
| 782 |
payload["Attributes"] = ad_opts.get("Attributes") |
| 783 |
payload['Id'] = prof['Id'] |
| 784 |
if diff: |
| 785 |
if module.check_mode: |
| 786 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 787 |
rest_obj.invoke_request('PUT', PROFILE_VIEW + "({0})".format(payload['Id']), data=payload) |
| 788 |
module.exit_json(msg="Successfully modified the profile.", changed=True) |
| 789 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 790 |
|
| 791 |
|
| 792 |
def _update_name(mparam, payload, prof): |
| 793 |
new_name = mparam.get('new_name') |
| 794 |
payload['Name'] = prof['ProfileName'] |
| 795 |
if new_name and new_name != prof['ProfileName']: |
| 796 |
payload['Name'] = new_name |
| 797 |
return 1 |
| 798 |
return 0 |
| 799 |
|
| 800 |
|
| 801 |
def _update_description(mparam, payload, prof): |
| 802 |
desc = mparam.get('description') |
| 803 |
if desc and desc != prof['ProfileDescription']: |
| 804 |
payload['Description'] = desc |
| 805 |
return 1 |
| 806 |
return 0 |
| 807 |
|
| 808 |
|
| 809 |
def delete_profile(module, rest_obj): |
| 810 |
mparam = module.params |
| 811 |
if mparam.get('name'): |
| 812 |
prof = get_profile(rest_obj, module) |
| 813 |
if prof: |
| 814 |
if prof['ProfileState'] > 0: |
| 815 |
module.exit_json(msg="Profile has to be in an unassigned state for it to be deleted.", failed=True) |
| 816 |
if module.check_mode: |
| 817 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 818 |
rest_obj.invoke_request('DELETE', PROFILE_VIEW + "({0})".format(prof['Id'])) |
| 819 |
module.exit_json(msg="Successfully deleted the profile.", changed=True) |
| 820 |
else: |
| 821 |
module.exit_json(msg=PROFILE_NOT_FOUND.format(name=mparam.get('name'))) |
| 822 |
if mparam.get('filters'): |
| 823 |
payload = mparam.get('filters') |
| 824 |
if module.check_mode: |
| 825 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 826 |
rest_obj.invoke_request('POST', PROFILE_ACTION.format(action='Delete'), data=payload) |
| 827 |
module.exit_json(msg="Successfully completed the delete operation.", changed=True) |
| 828 |
|
| 829 |
|
| 830 |
def migrate_profile(module, rest_obj): |
| 831 |
mparam = module.params |
| 832 |
payload = {} |
| 833 |
payload['ForceMigrate'] = mparam.get('force') |
| 834 |
target = get_target_details(module, rest_obj) |
| 835 |
if not isinstance(target, dict): |
| 836 |
module.exit_json(msg=target, failed=True) |
| 837 |
payload['TargetId'] = target['Id'] |
| 838 |
prof = get_profile(rest_obj, module) |
| 839 |
if prof: |
| 840 |
if target['Id'] == prof['TargetId']: |
| 841 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 842 |
try: |
| 843 |
resp = rest_obj.invoke_request('POST', PROFILE_ACTION.format(action='GetInvalidTargetsForAssignProfile'), |
| 844 |
data={'Id': prof['Id']}) |
| 845 |
if target['Id'] in list(resp.json_data): |
| 846 |
module.exit_json(msg="The target device is invalid for the given profile.", failed=True) |
| 847 |
except HTTPError: |
| 848 |
resp = None |
| 849 |
if prof['ProfileState'] == 4: |
| 850 |
payload['ProfileId'] = prof['Id'] |
| 851 |
if module.check_mode: |
| 852 |
module.exit_json(msg=CHANGES_MSG, changed=True) |
| 853 |
rest_obj.invoke_request('POST', PROFILE_ACTION.format(action='MigrateProfile'), data=payload) |
| 854 |
handle_post_assignment(module=module, rest_obj=rest_obj, action="MigrateProfile") |
| 855 |
else: |
| 856 |
module.exit_json(msg="Profile needs to be in a deployed state for a migrate operation.", failed=True) |
| 857 |
else: |
| 858 |
module.exit_json(msg=PROFILE_NOT_FOUND.format(name=mparam.get('name')), failed=True) |
| 859 |
|
| 860 |
|
| 861 |
def profile_operation(module, rest_obj): |
| 862 |
command = module.params.get("command") |
| 863 |
if command == "create": |
| 864 |
create_profile(module, rest_obj) |
| 865 |
if command == "modify": |
| 866 |
modify_profile(module, rest_obj) |
| 867 |
if command == "delete": |
| 868 |
delete_profile(module, rest_obj) |
| 869 |
if command == "assign": |
| 870 |
assign_profile(module, rest_obj) |
| 871 |
if command == "unassign": |
| 872 |
unassign_profile(module, rest_obj) |
| 873 |
if command == "migrate": |
| 874 |
migrate_profile(module, rest_obj) |
| 875 |
|
| 876 |
|
| 877 |
def main(): |
| 878 |
network_iso_spec = {"boot_to_network": {"required": True, "type": 'bool'}, |
| 879 |
"share_type": {"choices": ['NFS', 'CIFS']}, |
| 880 |
"share_ip": {"type": 'str'}, |
| 881 |
"share_user": {"type": 'str'}, |
| 882 |
"share_password": {"type": 'str', "no_log": True}, |
| 883 |
"workgroup": {"type": 'str'}, |
| 884 |
"iso_path": {"type": 'str'}, |
| 885 |
"iso_timeout": {"type": 'int', "default": 4, |
| 886 |
"choices": [1, 2, 4, 8, 16]}} |
| 887 |
assign_spec = {"Attributes": {"type": 'list', "elements": 'dict'}, |
| 888 |
"Options": {"type": 'dict'}, |
| 889 |
"Schedule": {"type": 'dict'}} |
| 890 |
specs = { |
| 891 |
"command": {"default": "create", |
| 892 |
"choices": ['create', 'modify', 'delete', 'assign', 'unassign', 'migrate']}, |
| 893 |
"name_prefix": {"default": "Profile", "type": 'str'}, |
| 894 |
"name": {"type": 'str'}, |
| 895 |
"new_name": {"type": 'str'}, |
| 896 |
"number_of_profiles": {"default": 1, "type": 'int'}, |
| 897 |
"template_name": {"type": 'str'}, |
| 898 |
"template_id": {"type": "int"}, |
| 899 |
"device_id": {"type": 'int'}, |
| 900 |
"device_service_tag": {"type": 'str'}, |
| 901 |
"description": {"type": 'str'}, |
| 902 |
"boot_to_network_iso": {"type": 'dict', "options": network_iso_spec, |
| 903 |
"required_if": [ |
| 904 |
['boot_to_network', True, ['share_type', 'share_ip', 'iso_path']], |
| 905 |
['share_type', 'CIFS', ['share_user', 'share_password']] |
| 906 |
]}, |
| 907 |
"filters": {"type": 'dict'}, |
| 908 |
"attributes": {"type": 'dict', "options": assign_spec}, |
| 909 |
"force": {"default": False, "type": 'bool'} |
| 910 |
} |
| 911 |
|
| 912 |
module = OmeAnsibleModule( |
| 913 |
argument_spec=specs, |
| 914 |
required_if=[ |
| 915 |
['command', 'create', ['template_name', 'template_id'], True], |
| 916 |
['command', 'modify', ['name']], |
| 917 |
['command', 'modify', ['new_name', 'description', 'attributes', 'boot_to_network_iso'], True], |
| 918 |
['command', 'assign', ['name']], |
| 919 |
['command', 'assign', ['device_id', 'device_service_tag'], True], |
| 920 |
['command', 'unassign', ['name', "filters"], True], |
| 921 |
['command', 'delete', ['name', "filters"], True], |
| 922 |
['command', 'migrate', ['name']], |
| 923 |
['command', 'migrate', ['device_id', 'device_service_tag'], True], |
| 924 |
], |
| 925 |
mutually_exclusive=[ |
| 926 |
['name', 'name_prefix'], |
| 927 |
['name', 'number_of_profiles'], |
| 928 |
['name', 'filters'], |
| 929 |
['device_id', 'device_service_tag'], |
| 930 |
['template_name', 'template_id']], |
| 931 |
supports_check_mode=True) |
| 932 |
try: |
| 933 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 934 |
profile_operation(module, rest_obj) |
| 935 |
except HTTPError as err: |
| 936 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 937 |
except URLError as err: |
| 938 |
module.exit_json(msg=str(err), unreachable=True, failed=True) |
| 939 |
except (IOError, ValueError, TypeError, ConnectionError, SSLValidationError, OSError) as err: |
| 940 |
module.exit_json(msg=str(err), failed=True) |
| 941 |
|
| 942 |
|
| 943 |
if __name__ == '__main__': |
| 944 |
main() |