How to pass data to my callback

Hi All,
My application accepts a request and gives back a job id(my_custom_job_id) so that i can process it in background. The user would then keep polling this my_custom_job_id using a REST Api which queries the database table and get details.

Application triggers the playbook using the python api. Now since there are many tasks and each might take a long time, i need to keep the user informed of what is going on. So i have created a Job and Tasks table and idea is to create the Job before initiating the playbook.run() . Now for me to keep updating the state of the tasks i need to implement the callback plugin and here i can insert the tasks to the database and keep updating the states. Now problem is how do i get the data(my_custom_job_id) i defined in the calling code where i ran playbook.run()

I come from a Java background and am new to Python , so that might also be a reason why i am not getting it :frowning:

#(i have created this in the calling code jobapi.createjob(my_custom_job_id ))

class CallbackModule(object):
“”"
logs playbook results, per host, in /var/log/ansible/hosts
“”"

def init(self):
self.stats = {}
self.task_id = None

def on_any(self, *args, **kwargs):
pass

def runner_on_failed(self, host, res, ignore_errors=False):
if type(self.task_id) is str:
print "Task name "+ self.task.name
print "Task id "+self.task_id
log(host, ‘FAILED’, res)

def runner_on_ok(self, host, res):
if type(self.task_id) is str:
print "Task name "+ self.task.name
print "Task id "+self.task_id
log(host, ‘OK’, res)

def playbook_on_task_start(self, name, is_conditional):
“”"
Logs the start of each task
“”"

HOW DO I GET THE my_custom_job_id WHICH I DEFINED IN MY PYTHON CODE Calling API playbook.run()

task_id=str(randint(2,9))

jobapi.addTask(my_custom_job_id, task_id,name)

self.task_id = task_id

Set an environment variable for your job id before calling playbook.run():

os.environ[‘MY_JOB_ID’] = str(job_id)

Then use the variable in your callback to get the current job id:

jobapi.addTask(os.environ[‘MY_JOB_ID’], task_id, name)

There’s also this nifty product called Ansible Tower (http://www.ansible.com/tower) that does the same thing and much more, including a REST API for launching and monitoring jobs.

Thanks Chris, Both your approach 1>Environment variable and 2>Setting properties on pb.job_id work , Thanks !