#!/usr/bin/python import os.path import time import shlex from ansible.plugins.action import ActionBase class ActionModule(ActionBase): def run(self, tmp=None, task_vars=None): super(ActionModule, self).run(tmp, task_vars) module_args = self._task.args.copy() async_job_finished = False async_status = None status_file = shlex.quote(module_args["status_file"]) bytes_read_so_far = 0 stat_reported_bytes = 0 while not async_job_finished: stat_return = self._execute_module( module_name="command", module_args={ "argv": ["stat", "-c", "%s", status_file] }, task_vars=task_vars, tmp=tmp ) stat_reported_bytes = int(stat_return["stdout"]) if bytes_read_so_far < stat_reported_bytes: rsync_window_ret = self.read_status_file(stat_reported_bytes, bytes_read_so_far, status_file, task_vars, tmp) print(rsync_window_ret["stdout"], end="", flush=True) bytes_read_so_far = stat_reported_bytes time.sleep(1) async_status = self._execute_module( module_name="async_status", module_args={ "jid": module_args["async_job"]["ansible_job_id"], "_async_dir": os.path.dirname(module_args["async_job"]["results_file"]) }, task_vars=task_vars, tmp=tmp ) async_job_finished = bool(async_status['finished']) ret = {} if async_status["rc"] == 0: ret["changed"] = False else: ret["failed"] = True ret["stderr"] = async_status["stderr"] print(self.read_status_file(stat_reported_bytes, bytes_read_so_far, status_file, task_vars, tmp)["stdout"]) return ret def read_status_file(self, stat_reported_bytes, bytes_read_so_far, status_file, task_vars, tmp): return self._execute_module( module_name="command", module_args={ "argv": ["sh", "-c", f"head -q -c {stat_reported_bytes} {status_file} | tail -q -c +{bytes_read_so_far}"] }, task_vars=task_vars, tmp=tmp )