Ansible version : 2.0.0.1
I've been looking around quite a bit now, and most documentation I find is either incomplete or deprecated (this post is for version 1.8.4, ie)
I'm trying to launch an Ansible playbook through the Python API. Ansible's documentation seem to be showing how to generate and play tasks, but not how to load and run a playbook yml file. I've been digging into the code to try to understand how to launch it, and I think I've done some progress, but I'm really hitting a wall. Here's what I have so far :
def createcluster(region, environment, cluster):
Options = namedtuple('Options', ['region','env', 'cluster'])
# initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(region=region, env=environment, cluster=cluster)
options.listhosts = False
vault_password = getpass.getpass('Enter vault password :')
passwords = dict(vault_pass=vault_password)
#Getting hosts
hostsread = open('provisioning/inventory/hosts','r')
hosts = hostsread.read()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts)
variable_manager.set_inventory(inventory)
#Create and load the playbook file
playbook = Playbook(loader)
playbook.load('provisioning/cluster.yml', variable_manager,loader)
#Create an executor to launch the playbook ?
executor = None
executor = PlaybookExecutor(playbook,inventory,variable_manager,loader,options,passwords)
try:
result = executor.run()
finally:
if executor is not None:
executor.cleanup()
I'm not sure at all about the executor part, and I keep getting a "AttributeError: 'Options' object has no attribute 'listhosts'" error when I try to launch the code (weirdly enough as it should just ignore its absence, I think (line 60))
How am I supposed to load a YML file and launch it through the Python API ? Am I on the good path or did I lose myself ? Why isn't Ansible better documented ? Why would 42 be the answer to 7*7 ?