# How to pass data to my callback

**URL:** <https://forum.ansible.com/t/how-to-pass-data-to-my-callback/16944>\
**Category:** Ansible Developer\
**Created:** [December 2, 2014, 11:00pm UTC](https://forum.ansible.com/t/how-to-pass-data-to-my-callback/16944 "2014-12-02T23:00:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ritesh\_Shetty](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Ritesh\_Shetty](https://forum.ansible.com/u/Ritesh_Shetty)\
**Post date:** [December 2, 2014, 11:00pm UTC](https://forum.ansible.com/t/how-to-pass-data-to-my-callback/16944/1 "2014-12-02T23:00:49Z")

</div>

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 ☹

#(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](http://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](http://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

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [December 3, 2014, 3:50am UTC](https://forum.ansible.com/t/how-to-pass-data-to-my-callback/16944/2 "2014-12-03T03:50:05Z")

</div>

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](http://www.ansible.com/tower)) that does the same thing and much more, including a REST API for launching and monitoring jobs.

---

<div class="post-metadata">

**Author:** ![Ritesh\_Shetty](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Ritesh\_Shetty](https://forum.ansible.com/u/Ritesh_Shetty)\
**Post date:** [December 4, 2014, 4:55am UTC](https://forum.ansible.com/t/how-to-pass-data-to-my-callback/16944/3 "2014-12-04T04:55:12Z")

</div>

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