Skip to content

Commit

Permalink
Add upload_playbook attribute to run method on PlaybookRunner class to (
Browse files Browse the repository at this point in the history
#125)

allow the playbook run from the remote host, instead of the local one

Add test for remote_playbook case

Co-authored-by: Guilherme Santos <[email protected]>
  • Loading branch information
santos1709 and Guilherme Santos authored Mar 9, 2020
1 parent 2b38f5d commit b6ccb63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rrmngmnt/playbook_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PlaybookRunner(Service):
Class for working with and especially executing Ansible playbooks on hosts.
On your Host instance, it might be accessed (similar to other services) by
playbook property.
Each instance of this class represent a unique ansible run with an uuid.
Example:
rc,out, err = my_host.playbook.run('long_task.yml')
Expand Down Expand Up @@ -106,13 +107,14 @@ def _generate_default_inventory(self):
def run(
self, playbook, extra_vars=None, vars_files=None, inventory=None,
verbose_level=1, run_in_check_mode=False, ssh_common_args=None,
upload_playbook=True
):
"""
Run Ansible playbook on host
Args:
playbook (str): Path to playbook you want to execute (on your
machine)
playbook (str): Path to playbook you want to execute (the location
is determine by remote_playbook argument - default is local)
extra_vars (dict): Dictionary of extra variables that are to be
passed to playbook execution. They will be dumped to JSON file
and included using -e@ parameter
Expand All @@ -132,6 +134,8 @@ def run(
replace) the list of default options that Ansible uses when
calling ssh/sftp/scp. Example: ["-o StrictHostKeyChecking=no",
"-o UserKnownHostsFile=/dev/null"]
upload_playbook (bool): If the playbook is going to be uploaded
from the local machine (True - Default) or not (False)
Returns:
tuple: tuple of (rc, out, err)
Expand Down Expand Up @@ -172,7 +176,9 @@ def run(
)
)

self.cmd.append(self._upload_file(playbook))
if upload_playbook:
playbook = self._upload_file(playbook)
self.cmd.append(playbook)

self.logger.debug("Executing: {}".format(" ".join(self.cmd)))

Expand Down
23 changes: 23 additions & 0 deletions tests/test_playbook_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def fake_playbook(self, tmpdir):
fp.write(self.playbook_content)
return str(fp)

@pytest.fixture()
def fake_remote_playbook(self, fake_host):
fp = os.path.join(self.tmp_dir, self.playbook_name)
fake_host.fs.create_file(
content=self.playbook_content, path=str(fp)
)
return str(fp)

@pytest.fixture()
def playbook_runner(self, fake_host):
playbook_runner = PlaybookRunner(fake_host)
Expand Down Expand Up @@ -163,6 +171,21 @@ def test_basic_scenario(self, playbook_runner, fake_playbook):
)


class TestRemoteBasic(PlaybookRunnerBase):

files = {}

def test_remote_scenario(self, playbook_runner, fake_remote_playbook):
""" User has provided only remote playbook """
rc, _, _ = playbook_runner.run(
playbook=fake_remote_playbook, upload_playbook=False
)
assert not rc
assert self.check_files_on_host(
os.path.join(self.tmp_dir, PlaybookRunner.default_inventory_name)
)


class TestExtraVars(PlaybookRunnerBase):

files = {}
Expand Down

0 comments on commit b6ccb63

Please sign in to comment.