I am having trouble about finding the right syntax for running a playbook
with ansible api below is my code
#!/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
from runners.vcenterjobs import VcenterJobs
from ansible.inventory import Host
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']
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.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.satellite_playbook_path = data['satellite_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='',
become_user='', verbosity=1, check='')
passwords = dict(conn_pass='foo', become_pass='foo')
#test
#self.hostname_hosts = ['10.35.101.247',]
#self.host = '10.35.101.247'
# 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")
I am getting no hosts matched error.
host_list is my inventory file path. Content is:
[local]
127.0.0.1 ansible_python_interpreter=python
[default]
10.35.101.247
self.host is 'default' the rest are the variables that i am using at the
playbook.
This the output:
✗ python cc.py
PLAY [Set the hostname] ********************************************************
TASK [setup] *******************************************************************
fatal: [10.35.101.247]: FAILED! => {"failed": true, "msg": "module (setup) is
missing interpreter line"}
Seems i am doing defing host_list and hosts part wrong. Anyone can show me my
mistake or can show me a working tested example of v2 API with python?
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/349a5d14-9d08-4bf8-977f-e5891881baa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.