Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ansible, how to access extra arguments supplied to playbook in callback plugins?

Tags:

python

ansible

I'm trying to write a callback plugin that will time each task. That wasn't the tough part. But I've a series of playbooks that run to provision and configure an instance. For each run of a sequence, I want to pass a runID to each playbook and that runID is constant for one run, something like this:

ansible-playbook -e "runID=seq198837" provision.yml
ansible-playbook -e "runID=seq198837" build.yml
ansible-playbook -e "runID=seq198837" deploy.yml

The callback plugin I'm writing has to collect time information for each task in a plugin, label them with the runID and deposit them to a logging agent.

I've tried several hooks and events in the callback plugin but am unable to get the runID extra argument passed to the playbook. Is there any way to access that variable?

like image 449
kumar Avatar asked Oct 17 '25 23:10

kumar


1 Answers

You can get it from play object, and you can access that inside on_play_start.

Like this:

def v2_playbook_on_play_start(self, play):
    vm = play.get_variable_manager()
    extra_vars = vm.extra_vars
    self.run_id = extra_vars['runID']

And then, when you dump your results somewhere in on_stats (for example), you can refer self.run_id to access your runID extra variable.

like image 117
Konstantin Suvorov Avatar answered Oct 20 '25 12:10

Konstantin Suvorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!