Created
February 26, 2016 22:29
-
-
Save viper233/69dfe7943b076f0d79db to your computer and use it in GitHub Desktop.
Example using Ansible API 2.0 to run a playbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
No it does not, the API has changed.
Look at this document https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this actually work for you? I'm seeing:
./test2.py
Traceback (most recent call last):
File "./test2.py", line 10, in
from ansible.inventory import Inventory
ImportError: cannot import name Inventory