From 65b0e1425bbdc8e78f95f28396c3b58402803eac Mon Sep 17 00:00:00 2001 From: ShachafGoldstein Date: Tue, 25 Jun 2019 23:19:59 +0300 Subject: 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 --- lib/ansible/modules/windows/win_pagefile.ps1 | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ansible/modules/windows/win_pagefile.ps1 b/lib/ansible/modules/windows/win_pagefile.ps1 index c3a88d5a8e..610028a0e1 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 = @() -- cgit v1.2.1