| 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_info |
| 16 |
short_description: Gather information about LDAP Policies in Cisco Intersight |
| 17 |
description: |
| 18 |
- Gather information about LDAP Policies in L(Cisco Intersight,https://intersight.com). |
| 19 |
- Information can be filtered by O(organization) and O(name). |
| 20 |
- If no filters are passed, all LDAP Policies will be returned. |
| 21 |
- This module retrieves LDAP policy details including associated groups and providers. |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
organization: |
| 25 |
description: |
| 26 |
- The name of the organization the LDAP Policy belongs to. |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the LDAP Policy to gather information from. |
| 31 |
type: str |
| 32 |
author: |
| 33 |
- Ron Gershburg (@rgershbu) |
| 34 |
''' |
| 35 |
|
| 36 |
EXAMPLES = r''' |
| 37 |
- name: Fetch a specific LDAP Policy by name |
| 38 |
cisco.intersight.intersight_ldap_policy_info: |
| 39 |
api_private_key: "{{ api_private_key }}" |
| 40 |
api_key_id: "{{ api_key_id }}" |
| 41 |
name: "ldap-dns-policy" |
| 42 |
|
| 43 |
- name: Fetch all LDAP Policies in a specific Organization |
| 44 |
cisco.intersight.intersight_ldap_policy_info: |
| 45 |
api_private_key: "{{ api_private_key }}" |
| 46 |
api_key_id: "{{ api_key_id }}" |
| 47 |
organization: "org_test" |
| 48 |
|
| 49 |
- name: Fetch all LDAP Policies |
| 50 |
cisco.intersight.intersight_ldap_policy_info: |
| 51 |
api_private_key: "{{ api_private_key }}" |
| 52 |
api_key_id: "{{ api_key_id }}" |
| 53 |
''' |
| 54 |
|
| 55 |
RETURN = r''' |
| 56 |
api_response: |
| 57 |
description: The API response output returned by the specified resource. |
| 58 |
returned: always |
| 59 |
type: dict |
| 60 |
sample: |
| 61 |
"api_response": [ |
| 62 |
{ |
| 63 |
"Name": "ldap-dns-policy", |
| 64 |
"ObjectType": "iam.LdapPolicy", |
| 65 |
"Enabled": true, |
| 66 |
"BaseProperties": { |
| 67 |
"BaseDn": "dc=example,dc=com", |
| 68 |
"Domain": "example.com", |
| 69 |
"Timeout": 30, |
| 70 |
"EnableEncryption": true, |
| 71 |
"BindMethod": "LoginCredentials", |
| 72 |
"Filter": "sAMAccountName", |
| 73 |
"GroupAttribute": "memberOf", |
| 74 |
"Attribute": "CiscoAvPair", |
| 75 |
"EnableGroupAuthorization": true, |
| 76 |
"EnableNestedGroupSearch": true, |
| 77 |
"NestedGroupSearchDepth": 64 |
| 78 |
}, |
| 79 |
"EnableDns": true, |
| 80 |
"DnsParameters": { |
| 81 |
"Source": "Configured", |
| 82 |
"SearchDomain": "example.com", |
| 83 |
"SearchForest": "example.com" |
| 84 |
}, |
| 85 |
"UserSearchPrecedence": "LocalUserDb", |
| 86 |
"Groups": [ |
| 87 |
{ |
| 88 |
"Name": "admin-group", |
| 89 |
"ObjectType": "iam.LdapGroup", |
| 90 |
"GroupDn": "cn=admins,ou=groups,dc=example,dc=com", |
| 91 |
"Domain": "example.com", |
| 92 |
"Moid": "5dee1d736972652d321d26c5" |
| 93 |
}, |
| 94 |
{ |
| 95 |
"Name": "readonly-group", |
| 96 |
"ObjectType": "iam.LdapGroup", |
| 97 |
"GroupDn": "cn=readers,ou=groups,dc=example,dc=com", |
| 98 |
"Domain": "", |
| 99 |
"Moid": "5dee1d736972652d321d26c6" |
| 100 |
} |
| 101 |
], |
| 102 |
"Providers": [], |
| 103 |
"Organization": { |
| 104 |
"Name": "default", |
| 105 |
"Moid": "5dee1d736972652d321d26b5", |
| 106 |
"ObjectType": "organization.Organization" |
| 107 |
}, |
| 108 |
"Tags": [ |
| 109 |
{ |
| 110 |
"Key": "Environment", |
| 111 |
"Value": "Production" |
| 112 |
} |
| 113 |
] |
| 114 |
}, |
| 115 |
{ |
| 116 |
"Name": "ldap-provider-policy", |
| 117 |
"ObjectType": "iam.LdapPolicy", |
| 118 |
"Enabled": true, |
| 119 |
"BaseProperties": { |
| 120 |
"BaseDn": "dc=company,dc=local", |
| 121 |
"Domain": "company.local", |
| 122 |
"Timeout": 0, |
| 123 |
"EnableEncryption": false, |
| 124 |
"BindMethod": "Anonymous", |
| 125 |
"Filter": "sAMAccountName", |
| 126 |
"GroupAttribute": "memberOf", |
| 127 |
"Attribute": "CiscoAvPair", |
| 128 |
"EnableGroupAuthorization": false, |
| 129 |
"EnableNestedGroupSearch": false |
| 130 |
}, |
| 131 |
"EnableDns": false, |
| 132 |
"UserSearchPrecedence": "LocalUserDb", |
| 133 |
"Groups": [ |
| 134 |
{ |
| 135 |
"Name": "users-group", |
| 136 |
"ObjectType": "iam.LdapGroup", |
| 137 |
"GroupDn": "cn=users,ou=groups,dc=company,dc=local", |
| 138 |
"Domain": "", |
| 139 |
"Moid": "5dee1d736972652d321d26c7" |
| 140 |
} |
| 141 |
], |
| 142 |
"Providers": [ |
| 143 |
{ |
| 144 |
"Server": "10.10.10.10", |
| 145 |
"Port": 389, |
| 146 |
"Vendor": "OpenLDAP", |
| 147 |
"ObjectType": "iam.LdapProvider", |
| 148 |
"Moid": "5dee1d736972652d321d26c8" |
| 149 |
}, |
| 150 |
{ |
| 151 |
"Server": "10.10.10.11", |
| 152 |
"Port": 389, |
| 153 |
"Vendor": "OpenLDAP", |
| 154 |
"ObjectType": "iam.LdapProvider", |
| 155 |
"Moid": "5dee1d736972652d321d26c9" |
| 156 |
} |
| 157 |
], |
| 158 |
"Organization": { |
| 159 |
"Name": "default", |
| 160 |
"Moid": "5dee1d736972652d321d26b5", |
| 161 |
"ObjectType": "organization.Organization" |
| 162 |
}, |
| 163 |
"Tags": [] |
| 164 |
} |
| 165 |
] |
| 166 |
''' |
| 167 |
|
| 168 |
|
| 169 |
from ansible.module_utils.basic import AnsibleModule |
| 170 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 171 |
|
| 172 |
|
| 173 |
def main(): |
| 174 |
argument_spec = intersight_argument_spec.copy() |
| 175 |
argument_spec.update( |
| 176 |
organization=dict(type='str'), |
| 177 |
name=dict(type='str') |
| 178 |
) |
| 179 |
module = AnsibleModule( |
| 180 |
argument_spec, |
| 181 |
supports_check_mode=True, |
| 182 |
) |
| 183 |
|
| 184 |
# Initialize the Intersight module |
| 185 |
intersight = IntersightModule(module) |
| 186 |
intersight.result['api_response'] = {} |
| 187 |
intersight.result['trace_id'] = '' |
| 188 |
|
| 189 |
# Resource path used to fetch info |
| 190 |
resource_path = '/iam/LdapPolicies' |
| 191 |
|
| 192 |
# Set query parameters |
| 193 |
query_params = intersight.set_query_params() |
| 194 |
|
| 195 |
# Get LDAP policies |
| 196 |
intersight.get_resource( |
| 197 |
resource_path=resource_path, |
| 198 |
query_params=query_params, |
| 199 |
return_list=True |
| 200 |
) |
| 201 |
|
| 202 |
# Fetch LDAP groups and providers for each LDAP policy |
| 203 |
ldap_policies = intersight.result['api_response'] |
| 204 |
if isinstance(ldap_policies, list): |
| 205 |
for policy in ldap_policies: |
| 206 |
if policy.get('Moid'): |
| 207 |
# Fetch LDAP groups for this policy |
| 208 |
groups_query_params = { |
| 209 |
'$filter': f"LdapPolicy.Moid eq '{policy['Moid']}'" |
| 210 |
} |
| 211 |
temp_intersight_groups = IntersightModule(module) |
| 212 |
temp_intersight_groups.get_resource( |
| 213 |
resource_path='/iam/LdapGroups', |
| 214 |
query_params=groups_query_params, |
| 215 |
return_list=True |
| 216 |
) |
| 217 |
policy['Groups'] = temp_intersight_groups.result.get('api_response', []) |
| 218 |
|
| 219 |
# Fetch LDAP providers for this policy |
| 220 |
providers_query_params = { |
| 221 |
'$filter': f"LdapPolicy.Moid eq '{policy['Moid']}'" |
| 222 |
} |
| 223 |
temp_intersight_providers = IntersightModule(module) |
| 224 |
temp_intersight_providers.get_resource( |
| 225 |
resource_path='/iam/LdapProviders', |
| 226 |
query_params=providers_query_params, |
| 227 |
return_list=True |
| 228 |
) |
| 229 |
policy['Providers'] = temp_intersight_providers.result.get('api_response', []) |
| 230 |
|
| 231 |
elif isinstance(ldap_policies, dict) and ldap_policies.get('Moid'): |
| 232 |
# Single policy case |
| 233 |
groups_query_params = { |
| 234 |
'$filter': f"LdapPolicy.Moid eq '{ldap_policies['Moid']}'" |
| 235 |
} |
| 236 |
temp_intersight_groups = IntersightModule(module) |
| 237 |
temp_intersight_groups.get_resource( |
| 238 |
resource_path='/iam/LdapGroups', |
| 239 |
query_params=groups_query_params, |
| 240 |
return_list=True |
| 241 |
) |
| 242 |
ldap_policies['Groups'] = temp_intersight_groups.result.get('api_response', []) |
| 243 |
|
| 244 |
# Fetch LDAP providers for this policy |
| 245 |
providers_query_params = { |
| 246 |
'$filter': f"LdapPolicy.Moid eq '{ldap_policies['Moid']}'" |
| 247 |
} |
| 248 |
temp_intersight_providers = IntersightModule(module) |
| 249 |
temp_intersight_providers.get_resource( |
| 250 |
resource_path='/iam/LdapProviders', |
| 251 |
query_params=providers_query_params, |
| 252 |
return_list=True |
| 253 |
) |
| 254 |
ldap_policies['Providers'] = temp_intersight_providers.result.get('api_response', []) |
| 255 |
|
| 256 |
# Exit the module |
| 257 |
module.exit_json(**intersight.result) |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |