Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying a particular callback to be used in playbook

I have created different playbooks for different operations in ansible.

And I have also created different Callback Scripts for different kinds of Playbooks (And packaged them with Ansible and installed).

The playbooks will be called from many different scripts/cron jobs.

Now, is it possible to specify a particular callback script to be called for a particular playbook? (Using a command line argument probably?)

What's happening right now is, all the Callback scripts are called for each playbook.

I cannot put the callback script relative to the location/folder of the playbook because it's already packaged inside the ansible package. Also, all the playbooks are in the same location too.

I am fine with modifying a bit of ansible source code to accommodate it too if needed.

like image 662
Codebender Avatar asked Sep 09 '15 04:09

Codebender


1 Answers

After going through the code of Ansible, I was able to solve it with the below...

In each callback_plugin, you can specify self.disabled = True and the callback wont be called at all...

Also, which calling a playbook, there's an option to parsing extra arguments as key=value pairs. It will be part of the playbook object as extra_vars field.

So I did something like this in my callback_plugin.

def playbook_on_start(self):
    callback_plugins = self.playbook.extra_vars.get('callback_plugin', '') // self.playbook is populated in your callback plugin by Ansible.
    if callback_plugins not in ['email_reporter', 'all']:
        self.disabled = True

And while calling the playbook, I can do something like,

ansible-playbook -e callback_plugin=email_reporter //Note -e is the argument prefix key for extra vars.
like image 73
Codebender Avatar answered Oct 14 '22 01:10

Codebender