Hi all,

I am having trouble about using the ansible 2 api. Below is my code:

#!/usr/bin/env python

import os
import sys
import json
from collections import namedtuple

from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager

class AnsibleJobs:

    def __init__(self, data, logger):

        self.host = data['host']
        self.hostname = data['hostname']
        self.conn_name = data['conn_name']
        self.guest_ip4_address = data['guest_ip4_address']
        self.guest_ip4_address_for_restart = 
        self.guest_gw4_address = data['guest_gw4_address']
        self.host = data['host']
        self.hostname_hosts = data['hostname_hosts']
        self.playbook_path = data['hostname_playbook']
        self.logger = logger

        if not os.path.exists(self.playbook_path):
            self.logger.error('The playbook does not exist')

    def run(self):
        #import ipdb
        Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts',
                                         'syntax', 'connection', 'module_path', 
                                         'remote_user', 'private_key_file', 
                                         'ssh_extra_args', 'sftp_extra_args', 
'scp_extra_args', 'become',
                                         'become_method', 'become_user', 
'verbosity', 'check'])

        variable_manager = VariableManager()
        loader = DataLoader()

        options = Options(listtags=False, listtasks=False, listhosts=False, 
                          connection='paramiko', module_path=None, forks=100, 
                          private_key_file=None, ssh_common_args=None, 
                          sftp_extra_args=None, scp_extra_args=None, 
become=True, become_method='sudo',
                          become_user='root', verbosity=None, check=False)

        passwords = dict(conn_pass='xxx', become_pass='xxx')

        # create inventory and pass to var manager
        inventory = Inventory(loader=loader, variable_manager=variable_manager, 

        variable_manager.extra_vars = {'hosts': self.host, 'hostname': 
                                       'conn_name': self.conn_name, 
'guest_ip4_address': self.guest_ip4_address,
self.guest_gw4_address} # This can accomodate various other command line 


        self.logger.info("Running the playbook")


        pbex = PlaybookExecutor(playbooks=[self.playbook_path], 
                                options=options, passwords=passwords)

        ###cb = ResultCallback(self.logger)
        ###pbex._tqm._stdout_callback = cb
        results = pbex.run()
        ##except Exception as e:
            ###print e

        self.logger.info("Playbook run is ended")

When i run the python code, below is the error i got 

An exception occurred during task execution. To see the full traceback, use 
-vvv. The error was: TypeError: sequence item 0: expected string, NoneType found
fatal: []: FAILED! => {"failed": true, "msg": "Unexpected failure 
during module execution.", "stdout": ""}

Here is the main py file:

from runners.vcenterjobs import VcenterJobs
from settings_handler import parse
from runners.ansible.ansiblejobs import AnsibleJobs

if __name__ == "__main__":

    settings_data = parse("settings.yml")

    vc = VcenterJobs(settings_data)

    logger = vc.logger

    ##ansible = ANSIBLEJOBS(setings_data, logger)
    logger.info('Entering the Ansible playbook')
    ansible_job = AnsibleJobs(settings_data, logger)

Any idea about where the problem is?

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 
For more options, visit https://groups.google.com/d/optout.

Reply via email to