Skip to content

Commit

Permalink
Make name optional
Browse files Browse the repository at this point in the history
Allows using the identity module option if no name was specified.
Currently name must be set with state=present but identity will be used
if no name was specified.
  • Loading branch information
jborean93 committed Oct 2, 2023
1 parent 6a577ee commit 42c54af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/search-by-identity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- Make ``name`` an optional parameter for the AD modules. Either ``name`` or ``identity`` needs to be set with their
respective behaviours. If creating a new AD user and only ``identity`` is set, that will be the value used for the
name of the object.
2 changes: 1 addition & 1 deletion plugins/doc_fragments/ad_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ModuleDocFragment:
- The C(name) of the AD object to manage.
- If I(identity) is specified, and the name of the object it found does not
match this value, the object will be renamed.
- This must be set when I(state=present) or if I(identity) is not set.
- This if I(identity) must be set to find the object to manage.
- This is not always going to be the same as the C(sAMAccountName) for user
objects. It is strictly the C(name) of the object in the path specified.
Use I(identity) to select an object to manage by C(sAMAccountName).
Expand Down
19 changes: 13 additions & 6 deletions plugins/module_utils/_ADObject.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ Function Invoke-AnsibleADObject {
}

$stateRequiredIf = @{
present = @('name')
present = @()
absent = @()
}

Expand Down Expand Up @@ -905,9 +905,15 @@ Function Invoke-AnsibleADObject {
$objectGuid = $null

if (-not $adObject) {
$adName = if ($module.Params.name) {
$module.Params.name
}
else {
$module.Params.identity
}
$newParams = @{
Confirm = $false
Name = $module.Params.name
Name = $adName
WhatIf = $module.CheckMode
PassThru = $true
}
Expand All @@ -926,7 +932,7 @@ Function Invoke-AnsibleADObject {

$module.Diff.after = @{
attributes = $diffAttributes.after
name = $module.Params.name
name = $adName
path = $objectPath
}

Expand Down Expand Up @@ -985,7 +991,7 @@ Function Invoke-AnsibleADObject {
$module.Result.changed = $true

if ($module.CheckMode) {
$objectDN = "$namePrefix=$($module.Params.name -replace ',', '\,'),$objectPath"
$objectDN = "$namePrefix=$($adName -replace ',', '\,'),$objectPath"
$objectGuid = [Guid]::Empty # Dummy value for check mode
}
else {
Expand Down Expand Up @@ -1075,8 +1081,9 @@ Function Invoke-AnsibleADObject {
}

$finalADObject = $null
if ($module.Params.name -cne $objectName) {
$objectName = $module.Params.name
$desiredName = $module.Params.name
if ($desiredName -and $desiredName -cne $objectName) {
$objectName = $desiredName
$module.Diff.after.name = $objectName

$finalADObject = Rename-ADObject @commonParams @adParams -NewName $objectName
Expand Down

0 comments on commit 42c54af

Please sign in to comment.