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

win_domain_computer module: Fix idempotence when name != sam_account_name #56967

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/win_domain_computer-idempotence.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- win_domain_computer - Fix idempotence checks when ``sAMAccountName`` is different from ``name``
8 changes: 5 additions & 3 deletions lib/ansible/modules/windows/win_domain_computer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ If ($state -eq "present") {
} Else {
$desired_state = [ordered]@{
name = $name
sam_account_name = $sam_account_name
state = $state
}
}
Expand All @@ -68,7 +69,7 @@ Function Get-InitialState($desired_state) {
# Test computer exists
$computer = Try {
Get-ADComputer `
-Identity $desired_state.name `
-Identity $desired_state.sam_account_name `
-Properties DistinguishedName,DNSHostName,Enabled,Name,SamAccountName,Description,ObjectClass `
@extra_args
} Catch { $null }
Expand All @@ -87,6 +88,7 @@ Function Get-InitialState($desired_state) {
} Else {
$initial_state = [ordered]@{
name = $desired_state.name
sam_account_name = $desired_state.sam_account_name
state = "absent"
}
}
Expand All @@ -112,7 +114,7 @@ Function Set-ConstructedState($initial_state, $desired_state) {
If ($initial_state.distinguished_name -cne $desired_state.distinguished_name) {
# Move computer to OU
Try {
Get-ADComputer -Identity $desired_state.name @extra_args |
Get-ADComputer -Identity $desired_state.sam_account_name @extra_args |
Move-ADObject `
-TargetPath $desired_state.ou `
-Confirm:$False `
Expand Down Expand Up @@ -147,7 +149,7 @@ Function Add-ConstructedState($desired_state) {
# ------------------------------------------------------------------------------
Function Remove-ConstructedState($initial_state) {
Try {
Get-ADComputer -Identity $initial_state.name @extra_args |
Get-ADComputer -Identity $initial_state.sam_account_name @extra_args |
Remove-ADObject `
-Recursive `
-Confirm:$False `
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/windows/win_domain_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
- name: Add linux computer to Active Directory OU using a windows machine
win_domain_computer:
name: one_linux_server.my_org.local
sam_account_name: linux_server
sam_account_name: linux_server$
dns_hostname: one_linux_server.my_org.local
ou: "OU=servers,DC=my_org,DC=local"
description: Example of linux server
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/win_domain_computer/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unsupported
71 changes: 71 additions & 0 deletions test/integration/targets/win_domain_computer/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# this won't run in Ansible's integration tests until we get a domain set up
# these are here if someone wants to run the module tests locally on their own
# domain.
# Requirements:
# LDAP Base path set in defaults/main.yml like DC=ansible,DC=local
# Custom OU path set in defaults/main.yml like OU=ou1,DC=ansible,DC=local
---
- name: run win_domain_users test
hosts: win_domain_computer_testing_host
vars:
test_win_domain_computer_ldap_base: "{{ test_ad_ou }}"
test_win_domain_computer_ou_path: "{{ test_ad_group_ou }}"
test_win_domain_computer_name: "test_computer.{{ test_domain_name }}"
tasks:

- name: ensure the computer is deleted before the test
win_domain_computer:
name: '{{ test_win_domain_computer_name }}'
state: absent

# --------------------------------------------------------------------------

- name: Test computer with long name and distinct sam_account_name
vars:
test_win_domain_computer_long_name: '{{ test_win_domain_computer_name }}_with_long_name'
test_win_domain_computer_sam_account_name: '{{ test_win_domain_computer_name }}$'
block:

# ----------------------------------------------------------------------
- name: create computer with long name and distinct sam_account_name
win_domain_computer:
name: '{{ test_win_domain_computer_long_name }}'
sam_account_name: '{{ test_win_domain_computer_sam_account_name }}'
enabled: yes
state: present
register: create_distinct_sam_account_name
check_mode: yes

- name: get actual computer with long name and distinct sam_account_name
win_command: powershell.exe "Import-Module ActiveDirectory; Get-ADComputer -Identity '{{ test_win_domain_computer_sam_account_name }}'"
register: create_distinct_sam_account_name_check
ignore_errors: True

- name: assert create computer with long name and distinct sam_account_name
assert:
that:
- create_distinct_sam_account_name is changed
- create_distinct_sam_account_name_check.rc == 1

- name: (Idempotence) create computer with long name and distinct sam_account_name
win_domain_computer:
name: '{{ test_win_domain_computer_long_name }}'
sam_account_name: '{{ test_win_domain_computer_sam_account_name }}'
enabled: yes
state: present
register: create_distinct_sam_account_name_idempotence
check_mode: yes

- name: (Idempotence) assert create computer with long name and distinct sam_account_name
assert:
that:
- create_distinct_sam_account_name_idempotence is not changed

- name: ensure the test group is deleted after the test
win_domain_computer:
name: '{{ test_win_domain_computer_long_name }}'
sam_account_name: '{{ test_win_domain_computer_sam_account_name }}'
state: absent
ignore_protection: True

# ----------------------------------------------------------------------