diff --git a/src/cotea/runner.py b/src/cotea/runner.py index 12dc95f..72c0410 100644 --- a/src/cotea/runner.py +++ b/src/cotea/runner.py @@ -1,4 +1,5 @@ import json +import os import threading import cotea.utils as cotea_utils @@ -35,7 +36,8 @@ class runner: - def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False): + def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False, + ansible_pb_bin="/usr/local/bin/ansible-playbook"): logging_lvl = logging.INFO if debug_mod: logging_lvl= logging.DEBUG @@ -64,6 +66,11 @@ def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False): self.progress_bar = ansible_progress_bar() self.execution_tree = AnsibleExecTree() + if os.path.isfile(ansible_pb_bin): + self.ansible_pb_bin = ansible_pb_bin + else: + raise Exception(f"Ansible playbook bin {ansible_pb_bin} not found") + self._set_wrappers() start_ok = self._start_ansible() self.logger.debug("Ansible start ok: %s", start_ok) @@ -133,7 +140,7 @@ def _except_hook(self, args, /): def _start_ansible(self): args = self.arg_maker.args - args.insert(0, "/usr/local/bin/ansible-playbook") + args.insert(0, self.ansible_pb_bin) args.insert(1, self.pb_path) self.pbCLI = PlaybookCLI(args) @@ -363,7 +370,7 @@ def get_all_error_msgs(self): return self.update_conn_wrapper.error_msgs - # returns last error msg that wasn't ignored + # returns last error msg that wasn't ignored def get_error_msg(self): res = "" diff --git a/src/cotea_ansible_error_case.py b/src/cotea_ansible_error_case.py index 83a8d1d..164eda8 100644 --- a/src/cotea_ansible_error_case.py +++ b/src/cotea_ansible_error_case.py @@ -1,3 +1,4 @@ +import shutil import unittest from cotea.runner import runner @@ -10,7 +11,9 @@ def run_ansible_error_case(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) while r.has_next_play(): while r.has_next_task(): @@ -30,7 +33,9 @@ def run_ansible_error_case_with_ignore(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) while r.has_next_play(): while r.has_next_task(): diff --git a/src/cotea_internal_error_case.py b/src/cotea_internal_error_case.py index e495dc4..121d7f1 100644 --- a/src/cotea_internal_error_case.py +++ b/src/cotea_internal_error_case.py @@ -1,3 +1,4 @@ +import shutil import unittest @@ -10,6 +11,25 @@ def tearDown(self) -> None: # to clear previous execution context remove_modules_from_imported(module_name_like="cotea") + def test_incorrect_playbook_bin_path(self): + from cotea.runner import runner + from cotea.arguments_maker import argument_maker + + pb_path = "cotea_run_files/ok.yaml" + inv_path = "cotea_run_files/inv" + ansible_pb_bin = "/path/to/.venv/bin/ansible-playbook" + + arg_maker = argument_maker() + arg_maker.add_arg("-i", inv_path) + + try: + runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=ansible_pb_bin) + except Exception as e: + self.assertTrue(str(e) == f"Ansible playbook bin {ansible_pb_bin} not found", + msg="Unexpected exception message") + else: + self.assertFalse(True, msg="Ansible is supposed to fail due to incorrect ansible playbook bin path") + def test_incorrect_playbook_path_case(self): from cotea.runner import runner from cotea.arguments_maker import argument_maker @@ -17,9 +37,11 @@ def test_incorrect_playbook_path_case(self): pb_path = "cotea_run_files/#%|&" inv_path = "cotea_run_files/inv" + bin_path = shutil.which('ansible-playbook') + arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) try: while r.has_next_play(): @@ -41,10 +63,11 @@ def test_incorrect_syntax_case(self): pb_path = "cotea_run_files/incorrect.yaml" inv_path = "cotea_run_files/inv" + bin_path = shutil.which('ansible-playbook') arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) try: while r.has_next_play(): diff --git a/src/cotea_ok_case.py b/src/cotea_ok_case.py index 5f3fa6a..80d8df8 100644 --- a/src/cotea_ok_case.py +++ b/src/cotea_ok_case.py @@ -1,3 +1,4 @@ +import shutil import unittest from cotea.runner import runner @@ -24,7 +25,9 @@ def run_cotea_ok_case(pb_path, inv_path): arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) - r = runner(pb_path, arg_maker, show_progress_bar=True) + bin_path = shutil.which('ansible-playbook') + + r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path) plays_ind = 0 tasks_ind = 0