Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More plugin and output fixes #208

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ansible-deployer
version = 0.0.52
version = 0.0.53
description = Wrapper around ansible-playbook allowing configurable tasks and permissions
long_description = file: README.md
long_description_content_type = text/markdown; charset=UTF-8
Expand Down
2 changes: 1 addition & 1 deletion source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def main(options: dict):
db_writer)
if options["lock"]:
lock.unlock_inventory(lockpath)
blocks.log_exit_messages(logger.logger, log_path, db_path)
blocks.log_exit_messages(logger.logger, workdir, log_path, db_path)
db_writer.finalize_db_write(sequence_record_dict, False)
elif options["subcommand"] == "lock":
lock.lock_inventory(lockpath)
Expand Down
2 changes: 1 addition & 1 deletion source/modules/database/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"play_item_tasks": {
"sequence_id": "text",
"timestamp": "text",
"hostname": "text",
"result": "text",
"changed": "text",
"hostname": "text",
"task_name": "text"
}
}
2 changes: 1 addition & 1 deletion source/modules/outputs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AnsibleDeployerLogger:

formatter = logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")
basic_formatter = "%(asctime)s [%(levelname)s]: %(message)s"
console_formatter = "\n%(asctime)s [%(levelname)s]: %(message)s\n"
console_formatter = "\n%(asctime)s [%(levelname)s]: %(message)s"
raw_formatter = "%(message)s"

def __init__(self, name: str, options: dict):
Expand Down
12 changes: 7 additions & 5 deletions source/modules/outputs/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from os import sep as ossep


def log_exit_messages(logger: Logger, log_path: str, db_path: str) -> None:
def log_exit_messages(logger: Logger, workdir: str, log_path: str, db_path: str) -> None:
"""Log info about logfile and db at the end of exec"""
exe_id = log_path.rsplit(ossep, 2)[1]
logger.info(f"This execution received ID: {exe_id}")
logger.info("More information on this execution can be acquired in:")
logger.info(f"logfile: {log_path}")
logger.info(f"database: {db_path}")
logger.info(f"More information on this execution (ID: {exe_id} ) can be acquired in:")
logger.info(f"\traw logfiles (per play_item):\t{workdir}{ossep}rawlog_*.log")
logger.info(f"\tmain logfile:\t\t\t{log_path}")
logger.info(f"\tdatabase:\t\t\t{db_path}")
logger.info("Popular query example:\t\"SELECT sequence_id,timestamp,result,changed,hostname,"\
"task_name FROM play_item_tasks WHERE changed = 'True' ORDER BY hostname;\"")
9 changes: 6 additions & 3 deletions source/modules/outputs/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ def __init__(self, logger):
def positive_ansible_output(self, warning: list, output: list, command: str):
"""Log output for a positive case in ansible execution"""
if warning:
self.logger.warning("\n".join(warning))
full_warning = "\n\n".join(warning)
self.logger.warning(f"\n{full_warning}")
self.logger.info("\"%s\" ran succesfully", " ".join(command))

def negative_ansible_output(self, warning: list, error: list, command: str):
"""Log output for a negative case in ansible execution"""
if warning:
self.logger.warning("\n".join(warning))
full_warning = "\n\n".join(warning)
self.logger.warning(f"\n{full_warning}")
self.logger.error("\"%s\" failed due to:", " ".join(command))
if error:
self.logger.error("\n".join(error))
full_error = "\n\n".join(error)
self.logger.error(f"\n{full_error}")

def format_std_out(self, std_out):
"""Decode standard output to logger.info"""
Expand Down
3 changes: 3 additions & 0 deletions source/modules/runners/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ansible_deployer.modules.globalvars import ANSIBLE_DEFAULT_CALLBACK_PLUGIN_PATH,\
REQUIRED_CALLBACK_PLUGINS
from ansible_deployer.modules.outputs import blocks
from ansible_deployer.modules.outputs.formatting import Formatters
from ansible_deployer.modules.outputs.loggers import RunLogger

Expand Down Expand Up @@ -170,6 +171,8 @@ def run_playitem(self, callback_settings: dict, config: dict, options: dict, inv
parsed_std["error"], command)
self.lock_obj.unlock_inventory(lockpath)
db_writer.finalize_db_write(sequence_records, False)
blocks.log_exit_messages(
self.logger, self.workdir, self.log_path, self.db_path)
self.logger.critical("Program will exit now.")
sys.exit(71)
except Exception as exc:
Expand Down
5 changes: 3 additions & 2 deletions source/plugins/log_plays_adjusted.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CallbackModule(CallbackBase):
CALLBACK_NEEDS_WHITELIST = True

TIME_FORMAT = "%b %d %Y %H:%M:%S"
MSG_FORMAT = "%(now)s - %(playbook)s - %(task_name)s - %(category)s - changed: \
%(changed_status)s\n\n"
MSG_FORMAT = "%(now)s - %(hostname)s - %(playbook)s - %(task_name)s - %(category)s - changed:"\
" %(changed_status)s\n\n"

def __init__(self):

Expand Down Expand Up @@ -60,6 +60,7 @@ def log(self, result, category):
self.MSG_FORMAT
% {
"now": now,
"hostname": result._host.get_name(),
"playbook": self.playbook,
"task_name": result._task.name,
"category": category,
Expand Down
4 changes: 2 additions & 2 deletions source/plugins/sqlite_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class CallbackModule(CallbackBase):
TABLE_COLUMNS = [
"sequence_id",
"timestamp",
"hostname",
"result",
"changed",
"hostname",
"task_name"
]
TIME_FORMAT = "%b %d %Y %H:%M:%S"
Expand Down Expand Up @@ -85,9 +85,9 @@ def log(self, result, category):
self.TABLE_NAME, *self.TABLE_COLUMNS), (
self.sequence,
now,
result._host.get_name(),
category,
changed_status,
result._host.get_name(),
result._task.name
)
)
Expand Down
6 changes: 3 additions & 3 deletions tests/02a-checkrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ echo -e " ___ ____ _ _\n / _ \___ \ __ _
check_run_ok "ansible-deployer show --debug" "\[DEBUG\]: load_configuration called"

# Check different output options
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra" "\[ERROR\]:.*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra --raw-runner-output" "\[ERROR\]:.*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra --raw-runner-output" ".*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra" "\[ERROR\]:.*\n\n\nTASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra --raw-runner-output" "\[ERROR\]:.*\n\n\nTASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run --task task_with_ansible_fail --stage testing --infrastructure testInfra --raw-runner-output" ".*\n\n\nTASK \[Run ll\]"

echo -e " ___ ____ _ _\n / _ \___ \ __ _ ___| |__ ___ ___| | ___ __ _ _ _ __\n | | | |__) / _\` | _____ / __| '_ \ / _ \/ __| |/ / '__| | | | '_ \ \n | |_| / __/ (_| | |_____| | (__| | | | __/ (__| <| | | |_| | | | |\n \___/_____\__,_| \___|_| |_|\___|\___|_|\_\_| \__,_|_| |_|\n \n _ _ _ _ _ _\n | (_)_ __ ___ (_) |_ ___ _ __ | |_(_) ___ _ __\n | | | '_ \` _ \| | __| / _ \| '_ \| __| |/ _ \| '_ \ \n | | | | | | | | | |_ | (_) | |_) | |_| | (_) | | | |\n |_|_|_| |_| |_|_|\__| \___/| .__/ \__|_|\___/|_| |_|\n |_|\n"
# Check --limit option
Expand Down
6 changes: 3 additions & 3 deletions tests/02a-checkrun_short.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ echo -e " ___ ____ _ _
check_run_ok "ansible-deployer show -d" "\[DEBUG\]: load_configuration called"

# Check different output options
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra" "\[ERROR\]:.*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra --raw-runner-output" "\[ERROR\]:.*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra --raw-runner-output" ".*\n.*TASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra" "\[ERROR\]:.*\n\n\nTASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra --raw-runner-output" "\[ERROR\]:.*\n\n\nTASK \[Run ll\]"
check_message_with_newline_in_output "ansible-deployer run -t task_with_ansible_fail -s testing -i testInfra --raw-runner-output" ".*\n\n\nTASK \[Run ll\]"

echo -e " ___ ____ _ _ _ _\n / _ \___ \ __ _ ___| |__ ___ ___| | ___ __ _ _ _ __ ___| |__ ___ _ __| |_\n | | | |__) / _\` | _____ / __| '_ \ / _ \/ __| |/ / '__| | | | '_ \ / __| '_ \ / _ \| '__| __|\n | |_| / __/ (_| | |_____| | (__| | | | __/ (__| <| | | |_| | | | | \__ \ | | | (_) | | | |_\n \___/_____\__,_| \___|_| |_|\___|\___|_|\_\_| \__,_|_| |_| |___/_| |_|\___/|_| \__|\n \n _ _ _ _ _ _\n | (_)_ __ ___ (_) |_ ___ _ __ | |_(_) ___ _ __\n | | | '_ \` _ \| | __| / _ \| '_ \| __| |/ _ \| '_ \ \n | | | | | | | | | |_ | (_) | |_) | |_| | (_) | | | |\n |_|_|_| |_| |_|_|\__| \___/| .__/ \__|_|\___/|_| |_|\n |_|\n"
# Check --limit option
Expand Down
Loading