Created
June 11, 2022 09:13
-
-
Save weiyentan/9c398f61aa48ca4059a8aebdd3ee0512 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Make coding more python3-ish, this is required for contributions to Ansible | |
from __future__ import (absolute_import, division, print_function) | |
from turtle import delay | |
__metaclass__ = type | |
from ansible.errors import AnsibleError, AnsibleConnectionFailure | |
from ansible.plugins.action import ActionBase | |
from ansible.utils.display import Display | |
from datetime import datetime, timedelta | |
from time import sleep | |
display = Display() | |
class TimedOutException(Exception): | |
pass | |
class ActionModule(ActionBase): | |
TRANSFERS_FILES = False | |
_VALID_ARGS = frozenset(( | |
'duration_hrs' | |
'wait' | |
'delay' | |
)) | |
DEFAULT_DURATION_HRS = 3 | |
DEFAULT_WAIT = 10 | |
DEFAULT_DELAY = 5 | |
def _check_delay(self, key, default): | |
"""Ensure that the value is positive or zero""" | |
value = int(self._task.args.get(key, self._task.args.get(key, default))) | |
if value < 0: | |
value = 0 | |
return value | |
@property | |
def duration_hrs(self): | |
return self._check_delay('duration_hrs', self.DEFAULT_DURATION_HRS) | |
@property | |
def wait(self): | |
return self._check_delay('wait', self.DEFAULT_WAIT) | |
@property | |
def delay(self): | |
return self._check_delay('wait', self.DEFAULT_DELAY) | |
def do_until_success_timeout(self, duration_hrs , wait): | |
patchingstatus = 'inprogress' | |
max_end_time = datetime.utcnow() + timedelta(hours=duration_hrs) | |
while datetime.utcnow < max_end_time : | |
module_return = self._execute_module(module_name='win_apfwcompliance', module_args=module_args, task_vars=task_vars, tmp=tmp) | |
if not module_return.get('failed'): | |
for key, value in module_return['apfw_patchingstatus'].items(): | |
if key == 'Patchstatus': | |
patchstatus = value['Patchstatus'] | |
else: | |
display.v("Host not contactable Retrying in 10 seconds") | |
sleep(10) | |
if patchstatus != 'inprogress': | |
if patchstatus == 'success': | |
patchingstatus = 'success' | |
display.v('Patches were successfully installed') | |
return | |
def run(self, tmp=None, task_vars=None): | |
convert_minutes_to_seconds = delay * 60 | |
sleep(convert_minutes_to_seconds) | |
self.do_until_success_timeout(duration_hrs , wait) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment