diff --git a/rrmngmnt/playbook_runner.py b/rrmngmnt/playbook_runner.py index cd3a19c..d07ba2c 100644 --- a/rrmngmnt/playbook_runner.py +++ b/rrmngmnt/playbook_runner.py @@ -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') @@ -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 @@ -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) @@ -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))) diff --git a/tests/test_playbook_runner.py b/tests/test_playbook_runner.py index 66d7f90..246a24c 100644 --- a/tests/test_playbook_runner.py +++ b/tests/test_playbook_runner.py @@ -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) @@ -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 = {}