-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.py
executable file
·140 lines (113 loc) · 4.36 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python2
# Based on: https://serversforhackers.com/running-ansible-2-programmatically
import os
import sys
from ansible.executor import playbook_executor
from ansible.inventory import Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class Options(object):
"""
Options class to replace Ansible OptParser
"""
def __init__(self, **kwargs):
props = (
'ask_pass', 'ask_sudo_pass', 'ask_su_pass', 'ask_vault_pass',
'become_ask_pass', 'become_method', 'become', 'become_user',
'check', 'connection', 'diff', 'extra_vars', 'flush_cache',
'force_handlers', 'forks', 'inventory', 'listhosts', 'listtags',
'listtasks', 'module_path', 'module_paths',
'new_vault_password_file', 'one_line', 'output_file',
'poll_interval', 'private_key_file', 'python_interpreter',
'remote_user', 'scp_extra_args', 'seconds', 'sftp_extra_args',
'skip_tags', 'ssh_common_args', 'ssh_extra_args', 'subset', 'sudo',
'sudo_user', 'syntax', 'tags', 'timeout', 'tree',
'vault_password_files', 'verbosity')
for p in props:
if p in kwargs:
setattr(self, p, kwargs[p])
else:
setattr(self, p, None)
class Runner(object):
def __init__(
self, playbook, display, hosts='hosts', options={}, passwords={},
vault_pass=None):
# Set options
self.options = Options()
for k, v in options.iteritems():
setattr(self.options, k, v)
# Set global verbosity
self.display = display
self.display.verbosity = self.options.verbosity
# Executor has its own verbosity setting
playbook_executor.verbosity = self.options.verbosity
# Gets data from YAML/JSON files
self.loader = DataLoader()
# Set vault password
if vault_pass is not None:
self.loader.set_vault_password(vault_pass)
elif 'VAULT_PASS' in os.environ:
self.loader.set_vault_password(os.environ['VAULT_PASS'])
# All the variables from all the various places
self.variable_manager = VariableManager()
if self.options.python_interpreter is not None:
self.variable_manager.extra_vars = {
'ansible_python_interpreter': self.options.python_interpreter
}
# Set inventory, using most of above objects
self.inventory = Inventory(
loader=self.loader, variable_manager=self.variable_manager,
host_list=hosts)
if len(self.inventory.list_hosts()) == 0:
# Empty inventory
self.display.error("Provided hosts list is empty.")
sys.exit(1)
self.inventory.subset(self.options.subset)
if len(self.inventory.list_hosts()) == 0:
# Invalid limit
self.display.error("Specified limit does not match any hosts.")
sys.exit(1)
self.variable_manager.set_inventory(self.inventory)
# Setup playbook executor, but don't run until run() called
self.pbex = playbook_executor.PlaybookExecutor(
playbooks=[playbook],
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=passwords)
def run(self):
# Run Playbook and get stats
self.pbex.run()
stats = self.pbex._tqm._stats
return stats
def main():
runner = Runner(
playbook='site.yaml',
hosts='hosts',
display=display,
options={
'subset': '~^localhost',
# 'become': True,
# 'become_method': 'sudo',
# 'become_user': 'root',
# 'private_key_file': '/path/to/the/id_rsa',
'tags': '',
'skip_tags': '',
# 'verbosity': 0,
},
# passwords={
# 'become_pass': 'sudo_password',
# 'conn_pass': 'ssh_password',
# },
# vault_pass='vault_password',
)
stats = runner.run()
# Maybe do something with stats here? If you want!
if __name__ == '__main__':
main()