Skip to content

Commit

Permalink
Add raw logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cinek810 committed Oct 20, 2023
1 parent 9013918 commit 745b44f
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions source/modules/runners/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,45 +131,47 @@ def run_playitem(self, config: dict, options: dict, inventory: str, lockpath: st
for playitem in playitems:
command, command_env = self.construct_command(playitem, inventory, config, options)
self.logger.debug("Running '%s'.", command)
try:
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=command_env) as proc:

for msg in proc.stdout:
dec_msg = msg.split(b"\n")[0].decode("utf-8")
returned.append(dec_msg)
if options["raw_output"]:
print(dec_msg)

proc.communicate()
format_obj = Formatters(self.logger)
parsed_std = format_obj.format_ansible_output(returned)
host_list = db_writer.parse_yaml_output_for_hosts(parsed_std["complete"],
self.sequence_id)
sequence_records = db_writer.start_sequence_dict(host_list,
self.setup_hooks, options,
self.start_ts_raw,
with open(f"{self.log_path}.raw", "x", encoding="utf-8") as raw_log:
try:
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=command_env) as proc:

for msg in proc.stdout:
raw_log.write(msg.decode("utf-8"))
dec_msg = msg.split(b"\n")[0].decode("utf-8")
returned.append(dec_msg)
if options["raw_output"]:
print(dec_msg)

proc.communicate()
format_obj = Formatters(self.logger)
parsed_std = format_obj.format_ansible_output(returned)
host_list = db_writer.parse_yaml_output_for_hosts(parsed_std["complete"],
self.sequence_id)
sequence_records = db_writer.start_sequence_dict(host_list,
self.setup_hooks, options,
self.start_ts_raw,
self.sequence_id)

if proc.returncode == 0:
format_obj.positive_ansible_output(parsed_std["warning"],
parsed_std["output"], command)
else:
format_obj.negative_ansible_output(parsed_std["warning"],
parsed_std["error"], command)
self.lock_obj.unlock_inventory(lockpath)
db_writer.finalize_db_write(sequence_records, False)
self.logger.critical("Program will exit now.")
sys.exit(71)
except Exception as exc:
self.logger.critical("\"%s\" failed due to:")
self.logger.critical(exc)
sequence_records = db_writer.start_sequence_dict(host_list, self.setup_hooks,
options, self.start_ts_raw,
self.sequence_id)

if proc.returncode == 0:
format_obj.positive_ansible_output(parsed_std["warning"],
parsed_std["output"], command)
else:
format_obj.negative_ansible_output(parsed_std["warning"],
parsed_std["error"], command)
self.lock_obj.unlock_inventory(lockpath)
db_writer.finalize_db_write(sequence_records, False)
self.logger.critical("Program will exit now.")
sys.exit(71)
except Exception as exc:
self.logger.critical("\"%s\" failed due to:")
self.logger.critical(exc)
sequence_records = db_writer.start_sequence_dict(host_list, self.setup_hooks,
options, self.start_ts_raw,
self.sequence_id)
db_writer.finalize_db_write(sequence_records, True)
sys.exit(72)
return sequence_records
db_writer.finalize_db_write(sequence_records, True)
sys.exit(72)
return sequence_records

@staticmethod
def get_tags_for_task(config: dict, options: dict):
Expand Down

0 comments on commit 745b44f

Please sign in to comment.