Skip to content

Commit

Permalink
Fix for ansible-core 2.14 and higher now works for adding Blocks too …
Browse files Browse the repository at this point in the history
…(also useless ansible_bin_path was removed)
  • Loading branch information
davidBMSTU committed Oct 30, 2024
1 parent bb455cb commit 0366cd2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 43 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = cotea
version = 1.3.19
version = 1.3.20
author = David Badalyan
author_email = [email protected]
description = Tool that provides Python API to run Ansible programmatically.
Expand Down
14 changes: 6 additions & 8 deletions src/cotea/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@


class runner:
def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False,
ansible_pb_bin="/usr/local/bin/ansible-playbook"):
def __init__(self, pb_path, arg_maker, debug_mod=None, show_progress_bar=False):
logging_lvl = logging.INFO
if debug_mod:
logging_lvl= logging.DEBUG
Expand Down Expand Up @@ -78,11 +77,6 @@ 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)
Expand Down Expand Up @@ -152,7 +146,11 @@ def _except_hook(self, args, /):

def _start_ansible(self):
args = self.arg_maker.args
args.insert(0, self.ansible_pb_bin)

# this arg doesn't affect to something
# ansible will run everything, no matter either arg is None or any random string
args.insert(0, "/usr/local/bin/ansible-playbook")

args.insert(1, self.pb_path)

self.pbCLI = PlaybookCLI(args)
Expand Down
10 changes: 9 additions & 1 deletion src/cotea/wrappers/get_next_task_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ansible.inventory.host import Host
from ansible.playbook.task import Task as AnsibleTaskClass
from ansible.playbook.block import Block as AnsibleBlockClass

from cotea.wrappers.wrapper_base import wrapper_base
from cotea.wrappers.ansi_breakpoint import ansi_breakpoint
Expand Down Expand Up @@ -162,7 +164,13 @@ def add_tasks(self, new_tasks):
self.play_iterator.add_tasks(host, new_tasks)

if hasattr(self.play_iterator, "all_tasks"):
self.play_iterator.all_tasks.extend(new_tasks)
for task_or_block in new_tasks:
if isinstance(task_or_block, AnsibleTaskClass):
self.play_iterator.all_tasks.append(task_or_block)
elif isinstance(task_or_block, AnsibleBlockClass):
if task_or_block.has_tasks():
self.play_iterator._blocks.append(task_or_block)
self.play_iterator.all_tasks.extend(task_or_block.get_tasks())

return True, ""

Expand Down
8 changes: 2 additions & 6 deletions src/cotea_ansible_error_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def run_ansible_error_case(pb_path, inv_path):
arg_maker = argument_maker()
arg_maker.add_arg("-i", inv_path)

bin_path = shutil.which('ansible-playbook')

r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

while r.has_next_play():
while r.has_next_task():
Expand All @@ -33,9 +31,7 @@ def run_ansible_error_case_with_ignore(pb_path, inv_path):
arg_maker = argument_maker()
arg_maker.add_arg("-i", inv_path)

bin_path = shutil.which('ansible-playbook')

r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

while r.has_next_play():
while r.has_next_task():
Expand Down
26 changes: 2 additions & 24 deletions src/cotea_internal_error_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,16 @@ 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

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, ansible_pb_bin=bin_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

try:
while r.has_next_play():
Expand All @@ -63,11 +42,10 @@ 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, ansible_pb_bin=bin_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

try:
while r.has_next_play():
Expand Down
4 changes: 1 addition & 3 deletions src/cotea_ok_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def run_cotea_ok_case(pb_path, inv_path):
arg_maker = argument_maker()
arg_maker.add_arg("-i", inv_path)

bin_path = shutil.which('ansible-playbook')

r = runner(pb_path, arg_maker, show_progress_bar=True, ansible_pb_bin=bin_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)
plays_ind = 0
tasks_ind = 0

Expand Down

0 comments on commit 0366cd2

Please sign in to comment.