From 4a732c1e9f55b2ba836427daa9f605a5947db63c Mon Sep 17 00:00:00 2001 From: Tin Tvrtkovic Date: Thu, 18 Jul 2013 22:45:18 +0200 Subject: [PATCH] Enabled the use of extra vars in playbook file paths when including playbooks from other playbooks. --- lib/ansible/playbook/__init__.py | 2 +- test/TestPlayBook.py | 27 ++++++++++++++++++++++++++- test/playbook-templated-includer.yml | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/playbook-templated-includer.yml diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index f554bd3e31..8ae6d56ca2 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -130,7 +130,7 @@ class PlayBook(object): self.basedir = os.path.dirname(playbook) or '.' utils.plugins.push_basedir(self.basedir) - vars = {} + vars = extra_vars.copy() if self.inventory.basedir() is not None: vars['inventory_dir'] = self.inventory.basedir() self.filename = playbook diff --git a/test/TestPlayBook.py b/test/TestPlayBook.py index 5b44302af0..74ceb1e8f2 100644 --- a/test/TestPlayBook.py +++ b/test/TestPlayBook.py @@ -119,7 +119,8 @@ class TestPlaybook(unittest.TestCase): filename = os.path.join(self.stage_dir, filename) return filename - def _run(self, test_playbook, host_list='test/ansible_hosts'): + def _run(self, test_playbook, host_list='test/ansible_hosts', + extra_vars=None): ''' run a module and get the localhost results ''' # This ensures tests are independent of eachother global EVENTS @@ -135,6 +136,7 @@ class TestPlaybook(unittest.TestCase): timeout = 5, remote_user = self.user, remote_pass = None, + extra_vars = extra_vars, stats = ans_callbacks.AggregateStats(), callbacks = self.test_callbacks, runner_callbacks = self.test_callbacks @@ -216,6 +218,29 @@ class TestPlaybook(unittest.TestCase): assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True) + def test_templated_includes(self): + pb = os.path.join(self.test_dir, 'playbook-templated-includer.yml') + actual = self._run(pb, extra_vars={ 'dir': self.test_dir }) + + # if different, this will output to screen + print "**ACTUAL**" + actual_json = utils.jsonify(actual, format=True) + print actual_json + expected = { + "localhost": { + "changed": 0, + "failures": 0, + "ok": 2, + "skipped": 0, + "unreachable": 0 + } + } + expected_json = utils.jsonify(expected, format=True) + print "**EXPECTED**" + print expected_json + + assert actual_json == expected_json + def test_task_includes(self): pb = os.path.join(self.test_dir, 'task-includer.yml') actual = self._run(pb) diff --git a/test/playbook-templated-includer.yml b/test/playbook-templated-includer.yml new file mode 100644 index 0000000000..5f484c3d48 --- /dev/null +++ b/test/playbook-templated-includer.yml @@ -0,0 +1,2 @@ +--- +- include: "{{dir}}/playbook-included.yml variable=foobar" \ No newline at end of file