Add option to pass tags to the ansible hook

Currently we don't support options for the ansible hook, but it would
be useful to enable passing tags, like we already do for the puppet
hook.

Change-Id: Idf7e3aa520fd40a2c4c00b2d26c3c608059bb51d
This commit is contained in:
Steven Hardy 2016-11-03 17:42:51 +00:00 committed by Thomas Herve
parent f227bd609a
commit 6fb26a228c
2 changed files with 35 additions and 11 deletions

View File

@ -50,6 +50,8 @@ def main(argv=sys.argv):
for input in c['inputs']:
variables[input['name']] = input.get('value', '')
tags = c['options'].get('tags')
fn = os.path.join(WORKING_DIR, '%s_playbook.yaml' % c['id'])
vars_filename = os.path.join(WORKING_DIR, '%s_variables.json' % c['id'])
heat_outputs_path = os.path.join(OUTPUTS_DIR, c['id'])
@ -75,6 +77,9 @@ def main(argv=sys.argv):
'--extra-vars',
'@%s' % vars_filename
]
if tags:
cmd.insert(-1, '--tags')
cmd.insert(-1, tags)
log.debug('Running %s' % (' '.join(cmd),))
try:
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE,

View File

@ -27,6 +27,19 @@ class HookAnsibleTest(common.RunScriptTest):
'id': '1234',
'name': 'fake_resource_name',
'group': 'ansible',
'options': {},
'inputs': [
{'name': 'foo', 'value': 'bar'},
{'name': 'another', 'value': 'input'}
],
'config': 'the ansible playbook'
}
data_tags = {
'id': '1234',
'name': 'fake_resource_name_tags',
'group': 'ansible',
'options': {'tags': 'abc,def'},
'inputs': [
{'name': 'foo', 'value': 'bar'},
{'name': 'another', 'value': 'input'}
@ -60,6 +73,12 @@ class HookAnsibleTest(common.RunScriptTest):
})
def test_hook(self):
self._hook_run()
def test_hook_tags(self):
self._hook_run(data=self.data_tags, options=['--tags', 'abc,def'])
def _hook_run(self, data=None, options=None):
self.env.update({
'TEST_RESPONSE': json.dumps({
@ -68,7 +87,7 @@ class HookAnsibleTest(common.RunScriptTest):
}),
})
returncode, stdout, stderr = self.run_cmd(
[self.hook_path], self.env, json.dumps(self.data))
[self.hook_path], self.env, json.dumps(data or self.data))
self.assertEqual(0, returncode, stderr)
self.assertEqual({
@ -81,16 +100,16 @@ class HookAnsibleTest(common.RunScriptTest):
ansible_playbook = self.working_dir.join('1234_playbook.yaml')
vars_filename = self.working_dir.join('1234_variables.json')
self.assertEqual(
[
self.fake_tool_path,
'-i',
'localhost,',
ansible_playbook,
'--extra-vars',
'@%s' % vars_filename
],
state['args'])
expected_args = [
self.fake_tool_path,
'-i',
'localhost,',
ansible_playbook,
'--extra-vars']
if options:
expected_args += options
expected_args.append('@%s' % vars_filename)
self.assertEqual(expected_args, state['args'])
# Write 'variables' to file
variables = self.json_from_file(vars_filename)