Skip to content

Instantly share code, notes, and snippets.

@weiyentan
Created July 4, 2022 09:09
Show Gist options
  • Save weiyentan/b793bddd1c949940f044f337715f6b6d to your computer and use it in GitHub Desktop.
Save weiyentan/b793bddd1c949940f044f337715f6b6d to your computer and use it in GitHub Desktop.
def do_until_success_timeout(self, duration_hrs , wait):
starttime = datetime.utcnow
result = {}
patchingstatus = 'inprogress'
max_end_time = datetime.utcnow() + timedelta(hours=duration_hrs)
while datetime.utcnow < max_end_time :
try:
module_return = self._execute_module(module_name='win_apfwcompliance', module_args=module_args, task_vars=task_vars, tmp=tmp)
except:
if isinstance(e, _ReturnResultException):
msg = " The host {{ inventory_host }} results had an error . . . Continuing"
display.display(msg)
pass
if isinstance(e, AnsibleConnectionFailure ):
msg = " The host {{ inventory_host }} was not able to be contactable"
display.display(msg)
pass
if not module_return.get('failed'):
for key, value in module_return['apfw_patchingstatus'].items():
if key == 'Patchstatus':
patchstatus = value['Patchstatus']
if patchstatus != 'inprogress':
if patchstatus == 'success':
patchingstatus = 'success'
display.v('Patches were successfully installed within the timeframe of the Patching Window')
break
if patchstatus == 'failed':
patchingstatus = 'failed'
display.v('Patches failed to install within the timeframe of the Patching')
break
else:
display.v("Host not contactable Retrying in 10 seconds")
sleep(10)
if patchingstatus == 'success':
result['patchingstatus'] = 'success'
elapsed = datetime.utcnow() - starttime
result['elapsed'] = elapsed.seconds
if patchingstatus == 'failed':
msg = "The patching didn't complete in time."
raise AnsibleError( msg )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment