-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathansible_runner.py
87 lines (74 loc) · 2.92 KB
/
ansible_runner.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
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.playbook.play import Play
from ansible.inventory import Inventory
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
import logging
class ResultCallback(CallbackBase):
result = {}
def v2_runner_on_ok(self, result, **kwargs):
self.result = {'contacted': {result._host: result._result}}
def v2_runner_on_failed(self, result, **kwargs):
self.result = {'contacted': {result._host: result._result}}
def v2_runner_on_unreachable(self, result, **kwargs):
self.result = {'dark': result._host, 'result': result._result}
class Runner(object):
def __init__(self, inventory_file, module_name, module_args):
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader,
variable_manager=variable_manager,
host_list=inventory_file)
variable_manager.set_inventory(inventory)
hosts = [x.name for x in inventory.get_hosts()]
play_source = {
"name": "Ansible Play",
"hosts": hosts,
"gather_facts": "no",
"tasks": [{
"action": {
"module": module_name,
"args": module_args
}
}]
}
logging.info(play_source)
play = Play().load(play_source,
variable_manager=variable_manager,
loader=loader)
Options = namedtuple('Options', ['connection', 'module_path', 'forks',
'become', 'become_method',
'become_user', 'check'])
options = Options(connection='local',
module_path='',
forks=100,
become=None,
become_method=None,
become_user=None,
check=False)
self.inventory = inventory
self.variable_manager = variable_manager
self.loader = loader
self.play = play
self.options = options
self.passwords = {"vault_pass": 'secret'}
def run(self):
result = {}
results_callback = ResultCallback()
try:
tqm = TaskQueueManager(
inventory=self.inventory,
variable_manager=self.variable_manager,
loader=self.loader,
options=self.options,
passwords=self.passwords,
stdout_callback=results_callback
)
tqm.run(self.play)
finally:
result = results_callback.result
if tqm is not None:
tqm.cleanup()
return result