perf: 在 ansible 中切换用户时 添加超时操作

pull/12406/head
feng 2023-12-21 19:49:24 +08:00 committed by Bryan
parent bc5494bbb0
commit 6565f8c0a8
1 changed files with 6 additions and 0 deletions

View File

@ -26,6 +26,7 @@ def common_argument_spec():
class SSHClient:
TIMEOUT = 20
SLEEP_INTERVAL = 2
COMPLETE_FLAG = 'complete'
@ -170,7 +171,12 @@ class SSHClient:
time.sleep(self.SLEEP_INTERVAL)
output += self._get_recv()
continue
start_time = time.time()
while self.COMPLETE_FLAG not in output:
if time.time() - start_time > self.TIMEOUT:
error_msg = output
print("切换用户操作超时,跳出循环。")
break
time.sleep(self.SLEEP_INTERVAL)
received_output = self._get_recv().replace(f'"{self.COMPLETE_FLAG}"', '')
output += received_output