win_pagefile - Fix idempotency when same settings as current (#57893)

* win_pagefile - Fix idempotency when same settings as current

* Fix tests and code

* Fix problem with system managed

* Fix again systemmanaged detection

* Change check of systemmanged in creation

* Fix readability and wrong flag for test
pull/39994/head
ShachafGoldstein 5 years ago committed by Matt Davis
parent 5e2451c1a8
commit 65b0e1425b

@ -0,0 +1,2 @@
bugfixes:
- "win_pagefile - Fix idempotency when same settings as current (https://github.com/ansible/ansible/issues/57836)"

@ -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 = @()

@ -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

Loading…
Cancel
Save