From f588b1cdf9c39fffe6b17103dfac17f795c81730 Mon Sep 17 00:00:00 2001
From: Dag Wieers <dag@wieers.com>
Date: Tue, 28 Aug 2018 23:31:24 +0200
Subject: [PATCH] Fix issue when timeout and state=present (#43464)

* Fix issue when timeout and state=present

* added changelog fragment
---
 changelogs/fragments/win_wait_for-timeout.yaml |  2 ++
 lib/ansible/modules/windows/win_wait_for.ps1   | 12 ++++++------
 2 files changed, 8 insertions(+), 6 deletions(-)
 create mode 100644 changelogs/fragments/win_wait_for-timeout.yaml

diff --git a/changelogs/fragments/win_wait_for-timeout.yaml b/changelogs/fragments/win_wait_for-timeout.yaml
new file mode 100644
index 00000000000..4e52c39d2a6
--- /dev/null
+++ b/changelogs/fragments/win_wait_for-timeout.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+- win_wait_for - fix issue where timeout doesn't wait unless state=drained - https://github.com/ansible/ansible/issues/43446
diff --git a/lib/ansible/modules/windows/win_wait_for.ps1 b/lib/ansible/modules/windows/win_wait_for.ps1
index 8b780888489..9e299eb66c4 100644
--- a/lib/ansible/modules/windows/win_wait_for.ps1
+++ b/lib/ansible/modules/windows/win_wait_for.ps1
@@ -36,7 +36,7 @@ if ($path -ne $null) {
     if ($state -in @("stopped","drained")) {
         Fail-Json $result "state=$state should only be used for checking a port in the win_wait_for module"
     }
-    
+
     if ($exclude_hosts -ne $null) {
         Fail-Json $result "exclude_hosts should only be used when checking a port and state=drained in the win_wait_for module"
     }
@@ -84,7 +84,7 @@ Function Get-PortConnections($hostname, $port) {
     } else {
         $active_connections = $conn_info.GetActiveTcpConnections() | Where-Object { $_.LocalEndPoint.Address -eq $hostname -and $_.LocalEndPoint.Port -eq $port }
     }
-    
+
     if ($active_connections -ne $null) {
         foreach ($active_connection in $active_connections) {
             $connections += $active_connection.RemoteEndPoint.Address
@@ -101,7 +101,7 @@ if ($delay -ne $null) {
 }
 
 $attempts = 0
-if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
+if ($path -eq $null -and $port -eq $null -and $state -ne "drained") {
     Start-Sleep -Seconds $timeout
 } elseif ($path -ne $null) {
     if ($state -in @("present", "started")) {
@@ -133,7 +133,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
                 Fail-Json $result "timeout while waiting for file $path to be present"
             } else {
                 Fail-Json $result "timeout while waiting for string regex $search_regex in file $path to match"
-            }  
+            }
         }
     } elseif ($state -in @("absent")) {
         # check if the file is deleted or string doesn't exist in file
@@ -165,7 +165,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
                 Fail-Json $result "timeout while waiting for file $path to be absent"
             } else {
                 Fail-Json $result "timeout while waiting for string regex $search_regex in file $path to not match"
-            }            
+            }
         }
     }
 } elseif ($port -ne $null) {
@@ -252,7 +252,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
             $result.elapsed = $elapsed_seconds
             Fail-Json $result "timeout while waiting for $($hostname):$port to drain"
         }
-    }  
+    }
 }
 
 $result.wait_attempts = $attempts