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
This commit is contained in:
David Moreau-Simard 2017-02-28 15:43:22 -05:00
parent 8b7af86908
commit 0d52f5b047
2 changed files with 27 additions and 2 deletions

View File

@ -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

View File

@ -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({