# Getting two results with custom callbacks

**URL:** <https://forum.ansible.com/t/getting-two-results-with-custom-callbacks/27273>\
**Category:** Ansible Project\
**Created:** [April 13, 2018, 2:34pm UTC](https://forum.ansible.com/t/getting-two-results-with-custom-callbacks/27273 "2018-04-13T14:34:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommcf64](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tommcf64](https://forum.ansible.com/u/tommcf64)\
**Post date:** [April 13, 2018, 2:34pm UTC](https://forum.ansible.com/t/getting-two-results-with-custom-callbacks/27273/1 "2018-04-13T14:34:14Z")

</div>

Hi,  
I am attempting to use the Ansible API to run and playbook and insert the facts into a mongodb database but I am getting two documents for each machine. I am a newbie and unfamiliar with the API. Can anyone shed my insight into why my code creates two outputs, I would greatly appreciate the help.

Here is the code:

`

#!/usr/bin/env python

import json  
import shutil  
from collections import namedtuple  
from ansible.parsing.dataloader import DataLoader  
from ansible.vars.manager import VariableManager  
from ansible.inventory.manager import InventoryManager  
from ansible.executor.playbook\_executor import PlaybookExecutor  
from ansible.playbook.play import Play  
from ansible.executor.task\_queue\_manager import TaskQueueManager  
from ansible.plugins.callback import CallbackBase  
import ansible.constants as C  
import pymongo

connection = pymongo.MongoClient(“mongodb://\<ip\_address\>:27017”)  
db = connection.infrastructure  
servers = db.servers  
one\_server = dict()

class ResultCallback(CallbackBase):  
“”"A sample callback plugin used for performing an action as results come in

If you want to collect all results into a single object for processing at  
the end of the execution, look into utilizing the `json` callback plugin  
or writing your own custom callback plugin  
“”"  
def v2\_runner\_on\_ok(self, result, \*\*kwargs):  
“”"Print a json representation of the result

This method could store the result in an instance attribute for retrieval later  
“”"  
host = result.\_host  
#one\_server = ((json.dumps({host.name: result.\_result})))  
one\_server = ({host.name: result.\_result})  
#print(one\_server)  
servers.insert(one\_server,check\_keys=False)

Options = namedtuple(‘Options’, [‘connection’, ‘module\_path’, ‘forks’, ‘become’, ‘become\_method’, ‘become\_user’, ‘check’, ‘diff’])

# initialize needed objects

loader = DataLoader()  
options = Options(connection=‘local’, module\_path=[‘/path/to/mymodules’], forks=100, become=None, become\_method=None, become\_user=None, check=False,  
diff=False)  
passwords = dict(vault\_pass=‘secret’)

# Instantiate our ResultCallback for handling results as they come in

results\_callback = ResultCallback()

# create inventory and pass to var manager

# use path to host config file as source or hosts in a comma separated string

inventory = InventoryManager(loader=loader, sources=‘/etc/ansible/single/playbooks/hosts’)  
variable\_manager = VariableManager(loader=loader, inventory=inventory)

Options = namedtuple(‘Options’,  
[‘connection’,  
‘remote\_user’,  
‘ask\_sudo\_pass’,  
‘verbosity’,  
‘ack\_pass’,  
‘module\_path’,  
‘forks’,  
‘become’,  
‘become\_method’,  
‘become\_user’,  
‘check’,  
‘listhosts’,  
‘listtasks’,  
‘listtags’,  
‘syntax’,  
‘sudo\_user’,  
‘sudo’,  
‘diff’])  
options = Options(connection=‘smart’,  
remote\_user=None,  
ack\_pass=None,  
sudo\_user=None,  
forks=5,  
sudo=None,  
ask\_sudo\_pass=False,  
verbosity=5,  
module\_path=None,  
become=None,  
become\_method=None,  
become\_user=None,  
check=False,  
diff=False,  
listhosts=None,  
listtasks=None,  
listtags=None,  
syntax=None)

pbe = PlaybookExecutor(playbooks=[‘/etc/ansible/single/playbooks/setup.yml’],inventory=inventory,  
variable\_manager=variable\_manager,  
loader=loader,options=options,passwords=passwords)  
results\_callback = ResultCallback()  
pbe.\_tqm.\_stdout\_callback = results\_callback  
return\_code = pbe.run()

`

Thank you,  
Tom

---

<div class="post-metadata">

**Author:** ![tommcf64](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tommcf64](https://forum.ansible.com/u/tommcf64)\
**Post date:** [April 13, 2018, 7:14pm UTC](https://forum.ansible.com/t/getting-two-results-with-custom-callbacks/27273/2 "2018-04-13T19:14:12Z")

</div>

Here is my my playbook. I am also running ansible version 2.4.2.0  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 14, 2018, 9:26am UTC](https://forum.ansible.com/t/getting-two-results-with-custom-callbacks/27273/3 "2018-04-14T09:26:28Z")

</div>

Default Ansible is running gather\_facts, so when you are using the setup module also it with gather\_facts to times on each host.
