How to use Ansible 2.0 Python API to run a Playbook?

14,145

The documentation is surprisingly lacking and you'll have to get started here

That being said, here is a quick script I hacked together that manages to run a playbook.

#!/usr/bin/env python

import os
import sys
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

variable_manager = VariableManager()
loader = DataLoader()

inventory = Inventory(loader=loader, variable_manager=variable_manager,  host_list='/home/slotlocker/hosts2')
playbook_path = '/home/slotlocker/ls.yml'

if not os.path.exists(playbook_path):
    print '[INFO] The playbook does not exist'
    sys.exit()

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'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='slotlocker', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='root', verbosity=None, check=False)

variable_manager.extra_vars = {'hosts': 'mywebserver'} # This can accomodate various other command line arguments.`

passwords = {}

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

results = pbex.run()
Share:
14,145
ddro
Author by

ddro

BY DAY: Professional Nerd. BY NIGHT: Less professional Nerd.

Updated on June 16, 2022

Comments

  • ddro
    ddro almost 2 years

    I'm trying to write a python script which will call existing Ansible playbooks as it goes (because I want to loop over a list of plays while looping over a list of variables).

    This post explains it very well, for ansible pre-2.0: Running ansible-playbook using Python API

    This doc explains it very well if you're writing a new playbook in your script: http://docs.ansible.com/ansible/developing_api.html

    But I don't see how to call an existing playbook using Python API 2.0, and ansible.runner no longer works.

    Help me, Stackoverflow-Wan Kenobi. You're my only hope.

  • ddro
    ddro about 8 years
    Thanks Phani! I'll try this out later tonight.
  • mannoj
    mannoj over 7 years
    @Phani - I'm stuck here - "Timeout (3s) waiting for privilege escalation prompt: "
  • oz123
    oz123 about 6 years
    The documentation is surprisingly lacking , up for that! ansible is really badly documented (despite the large amount of documentation)...
  • Dag Wieers
    Dag Wieers about 6 years
    @Oz123 The API is not documented because it's not meant to be used and is not considered to be a stable interface. YMMV
  • Vijay
    Vijay about 5 years
    This is no longer working with ansible 2.7 . @phani-kumar is there any way we can pass the playbook path to execute instead of tasks