Here is my my playbook.  I am also running ansible version 2.4.2.0
---
- hosts: all
  tasks:
    - name: Run setup
      setup:
#      register: variables




On Friday, April 13, 2018 at 11:39:58 AM UTC-4, tommcf64 wrote:
>
> 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
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bd57c5f1-b221-417f-8edd-94e3ecb54579%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to