From 0d52f5b04737ca66418451c1983c02f0ab58f638 Mon Sep 17 00:00:00 2001 From: David Moreau-Simard Date: Tue, 28 Feb 2017 15:43:22 -0500 Subject: [PATCH] Add a generic option to configure Ansible callbacks in the agent Ansible callbacks [1] are used to hook into Ansible events such as running tasks. They can be used to alter the behavior of Ansible like providing helpful console output for tasks. [1]: http://docs.ansible.com/ansible/dev_guide/developing_plugins.html#callback-plugins Change-Id: I7e2c041ca359ab63140102c9d4a22914d9add228 --- heat-config-ansible/install.d/hook-ansible.py | 9 ++++++++- tests/test_hook_ansible.py | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/heat-config-ansible/install.d/hook-ansible.py b/heat-config-ansible/install.d/hook-ansible.py index b537e11..419c281 100755 --- a/heat-config-ansible/install.d/hook-ansible.py +++ b/heat-config-ansible/install.d/hook-ansible.py @@ -53,6 +53,7 @@ def main(argv=sys.argv): tags = c['options'].get('tags') skip_tags = c['options'].get('skip_tags') modulepath = c['options'].get('modulepath') + callback_plugins = c['options'].get('callback_plugins') fn = os.path.join(WORKING_DIR, '%s_playbook.yaml' % c['id']) vars_filename = os.path.join(WORKING_DIR, '%s_variables.json' % c['id']) @@ -89,10 +90,16 @@ def main(argv=sys.argv): cmd.insert(3, '--module-path') cmd.insert(4, modulepath) + env = os.environ.copy() + if callback_plugins: + env.update({ + 'ANSIBLE_CALLBACK_PLUGINS': callback_plugins + }) + log.debug('Running %s' % (' '.join(cmd),)) try: subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE, env=env) except OSError: log.warn("ansible not installed yet") return diff --git a/tests/test_hook_ansible.py b/tests/test_hook_ansible.py index 0041384..d5a7e30 100644 --- a/tests/test_hook_ansible.py +++ b/tests/test_hook_ansible.py @@ -58,6 +58,11 @@ class HookAnsibleTest(common.RunScriptTest): 'tags': 'abc,def'}, }) + data_callback_plugins = data.copy() + data_callback_plugins.update({ + 'options': {'callback_plugins': '/usr/lib/python/foo'} + }) + data_skip_tags = data.copy() data_skip_tags.update({'options': {'skip_tags': 'abc,def'}}) @@ -133,14 +138,25 @@ class HookAnsibleTest(common.RunScriptTest): '/opt/ansible:/usr/share/ansible', '--skip-tags', 'abc,def']) - def _hook_run(self, data=None, options=None): + def test_hook_with_callback_plugins(self): + self.assertTrue('ANSIBLE_CALLBACK_PLUGINS' not in self.env) + state = self._hook_run(data=self.data_callback_plugins) + opt = self.data_callback_plugins['options']['callback_plugins'] + self.assertEqual(self.env['ANSIBLE_CALLBACK_PLUGINS'], opt) + self.assertEqual(state['env']['ANSIBLE_CALLBACK_PLUGINS'], opt) + + def _hook_run(self, data=None, options=None): self.env.update({ 'TEST_RESPONSE': json.dumps({ 'stdout': 'ansible success', 'stderr': 'thing happened', }), }) + if data is not None and 'callback_plugins' in data['options']: + self.env.update({ + 'ANSIBLE_CALLBACK_PLUGINS': data['options']['callback_plugins'] + }) returncode, stdout, stderr = self.run_cmd( [self.hook_path], self.env, json.dumps(data or self.data)) @@ -178,6 +194,8 @@ class HookAnsibleTest(common.RunScriptTest): with open(ansible_playbook) as f: self.assertEqual('the ansible playbook', f.read()) + return state + def test_hook_inventory(self): self.env.update({