diff --git a/changelogs/fragments/win_pagefile-Fix-idempotency-when-same-settings-as-current.yml b/changelogs/fragments/win_pagefile-Fix-idempotency-when-same-settings-as-current.yml new file mode 100644 index 00000000000..4ccfdbdbc04 --- /dev/null +++ b/changelogs/fragments/win_pagefile-Fix-idempotency-when-same-settings-as-current.yml @@ -0,0 +1,2 @@ +bugfixes: + - "win_pagefile - Fix idempotency when same settings as current (https://github.com/ansible/ansible/issues/57836)" diff --git a/lib/ansible/modules/windows/win_pagefile.ps1 b/lib/ansible/modules/windows/win_pagefile.ps1 index c3a88d5a8ee..610028a0e13 100644 --- a/lib/ansible/modules/windows/win_pagefile.ps1 +++ b/lib/ansible/modules/windows/win_pagefile.ps1 @@ -95,8 +95,10 @@ if ($state -eq "absent") { Fail-Json $result "Unable to access '${drive}:' drive" } + $curPagefile = Get-Pagefile $fullPath + # Set pagefile - if ($null -eq (Get-Pagefile $fullPath)) { + if ($null -eq $curPagefile) { try { $pagefile = Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name = $fullPath; InitialSize = 0; MaximumSize = 0} -WhatIf:$check_mode } catch { @@ -129,6 +131,40 @@ if ($state -eq "absent") { } } $result.changed = $true + }else + { + $CurPageFileSystemManaged = (Get-CimInstance -ClassName win32_Pagefile -Property 'System' -Filter "name='$($fullPath.Replace('\','\\'))'").System + if ((-not $check_mode) -and + -not ($systemManaged -or $CurPageFileSystemManaged) -and + ( ($curPagefile.InitialSize -ne $initialSize) -or + ($curPagefile.maximumSize -ne $maximumSize))) + { + $curPagefile.InitialSize = $initialSize + $curPagefile.MaximumSize = $maximumSize + try { + $curPagefile.Put() | out-null + } catch { + $originalExceptionMessage = $($_.Exception.Message) + # Try workaround before failing + try { + Remove-Pagefile $fullPath -whatif:$check_mode + } catch { + Fail-Json $result "Failed to remove pagefile before workaround $($_.Exception.Message) Original exception: $originalExceptionMessage" + } + try { + $pagingFilesValues = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management").PagingFiles + } catch { + Fail-Json $result "Failed to get pagefile settings from the registry for workaround $($_.Exception.Message) Original exception: $originalExceptionMessage" + } + $pagingFilesValues += "$fullPath $initialSize $maximumSize" + try { + Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" "PagingFiles" $pagingFilesValues + } catch { + Fail-Json $result "Failed to set pagefile settings to the registry for workaround $($_.Exception.Message) Original exception: $originalExceptionMessage" + } + } + $result.changed = $true + } } } elseif ($state -eq "query") { $result.pagefiles = @() diff --git a/test/integration/targets/win_pagefile/tasks/main.yml b/test/integration/targets/win_pagefile/tasks/main.yml index 8e9555f5556..81da89d356a 100644 --- a/test/integration/targets/win_pagefile/tasks/main.yml +++ b/test/integration/targets/win_pagefile/tasks/main.yml @@ -164,6 +164,21 @@ that: - c_pagefile_override.changed == true + # Test 7: Test idempotent +- name: Set c pagefile 1024-1024, idempotent + win_pagefile: + drive: C + initial_size: 1024 + maximum_size: 1024 + override: no + state: present + register: c_pagefile_idempotent + +- name: Test set c pagefile idempotent + assert: + that: + - c_pagefile_idempotent.changed == false + - name: Query all pagefiles win_pagefile: state: query