Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement feature to flatten group members (closes #128) #132

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
28 changes: 24 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,

[Switch]
$NestedGroupFlatten,
Yannik marked this conversation as resolved.
Show resolved Hide resolved

[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 @getParams -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,13 @@ Function Invoke-AnsibleADObject {
}
)

if ($ModuleNoun -eq "ADGroup") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be part of the shared module util function but an option inside the group module itself. Unfortunately it probably means that the logic for the members option needs to split out of the util.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this in more detail? I don't really understand what to do here.

$spec.options['flatten'] = @{
type = 'bool'
default = $false
}
}

$module = [Ansible.Basic.AnsibleModule]::Create(@(), $spec)
$module.Result.distinguished_name = $null
$module.Result.object_guid = $null
Expand Down Expand Up @@ -1364,6 +1381,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
6 changes: 6 additions & 0 deletions plugins/modules/group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ DOCUMENTATION:
- Set this to an empty list to remove all members from a group.
type: list
elements: raw
flatten:
description:
- Flattens nested groups.
type: bool
default: false
version_added: 1.7.0
sam_account_name:
description:
- The C(sAMAccountName) value to set for the group.
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/targets/group/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,43 @@
that:
- not unset_member_again is changed

- name: create sub group and set members
group:
name: MySubGroup
scope: global
members:
set:
- my_user_1
- my_user_2
register: sub_group

- name: set members with sub group flattening
group:
name: MyGroup
flatten: true
members:
set:
- MySubGroup
- my_user_3
register: set_member_flattened

- name: get result of set members with flattening
object_info:
identity: '{{ object_identity }}'
properties:
- member
register: set_member_flattened_actual

- name: assert set members with flattening
assert:
that:
- set_member_flattened is changed
- set_member_flattened_actual.objects[0].member | length == 3
- test_users.results[0].distinguished_name in set_member_flattened_actual.objects[0].member
- test_users.results[1].distinguished_name in set_member_flattened_actual.objects[0].member
- test_users.results[2].distinguished_name in set_member_flattened_actual.objects[0].member
- sub_group.distinguished_name not in set_member_flattened_actual.objects[0].member

- name: remove group - check
group:
name: MyGroup
Expand Down