ansible 2 api usage error

68 views
Skip to first unread message

Oğuz Yarımtepe

unread,
Oct 21, 2016, 8:45:40 AM10/21/16
to Ansible Project
Below is my code sample to run a simple playbook on a host

#!/usr/bin/env python

import os
import sys
import tempfile
import time

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



class AnsibleJobs:

def __init__(self, data, logger, ipam):

self.data = data

self.host = data['host']
self.hostname = data['hostname']
self.conn_name = data['conn_name']
if ipam.ip:
self.guest_ip4_address = ipam.restart_ip
else:
self.guest_ip4_address = data['guest_ip4_address']

if ipam.ip:
self.guest_ip4_address_for_restart = ipam.ip
else:
self.guest_ip4_address_for_restart = data['guest_ip4_address_for_restart']

if ipam.gw:
self.guest_gw4_address = ipam.gw
else:
self.guest_gw4_address = data['guest_gw4_address']

self.template_ip = data['template_ip']

self.host = data['host']
self.hostname_hosts = data['hostname_hosts']
self.hostname_playbook_path = data['hostname_playbook']
self.ip_playbook_path = data['ip_playbook']
self.logger = logger

if not os.path.exists(self.hostname_playbook_path) or not os.path.exists(self.ip_playbook_path):
self.logger.error('The playbook does not exist')
sys.exit()

def run(self):
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='', listtasks='', listhosts='', syntax='',
connection='paramiko', module_path='', forks=5, remote_user='root',
private_key_file='', ssh_common_args='', ssh_extra_args='',
sftp_extra_args='', scp_extra_args='', become=True, become_method='sudo',
become_user='root', verbosity='', check='')

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

# 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, 'template_ip': self.template_ip} # This can accomodate various other command line arguments.`

variable_manager.set_inventory(inventory)


self.logger.info("Running the playbook: changing the hostname and setting ip")

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

results = pbex.run()


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


When i run this, this is the below error i got:


PLAY [Set the hostname] ********************************************************

TASK [setup] *******************************************************************
fatal: [10.35.75.247]: FAILED! => {"failed": true, "msg": "module (setup) is missing interpreter line"}

NO MORE HOSTS LEFT *************************************************************
	to retry, use: --limit @/Users/oyarimtepe/Dev/vmware/command-center/register/playbook_hostname.retry

PLAY RECAP *********************************************************************
10.35.75.247               : ok=0    changed=0    unreachable=0    failed=1   


The playbook runs with ansible-playbook without errors.


here is ansible.cfg

[defaults]
hostfile = hosts
log_path = /Users/oyarimtepe/Dev/vmware/command-center/cc-cli/runners/ansible/ansible.log
callback_plugins = /Users/oyarimtepe/Dev/vmware/command-center/cc-cli/runners/ansible/plugins/callback_plugins:~/.ansible/plugins/callback_plugins/:/usr/share/ansible_plugins/callback_plugins
pipelining=False
host_key_checking=False

I am using ansible 2.1.1



Any idea?
Reply all
Reply to author
Forward
0 new messages