Skip to content

Instantly share code, notes, and snippets.

@geek182
Forked from viper233/ansible_api_playbook.py
Created November 17, 2016 19:12
Show Gist options
  • Select an option

  • Save geek182/4d115d434dba371003582a811f1a39f5 to your computer and use it in GitHub Desktop.

Select an option

Save geek182/4d115d434dba371003582a811f1a39f5 to your computer and use it in GitHub Desktop.

Revisions

  1. @viper233 viper233 created this gist Feb 26, 2016.
    33 changes: 33 additions & 0 deletions ansible_api_playbook.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/usr/bin/env python
    # stolen from http://stackoverflow.com/questions/27590039/running-ansible-playbook-using-python-api

    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='/tmp/hosts')
    playbook_path = '/tmp/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='local', module_path=None, forks=100, remote_user='stephen', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='root', verbosity=None, check=False)

    variable_manager.extra_vars = {'hosts': 'localhost'} # 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()