Skip to content

Commit

Permalink
Implement feature to flatten group members (closes ansible-collection…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannik committed Aug 5, 2024
1 parent 553524d commit d332608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 23 additions & 4 deletions plugins/module_utils/_ADObject.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ Function ConvertTo-AnsibleADDistinguishedName {
[string]
$Server,

[boolean]
$NestedGroupFlatten,

[PSCredential]
$Credential,

Expand Down Expand Up @@ -611,10 +614,17 @@ Function ConvertTo-AnsibleADDistinguishedName {
continue
}

$adDN = Get-AnsibleADObject @getParams |
Select-Object -ExpandProperty DistinguishedName
if ($adDN) {
$results.Add($adDN)
$object = Get-AnsibleADObject @getParams
if ($object) {
if ($NestedGroupFlatten -and $object.ObjectClass -eq "group") {
$dns = Get-ADGroupMember $object -Recursive | Select-Object -ExpandProperty DistinguishedName
}
else {
$dns = $object | Select-Object -ExpandProperty DistinguishedName
}
foreach ($dn in $dns) {
$results.Add($dn)
}
}
else {
$invalidIdentities.Add($getParams.Identity)
Expand Down Expand Up @@ -1043,6 +1053,12 @@ Function Invoke-AnsibleADObject {
}
)

if ($ModuleNoun -eq "ADGroup") {
$spec.options['flatten'] = @{
type = 'bool'
}
}

$module = [Ansible.Basic.AnsibleModule]::Create(@(), $spec)
$module.Result.distinguished_name = $null
$module.Result.object_guid = $null
Expand Down Expand Up @@ -1364,6 +1380,9 @@ Function Invoke-AnsibleADObject {
Context = "$($propInfo.Name).$($actionKvp.Key)"
FailureAction = $propValue.lookup_failure_action
}
if ($propInfo.Name -eq 'members' -and $module.Params.flatten) {
$convertParams['NestedGroupFlatten'] = $true
}
$dns = $actionKvp.Value | ConvertTo-AnsibleADDistinguishedName @adParams @convertParams
$compareParams[$actionKvp.Key] = @($dns)
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/modules/group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ DOCUMENTATION:
- Set this to an empty list to remove all members from a group.
type: list
elements: raw
flatten:
description:
- For nested groups, group members are added directly (nested groups are "flattened").
type: bool
default: false
sam_account_name:
description:
- The C(sAMAccountName) value to set for the group.
Expand Down

0 comments on commit d332608

Please sign in to comment.