Skip to content

Commit

Permalink
Add a generic option to configure Ansible callbacks in the agent
Browse files Browse the repository at this point in the history
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
David Moreau-Simard committed Mar 17, 2017
1 parent 8b7af86 commit 0d52f5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion heat-config-ansible/install.d/hook-ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion tests/test_hook_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}})

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 0d52f5b

Please sign in to comment.