| 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_ldap_policy |
| 16 |
short_description: Manage LDAP Policies for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete LDAP Policies on Cisco Intersight. |
| 19 |
- Manage LDAP groups and providers associated with LDAP policies. |
| 20 |
- LDAP policies enable authentication of Cisco IMC users using an LDAP server. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/iam/LdapPolicy/get/). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
- Use 'default' for the default organization. |
| 36 |
type: str |
| 37 |
default: default |
| 38 |
name: |
| 39 |
description: |
| 40 |
- The name assigned to the LDAP Policy. |
| 41 |
- Must be unique within the organization. |
| 42 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
description: |
| 46 |
description: |
| 47 |
- The user-defined description for the LDAP Policy. |
| 48 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 49 |
type: str |
| 50 |
aliases: [descr] |
| 51 |
tags: |
| 52 |
description: |
| 53 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
enable_ldap: |
| 57 |
description: |
| 58 |
- If enabled, LDAP server performs authentication. |
| 59 |
type: bool |
| 60 |
default: true |
| 61 |
base_dn: |
| 62 |
description: |
| 63 |
- Base Distinguished Name (DN). |
| 64 |
- Starting point from where server will search for users and groups. |
| 65 |
- Required when state is present. |
| 66 |
type: str |
| 67 |
domain: |
| 68 |
description: |
| 69 |
- The IPv4 domain that all users must be in. |
| 70 |
- Required when state is present. |
| 71 |
type: str |
| 72 |
timeout: |
| 73 |
description: |
| 74 |
- LDAP authentication timeout duration, in seconds. |
| 75 |
- Valid range is 0-180. |
| 76 |
type: int |
| 77 |
default: 0 |
| 78 |
enable_encryption: |
| 79 |
description: |
| 80 |
- If enabled, the endpoint encrypts all information it sends to the LDAP server. |
| 81 |
type: bool |
| 82 |
default: false |
| 83 |
bind_method: |
| 84 |
description: |
| 85 |
- Authentication method to access LDAP servers. |
| 86 |
- C(logincredentials) uses the user credentials entered at login. |
| 87 |
- C(anonymous) uses no credentials to access the LDAP server. |
| 88 |
- C(configuredcredentials) uses a specific set of credentials configured for the LDAP server. |
| 89 |
type: str |
| 90 |
choices: [logincredentials, anonymous, configuredcredentials] |
| 91 |
default: logincredentials |
| 92 |
bind_dn: |
| 93 |
description: |
| 94 |
- Distinguished Name (DN) of the user, that is used to authenticate against LDAP servers. |
| 95 |
- Required when bind_method is configuredcredentials. |
| 96 |
type: str |
| 97 |
password: |
| 98 |
description: |
| 99 |
- The password of the user for initial bind process. |
| 100 |
- Can have any character except spaces, tabs, line breaks. |
| 101 |
- Cannot be more than 254 characters. |
| 102 |
- Required when bind_method is configuredcredentials. |
| 103 |
type: str |
| 104 |
filter: |
| 105 |
description: |
| 106 |
- Criteria to identify entries in search requests. |
| 107 |
- Required when state is present. |
| 108 |
type: str |
| 109 |
group_attribute: |
| 110 |
description: |
| 111 |
- Groups to which an LDAP entry belongs. |
| 112 |
- Required when state is present. |
| 113 |
type: str |
| 114 |
attribute: |
| 115 |
description: |
| 116 |
- Role and locale information of the user. |
| 117 |
- Required when state is present. |
| 118 |
type: str |
| 119 |
group_authorization: |
| 120 |
description: |
| 121 |
- If enabled, user authorization is also done at the group level for LDAP users not in the local user database. |
| 122 |
type: bool |
| 123 |
default: false |
| 124 |
nested_group_search: |
| 125 |
description: |
| 126 |
- If enabled, an extended search walks the chain of ancestry all the way to the root. |
| 127 |
- Returns all the groups and subgroups, each of those groups belong to recursively. |
| 128 |
type: bool |
| 129 |
default: false |
| 130 |
nested_group_search_depth: |
| 131 |
description: |
| 132 |
- Search depth to look for a nested LDAP group in an LDAP group map. |
| 133 |
- Valid range is 1-128. |
| 134 |
- Only applicable when nested_group_search is true. |
| 135 |
type: int |
| 136 |
default: 128 |
| 137 |
enable_dns: |
| 138 |
description: |
| 139 |
- Enables DNS to access LDAP servers. |
| 140 |
- When enabled, LDAP providers cannot be specified (DNS discovery is used instead). |
| 141 |
- When disabled, at least one LDAP provider must be configured. |
| 142 |
type: bool |
| 143 |
default: false |
| 144 |
dns_source: |
| 145 |
description: |
| 146 |
- Source of the domain name used for the DNS SRV request. |
| 147 |
- C(extracted) extracts the domain name from the login ID entered by the user. |
| 148 |
- C(configured) uses the configured search domain. |
| 149 |
- C(configuredextracted) uses configured search domain first, then extracted. |
| 150 |
- Only applicable when enable_dns is true. |
| 151 |
type: str |
| 152 |
choices: [extracted, configured, configuredextracted] |
| 153 |
default: extracted |
| 154 |
search_domain: |
| 155 |
description: |
| 156 |
- Domain name that acts as a source for a DNS query. |
| 157 |
- Required when dns_source is configured or configuredextracted. |
| 158 |
type: str |
| 159 |
search_forest: |
| 160 |
description: |
| 161 |
- Forest name that acts as a source for a DNS query. |
| 162 |
- Required when dns_source is configured or configuredextracted. |
| 163 |
type: str |
| 164 |
user_search_precedence: |
| 165 |
description: |
| 166 |
- Search precedence between local user database and LDAP user database. |
| 167 |
- C(localuserdb) searches local user database first. |
| 168 |
- C(ldapuserdb) searches LDAP user database first. |
| 169 |
type: str |
| 170 |
choices: [localuserdb, ldapuserdb] |
| 171 |
default: localuserdb |
| 172 |
ldap_groups: |
| 173 |
description: |
| 174 |
- List of LDAP groups to be created and attached to the LDAP policy. |
| 175 |
- Each group defines the mapping between LDAP server groups and Intersight roles. |
| 176 |
type: list |
| 177 |
elements: dict |
| 178 |
suboptions: |
| 179 |
name: |
| 180 |
description: |
| 181 |
- LDAP Group name in the LDAP server database. |
| 182 |
type: str |
| 183 |
required: true |
| 184 |
state: |
| 185 |
description: |
| 186 |
- Whether to create/update or delete the LDAP group. |
| 187 |
type: str |
| 188 |
choices: [present, absent] |
| 189 |
default: present |
| 190 |
group_dn: |
| 191 |
description: |
| 192 |
- LDAP Group DN in the LDAP server database. |
| 193 |
- Required when state is present. |
| 194 |
type: str |
| 195 |
domain: |
| 196 |
description: |
| 197 |
- LDAP server domain the Group resides in. |
| 198 |
type: str |
| 199 |
role: |
| 200 |
description: |
| 201 |
- Role assigned to all users in this LDAP server group. |
| 202 |
- C(admin) provides full administrative access. |
| 203 |
- C(readonly) provides read-only access. |
| 204 |
- C(user) provides standard user access. |
| 205 |
- Only 'admin' role is supported in domain. |
| 206 |
- Required when state is present. |
| 207 |
type: str |
| 208 |
choices: [admin, readonly, user] |
| 209 |
default: admin |
| 210 |
ldap_providers: |
| 211 |
description: |
| 212 |
- List of LDAP providers (servers) to be created and attached to the LDAP policy. |
| 213 |
- Providers define the LDAP servers to connect to. |
| 214 |
- Cannot be specified when enable_dns is true (DNS discovery is used instead). |
| 215 |
- Required when enable_dns is false (at least one provider must be specified). |
| 216 |
type: list |
| 217 |
elements: dict |
| 218 |
suboptions: |
| 219 |
server: |
| 220 |
description: |
| 221 |
- IP address or hostname of the LDAP server. |
| 222 |
- Required when state is present. |
| 223 |
type: str |
| 224 |
required: true |
| 225 |
state: |
| 226 |
description: |
| 227 |
- Whether to create/update or delete the LDAP provider. |
| 228 |
type: str |
| 229 |
choices: [present, absent] |
| 230 |
default: present |
| 231 |
port: |
| 232 |
description: |
| 233 |
- Port number on which the LDAP server is listening. |
| 234 |
type: int |
| 235 |
default: 389 |
| 236 |
vendor: |
| 237 |
description: |
| 238 |
- Type of LDAP server. |
| 239 |
- C(openldap) for OpenLDAP servers. |
| 240 |
- C(msad) for Microsoft Active Directory. |
| 241 |
type: str |
| 242 |
choices: [openldap, msad] |
| 243 |
default: openldap |
| 244 |
author: |
| 245 |
- Ron Gershburg (@rgershbu) |
| 246 |
''' |
| 247 |
|
| 248 |
EXAMPLES = r''' |
| 249 |
- name: Create LDAP Policy with DNS enabled |
| 250 |
cisco.intersight.intersight_ldap_policy: |
| 251 |
api_private_key: "{{ api_private_key }}" |
| 252 |
api_key_id: "{{ api_key_id }}" |
| 253 |
organization: "default" |
| 254 |
name: "ldap-dns-policy" |
| 255 |
description: "LDAP policy using DNS for server discovery" |
| 256 |
enable_ldap: true |
| 257 |
base_dn: "dc=example,dc=com" |
| 258 |
domain: "example.com" |
| 259 |
timeout: 30 |
| 260 |
enable_encryption: true |
| 261 |
bind_method: "logincredentials" |
| 262 |
filter: "sAMAccountName" |
| 263 |
group_attribute: "memberOf" |
| 264 |
attribute: "CiscoAvPair" |
| 265 |
group_authorization: true |
| 266 |
nested_group_search: true |
| 267 |
nested_group_search_depth: 64 |
| 268 |
enable_dns: true |
| 269 |
dns_source: "configured" |
| 270 |
search_domain: "example.com" |
| 271 |
search_forest: "example.com" |
| 272 |
user_search_precedence: "localuserdb" |
| 273 |
ldap_groups: |
| 274 |
- name: "admin-group" |
| 275 |
group_dn: "cn=admins,ou=groups,dc=example,dc=com" |
| 276 |
domain: "example.com" |
| 277 |
role: "admin" |
| 278 |
- name: "readonly-group" |
| 279 |
group_dn: "cn=readers,ou=groups,dc=example,dc=com" |
| 280 |
role: "readonly" |
| 281 |
tags: |
| 282 |
- Key: "Environment" |
| 283 |
Value: "Production" |
| 284 |
state: present |
| 285 |
|
| 286 |
- name: Create LDAP Policy with providers |
| 287 |
cisco.intersight.intersight_ldap_policy: |
| 288 |
api_private_key: "{{ api_private_key }}" |
| 289 |
api_key_id: "{{ api_key_id }}" |
| 290 |
organization: "default" |
| 291 |
name: "ldap-provider-policy" |
| 292 |
description: "LDAP policy with specific servers" |
| 293 |
enable_ldap: true |
| 294 |
base_dn: "company" |
| 295 |
domain: "company.local" |
| 296 |
timeout: 0 |
| 297 |
enable_encryption: false |
| 298 |
bind_method: "anonymous" |
| 299 |
filter: "sAMAccountName" |
| 300 |
group_attribute: "memberOf" |
| 301 |
attribute: "CiscoAvPair" |
| 302 |
group_authorization: false |
| 303 |
nested_group_search: false |
| 304 |
enable_dns: false |
| 305 |
user_search_precedence: "localuserdb" |
| 306 |
ldap_providers: |
| 307 |
- server: "10.10.10.10" |
| 308 |
port: 389 |
| 309 |
vendor: "openldap" |
| 310 |
- server: "10.10.10.11" |
| 311 |
port: 389 |
| 312 |
vendor: "openldap" |
| 313 |
ldap_groups: |
| 314 |
- name: "users-group" |
| 315 |
group_dn: "company" |
| 316 |
role: "user" |
| 317 |
state: present |
| 318 |
|
| 319 |
- name: Create LDAP Policy with configured credentials |
| 320 |
cisco.intersight.intersight_ldap_policy: |
| 321 |
api_private_key: "{{ api_private_key }}" |
| 322 |
api_key_id: "{{ api_key_id }}" |
| 323 |
organization: "default" |
| 324 |
name: "ldap-configured-creds" |
| 325 |
description: "LDAP policy with configured bind credentials" |
| 326 |
enable_ldap: true |
| 327 |
base_dn: "dc=corp,dc=net" |
| 328 |
domain: "corp.net" |
| 329 |
timeout: 60 |
| 330 |
enable_encryption: true |
| 331 |
bind_method: "configuredcredentials" |
| 332 |
bind_dn: "cn=admin,dc=corp,dc=net" |
| 333 |
password: "SecurePassword123" |
| 334 |
filter: "uid" |
| 335 |
group_attribute: "gidNumber" |
| 336 |
attribute: "description" |
| 337 |
group_authorization: true |
| 338 |
nested_group_search: true |
| 339 |
nested_group_search_depth: 128 |
| 340 |
enable_dns: false |
| 341 |
user_search_precedence: "ldapuserdb" |
| 342 |
ldap_providers: |
| 343 |
- server: "ldap.corp.net" |
| 344 |
port: 636 |
| 345 |
vendor: "msad" |
| 346 |
ldap_groups: |
| 347 |
- name: "administrators" |
| 348 |
group_dn: "cn=administrators,ou=groups,dc=corp,dc=net" |
| 349 |
domain: "corp.net" |
| 350 |
role: "admin" |
| 351 |
state: present |
| 352 |
|
| 353 |
- name: Update LDAP Policy - manage group states |
| 354 |
cisco.intersight.intersight_ldap_policy: |
| 355 |
api_private_key: "{{ api_private_key }}" |
| 356 |
api_key_id: "{{ api_key_id }}" |
| 357 |
organization: "default" |
| 358 |
name: "ldap-dns-policy" |
| 359 |
description: "Updated LDAP policy" |
| 360 |
enable_ldap: true |
| 361 |
base_dn: "dc=example,dc=com" |
| 362 |
domain: "example.com" |
| 363 |
filter: "sAMAccountName" |
| 364 |
group_attribute: "memberOf" |
| 365 |
attribute: "CiscoAvPair" |
| 366 |
enable_dns: true |
| 367 |
ldap_groups: |
| 368 |
- name: "admin-group" |
| 369 |
group_dn: "cn=admins,ou=groups,dc=example,dc=com" |
| 370 |
role: "admin" |
| 371 |
state: present |
| 372 |
- name: "old-group" |
| 373 |
state: absent |
| 374 |
- name: "new-group" |
| 375 |
group_dn: "cn=new,ou=groups,dc=example,dc=com" |
| 376 |
role: "user" |
| 377 |
state: present |
| 378 |
state: present |
| 379 |
|
| 380 |
- name: Delete LDAP Policy |
| 381 |
cisco.intersight.intersight_ldap_policy: |
| 382 |
api_private_key: "{{ api_private_key }}" |
| 383 |
api_key_id: "{{ api_key_id }}" |
| 384 |
name: "old-ldap-policy" |
| 385 |
state: absent |
| 386 |
''' |
| 387 |
|
| 388 |
RETURN = r''' |
| 389 |
api_response: |
| 390 |
description: The API response output returned by the specified resource. |
| 391 |
returned: always |
| 392 |
type: dict |
| 393 |
sample: |
| 394 |
"api_response": { |
| 395 |
"Name": "ldap-dns-policy", |
| 396 |
"ObjectType": "iam.LdapPolicy", |
| 397 |
"Enabled": true, |
| 398 |
"BaseProperties": { |
| 399 |
"BaseDn": "dc=example,dc=com", |
| 400 |
"Domain": "example.com", |
| 401 |
"Timeout": 30, |
| 402 |
"EnableEncryption": true, |
| 403 |
"BindMethod": "LoginCredentials", |
| 404 |
"Filter": "sAMAccountName", |
| 405 |
"GroupAttribute": "memberOf", |
| 406 |
"Attribute": "CiscoAvPair", |
| 407 |
"EnableGroupAuthorization": true, |
| 408 |
"EnableNestedGroupSearch": true, |
| 409 |
"NestedGroupSearchDepth": 64 |
| 410 |
}, |
| 411 |
"EnableDns": true, |
| 412 |
"DnsParameters": { |
| 413 |
"Source": "Configured", |
| 414 |
"SearchDomain": "example.com", |
| 415 |
"SearchForest": "example.com" |
| 416 |
}, |
| 417 |
"UserSearchPrecedence": "LocalUserDb", |
| 418 |
"Groups": [ |
| 419 |
{ |
| 420 |
"Name": "admin-group", |
| 421 |
"GroupDn": "cn=admins,ou=groups,dc=example,dc=com", |
| 422 |
"Domain": "example.com" |
| 423 |
} |
| 424 |
], |
| 425 |
"Providers": [] |
| 426 |
} |
| 427 |
''' |
| 428 |
|
| 429 |
|
| 430 |
from ansible.module_utils.basic import AnsibleModule |
| 431 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 432 |
|
| 433 |
|
| 434 |
def validate_input(module): |
| 435 |
""" |
| 436 |
Validate module input parameters. |
| 437 |
""" |
| 438 |
if module.params['state'] == 'present': |
| 439 |
required_fields = ['base_dn', 'domain', 'filter', 'group_attribute', 'attribute'] |
| 440 |
for field in required_fields: |
| 441 |
if not module.params.get(field): |
| 442 |
module.fail_json(msg=f"{field} is required when state is 'present'") |
| 443 |
if module.params['bind_method'] == 'configuredcredentials': |
| 444 |
if not module.params.get('bind_dn'): |
| 445 |
module.fail_json(msg="bind_dn is required when bind_method is 'configuredcredentials'") |
| 446 |
if not module.params.get('password'): |
| 447 |
module.fail_json(msg="password is required when bind_method is 'configuredcredentials'") |
| 448 |
if module.params['enable_dns']: |
| 449 |
if module.params['dns_source'] in ['configured', 'configuredextracted']: |
| 450 |
if not module.params.get('search_domain'): |
| 451 |
module.fail_json(msg="search_domain is required when dns_source is 'configured' or 'configuredextracted'") |
| 452 |
if not module.params.get('search_forest'): |
| 453 |
module.fail_json(msg="search_forest is required when dns_source is 'configured' or 'configuredextracted'") |
| 454 |
if module.params['timeout'] < 0 or module.params['timeout'] > 180: |
| 455 |
module.fail_json(msg="timeout must be between 0 and 180 seconds") |
| 456 |
if module.params['nested_group_search']: |
| 457 |
depth = module.params['nested_group_search_depth'] |
| 458 |
if depth < 1 or depth > 128: |
| 459 |
module.fail_json(msg="nested_group_search_depth must be between 1 and 128") |
| 460 |
|
| 461 |
ldap_groups = module.params.get('ldap_groups') or [] |
| 462 |
for group in ldap_groups: |
| 463 |
if group.get('state', 'present') == 'present': |
| 464 |
if not group.get('group_dn'): |
| 465 |
module.fail_json(msg=f"group_dn is required for LDAP group '{group['name']}' when state is 'present'") |
| 466 |
if not group.get('role'): |
| 467 |
module.fail_json(msg=f"role is required for LDAP group '{group['name']}' when state is 'present'") |
| 468 |
if module.params['enable_dns']: |
| 469 |
if module.params.get('ldap_providers'): |
| 470 |
module.fail_json(msg="ldap_providers cannot be specified when enable_dns is true. DNS discovery is used instead of explicit providers.") |
| 471 |
else: |
| 472 |
ldap_providers = module.params.get('ldap_providers') or [] |
| 473 |
if not ldap_providers: |
| 474 |
module.fail_json(msg="ldap_providers is required when enable_dns is false. Specify at least one LDAP server.") |
| 475 |
for provider in ldap_providers: |
| 476 |
if provider.get('state', 'present') == 'present': |
| 477 |
if not provider.get('server'): |
| 478 |
module.fail_json(msg="server is required for LDAP provider when state is 'present'") |
| 479 |
|
| 480 |
|
| 481 |
def get_endpoint_role_moid(intersight, role_name): |
| 482 |
""" |
| 483 |
Get EndPointRole MOID by role name. |
| 484 |
""" |
| 485 |
intersight.get_resource( |
| 486 |
resource_path='/iam/EndPointRoles', |
| 487 |
query_params={ |
| 488 |
'$filter': f"Name eq '{role_name}' and Type eq 'IMC'", |
| 489 |
'$select': 'Moid' |
| 490 |
} |
| 491 |
) |
| 492 |
return intersight.result['api_response'].get('Moid') |
| 493 |
|
| 494 |
|
| 495 |
def build_ldap_group_api_body(intersight, module, group_config, ldap_policy_moid): |
| 496 |
""" |
| 497 |
Build LDAP group API body. |
| 498 |
""" |
| 499 |
role_name = group_config['role'] |
| 500 |
endpoint_role_moid = get_endpoint_role_moid(intersight, role_name) |
| 501 |
if not endpoint_role_moid: |
| 502 |
module.fail_json(msg=f"EndPointRole '{role_name}' not found") |
| 503 |
group_api_body = { |
| 504 |
'Name': group_config['name'], |
| 505 |
'GroupDn': group_config['group_dn'], |
| 506 |
'EndPointRole': [endpoint_role_moid], |
| 507 |
'LdapPolicy': ldap_policy_moid |
| 508 |
} |
| 509 |
if group_config.get('domain'): |
| 510 |
group_api_body['Domain'] = group_config['domain'] |
| 511 |
return group_api_body |
| 512 |
|
| 513 |
|
| 514 |
def build_ldap_provider_api_body(provider_config, ldap_policy_moid): |
| 515 |
""" |
| 516 |
Build LDAP provider API body. |
| 517 |
""" |
| 518 |
vendor_map = { |
| 519 |
'openldap': 'OpenLDAP', |
| 520 |
'msad': 'MSAD' |
| 521 |
} |
| 522 |
provider_api_body = { |
| 523 |
'Server': provider_config['server'], |
| 524 |
'Port': provider_config.get('port', 389), |
| 525 |
'Vendor': vendor_map[provider_config.get('vendor', 'openldap')], |
| 526 |
'LdapPolicy': ldap_policy_moid |
| 527 |
} |
| 528 |
return provider_api_body |
| 529 |
|
| 530 |
|
| 531 |
def main(): |
| 532 |
ldap_group_options = dict( |
| 533 |
name=dict(type='str', required=True), |
| 534 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 535 |
group_dn=dict(type='str'), |
| 536 |
domain=dict(type='str'), |
| 537 |
role=dict(type='str', choices=['admin', 'readonly', 'user'], default='admin') |
| 538 |
) |
| 539 |
ldap_provider_options = dict( |
| 540 |
server=dict(type='str', required=True), |
| 541 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 542 |
port=dict(type='int', default=389), |
| 543 |
vendor=dict(type='str', choices=['openldap', 'msad'], default='openldap') |
| 544 |
) |
| 545 |
argument_spec = intersight_argument_spec.copy() |
| 546 |
argument_spec.update( |
| 547 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 548 |
organization=dict(type='str', default='default'), |
| 549 |
name=dict(type='str', required=True), |
| 550 |
description=dict(type='str', aliases=['descr']), |
| 551 |
tags=dict(type='list', elements='dict'), |
| 552 |
enable_ldap=dict(type='bool', default=True), |
| 553 |
base_dn=dict(type='str'), |
| 554 |
domain=dict(type='str'), |
| 555 |
timeout=dict(type='int', default=0), |
| 556 |
enable_encryption=dict(type='bool', default=False), |
| 557 |
bind_method=dict(type='str', choices=['logincredentials', 'anonymous', 'configuredcredentials'], default='logincredentials'), |
| 558 |
bind_dn=dict(type='str'), |
| 559 |
password=dict(type='str', no_log=True), |
| 560 |
filter=dict(type='str'), |
| 561 |
group_attribute=dict(type='str'), |
| 562 |
attribute=dict(type='str'), |
| 563 |
group_authorization=dict(type='bool', default=False), |
| 564 |
nested_group_search=dict(type='bool', default=False), |
| 565 |
nested_group_search_depth=dict(type='int', default=128), |
| 566 |
enable_dns=dict(type='bool', default=False), |
| 567 |
dns_source=dict(type='str', choices=['extracted', 'configured', 'configuredextracted'], default='extracted'), |
| 568 |
search_domain=dict(type='str'), |
| 569 |
search_forest=dict(type='str'), |
| 570 |
user_search_precedence=dict(type='str', choices=['localuserdb', 'ldapuserdb'], default='localuserdb'), |
| 571 |
ldap_groups=dict(type='list', elements='dict', options=ldap_group_options), |
| 572 |
ldap_providers=dict(type='list', elements='dict', options=ldap_provider_options) |
| 573 |
) |
| 574 |
|
| 575 |
module = AnsibleModule( |
| 576 |
argument_spec, |
| 577 |
supports_check_mode=True, |
| 578 |
) |
| 579 |
|
| 580 |
if module.params['state'] == 'present': |
| 581 |
validate_input(module) |
| 582 |
|
| 583 |
intersight = IntersightModule(module) |
| 584 |
intersight.result['api_response'] = {} |
| 585 |
intersight.result['trace_id'] = '' |
| 586 |
resource_path = '/iam/LdapPolicies' |
| 587 |
|
| 588 |
intersight.api_body = { |
| 589 |
'Organization': { |
| 590 |
'Name': intersight.module.params['organization'], |
| 591 |
}, |
| 592 |
'Name': intersight.module.params['name'] |
| 593 |
} |
| 594 |
|
| 595 |
# Start propogating the api body for the main resource (Ldap Policy) |
| 596 |
if intersight.module.params['state'] == 'present': |
| 597 |
intersight.set_tags_and_description() |
| 598 |
intersight.api_body['Enabled'] = intersight.module.params['enable_ldap'] |
| 599 |
|
| 600 |
bind_method_map = { |
| 601 |
'logincredentials': 'LoginCredentials', |
| 602 |
'anonymous': 'Anonymous', |
| 603 |
'configuredcredentials': 'ConfiguredCredentials' |
| 604 |
} |
| 605 |
|
| 606 |
base_properties = { |
| 607 |
'BaseDn': intersight.module.params['base_dn'], |
| 608 |
'Domain': intersight.module.params['domain'], |
| 609 |
'Timeout': intersight.module.params['timeout'], |
| 610 |
'EnableEncryption': intersight.module.params['enable_encryption'], |
| 611 |
'BindMethod': bind_method_map[intersight.module.params['bind_method']], |
| 612 |
'Filter': intersight.module.params['filter'], |
| 613 |
'GroupAttribute': intersight.module.params['group_attribute'], |
| 614 |
'Attribute': intersight.module.params['attribute'], |
| 615 |
'EnableGroupAuthorization': intersight.module.params['group_authorization'], |
| 616 |
'EnableNestedGroupSearch': intersight.module.params['nested_group_search'] |
| 617 |
} |
| 618 |
|
| 619 |
if intersight.module.params['bind_method'] == 'configuredcredentials': |
| 620 |
base_properties['BindDn'] = intersight.module.params['bind_dn'] |
| 621 |
base_properties['Password'] = intersight.module.params['password'] |
| 622 |
|
| 623 |
if intersight.module.params['nested_group_search']: |
| 624 |
base_properties['NestedGroupSearchDepth'] = intersight.module.params['nested_group_search_depth'] |
| 625 |
|
| 626 |
intersight.api_body['BaseProperties'] = base_properties |
| 627 |
intersight.api_body['EnableDns'] = intersight.module.params['enable_dns'] |
| 628 |
if intersight.module.params['enable_dns']: |
| 629 |
dns_source_map = { |
| 630 |
'extracted': 'Extracted', |
| 631 |
'configured': 'Configured', |
| 632 |
'configuredextracted': 'ConfiguredExtracted' |
| 633 |
} |
| 634 |
dns_parameters = { |
| 635 |
'Source': dns_source_map[intersight.module.params['dns_source']] |
| 636 |
} |
| 637 |
if intersight.module.params['dns_source'] in ['configured', 'configuredextracted']: |
| 638 |
dns_parameters['SearchDomain'] = intersight.module.params['search_domain'] |
| 639 |
dns_parameters['SearchForest'] = intersight.module.params['search_forest'] |
| 640 |
|
| 641 |
intersight.api_body['DnsParameters'] = dns_parameters |
| 642 |
|
| 643 |
user_search_precedence_map = { |
| 644 |
'localuserdb': 'LocalUserDb', |
| 645 |
'ldapuserdb': 'LDAPUserDb' |
| 646 |
} |
| 647 |
intersight.api_body['UserSearchPrecedence'] = user_search_precedence_map[intersight.module.params['user_search_precedence']] |
| 648 |
|
| 649 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 650 |
|
| 651 |
# Save the LDAP policy response |
| 652 |
ldap_policy_response = intersight.result['api_response'] |
| 653 |
ldap_policy_moid = None |
| 654 |
|
| 655 |
if intersight.module.params['state'] == 'present' and ldap_policy_response: |
| 656 |
ldap_policy_moid = ldap_policy_response.get('Moid') |
| 657 |
|
| 658 |
# Process LDAP groups |
| 659 |
groups_response = [] |
| 660 |
ldap_groups = intersight.module.params.get('ldap_groups') or [] |
| 661 |
if intersight.module.params['state'] == 'present' and ldap_groups: |
| 662 |
for group_config in ldap_groups: |
| 663 |
group_state = group_config.get('state', 'present') |
| 664 |
if group_state == 'present': |
| 665 |
group_api_body = build_ldap_group_api_body(intersight, module, group_config, ldap_policy_moid) |
| 666 |
intersight.api_body = group_api_body |
| 667 |
|
| 668 |
resource_path = '/iam/LdapGroups' |
| 669 |
custom_filter = f"Name eq '{group_config['name']}' and LdapPolicy.Moid eq '{ldap_policy_moid}'" |
| 670 |
intersight.configure_secondary_resource( |
| 671 |
resource_path=resource_path, |
| 672 |
state=group_state, |
| 673 |
custom_filter=custom_filter |
| 674 |
) |
| 675 |
if group_state == 'present': |
| 676 |
groups_response.append(intersight.result['api_response']) |
| 677 |
|
| 678 |
# Process LDAP providers |
| 679 |
providers_response = [] |
| 680 |
ldap_providers = intersight.module.params.get('ldap_providers') or [] |
| 681 |
if intersight.module.params['state'] == 'present' and not intersight.module.params['enable_dns']: |
| 682 |
if ldap_providers: |
| 683 |
for provider_config in ldap_providers: |
| 684 |
provider_state = provider_config.get('state', 'present') |
| 685 |
if provider_state == 'present': |
| 686 |
provider_api_body = build_ldap_provider_api_body(provider_config, ldap_policy_moid) |
| 687 |
intersight.api_body = provider_api_body |
| 688 |
|
| 689 |
resource_path = '/iam/LdapProviders' |
| 690 |
custom_filter = f"Server eq '{provider_config['server']}' and LdapPolicy.Moid eq '{ldap_policy_moid}'" |
| 691 |
intersight.configure_secondary_resource( |
| 692 |
resource_path=resource_path, |
| 693 |
state=provider_state, |
| 694 |
custom_filter=custom_filter |
| 695 |
) |
| 696 |
if provider_state == 'present': |
| 697 |
providers_response.append(intersight.result['api_response']) |
| 698 |
|
| 699 |
if ldap_policy_response: |
| 700 |
ldap_policy_response['Groups'] = groups_response |
| 701 |
ldap_policy_response['Providers'] = providers_response |
| 702 |
intersight.result['api_response'] = ldap_policy_response |
| 703 |
module.exit_json(**intersight.result) |
| 704 |
|
| 705 |
|
| 706 |
if __name__ == '__main__': |
| 707 |
main() |