Skip to content
Permalink
Browse files

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
  • Loading branch information...
dmsimard committed Feb 28, 2017
1 parent 8b7af86 commit 0d52f5b04737ca66418451c1983c02f0ab58f638
Showing with 27 additions and 2 deletions.
  1. +8 −1 heat-config-ansible/install.d/hook-ansible.py
  2. +19 −1 tests/test_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
@@ -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 @@ def test_hook_skip_tags_modulepath(self):
'/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 @@ def _hook_run(self, data=None, options=None):
with open(ansible_playbook) as f:
self.assertEqual('the ansible playbook', f.read())

return state

def test_hook_inventory(self):

self.env.update({

0 comments on commit 0d52f5b

Please sign in to comment.
You can’t perform that action at this time.