ansible api 2 usage

234 views
Skip to first unread message

Oğuz Yarımtepe

unread,
Sep 19, 2016, 1:18:01 PM9/19/16
to Ansible Project
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 = data['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')
sys.exit()

def run(self):
#import ipdb
#ipdb.set_trace()
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts',
'syntax', 'connection', 'module_path', 'forks',
'remote_user', 'private_key_file', 'ssh_common_args',
'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, syntax=False,
connection='paramiko', module_path=None, forks=100, remote_user='root',
private_key_file=None, ssh_common_args=None, ssh_extra_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, host_list=self.hostname_hosts)


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

variable_manager.set_inventory(inventory)


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



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

###cb = ResultCallback(self.logger)
###pbex._tqm._stdout_callback = cb
###try:
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: [10.35.101.247]: 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)
ansible_job.run()


Any idea about where the problem is?

Matt Martz

unread,
Sep 19, 2016, 1:49:11 PM9/19/16
to ansible...@googlegroups.com
I believe your ssh_*_args all need to be defaulted to an empty string instead of `None`.

--
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+unsubscribe@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/5ec14e0a-cdf6-4fbe-b554-89ead78b19bc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages