Created
March 8, 2016 09:56
-
-
Save badri/27aa6cd777a13f171027 to your computer and use it in GitHub Desktop.
ansible 2.0 run
This file contains hidden or 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/python2 | |
from collections import namedtuple | |
from ansible.parsing.dataloader import DataLoader | |
from ansible.vars import VariableManager | |
from ansible.inventory import Inventory | |
from ansible.playbook.play import Play | |
from ansible.executor.task_queue_manager import TaskQueueManager | |
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'remote_user', 'private_key_file', 'ssh_common_args', 'sftp_extra_args', 'scp_extra_args', 'ssh_extra_args', 'verbosity']) | |
# initialize needed objects | |
variable_manager = VariableManager() | |
loader = DataLoader() | |
options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False, remote_user='root', private_key_file='', ssh_common_args='', sftp_extra_args='', scp_extra_args='', ssh_extra_args='', verbosity=1) | |
passwords = dict(vault_pass='secret') | |
# create inventory and pass to var manager | |
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost') | |
variable_manager.set_inventory(inventory) | |
# create play with tasks | |
play_source = dict( | |
name = "Ansible Play", | |
hosts = 'localhost', | |
gather_facts = 'no', | |
tasks = [ | |
dict(action=dict(module='shell', args='ls'), register='shell_out'), | |
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) | |
] | |
) | |
play = Play().load(play_source, variable_manager=variable_manager, loader=loader) | |
# actually run it | |
tqm = None | |
try: | |
tqm = TaskQueueManager( | |
inventory=inventory, | |
variable_manager=variable_manager, | |
loader=loader, | |
options=options, | |
passwords=passwords, | |
stdout_callback='default', | |
) | |
result = tqm.run(play) | |
finally: | |
if tqm is not None: | |
tqm.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment