-
Notifications
You must be signed in to change notification settings - Fork 0
/
govnsible
68 lines (60 loc) · 2.49 KB
/
govnsible
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
#!/usr/bin/env python
import importlib
from elevate import elevate
from govnsiblecli.modules.base.utils.display import init_logger
from govnsiblecli.modules.base.utils import yaml_handler
from govnsiblecli.modules.base.play_handler import PlayHandler
from govnsiblecli.modules.base.inventory import InventoryBase
from govnsiblecli.modules.executor import scan_executor
logger = init_logger("root")
import argparse
def main():
parser = argparse.ArgumentParser(description='Playbook executor')
parser.add_argument('-i', type=str, help='Inventory filepath', required=False)
parser.add_argument('playbook', type=str, help='Path to playbook file')
args = parser.parse_args()
print(args.playbook)
print(args.i)
playbook_handler(args.playbook, args.i)
def playbook_handler(playbook_path, inventory_file):
playbook_content = yaml_handler.load_file(playbook_path)
inventory = InventoryBase(inventory_file)
inventory.init_inventory()
executor_list = scan_executor()
logger.info(f"Current inventory {inventory.groups} ")
for play in playbook_content:
executor_type = "ssh"
logger.info(play.get('name'))
play = PlayHandler(play, playbook_path)
connection_type = play.connection
hosts = play.hosts
if connection_type == 'local' and hosts == 'localhost':
logger.info('Executing playbook locally')
executor_type = 'local'
print(executor_type)
executor = [item for item in executor_list if item['executor_name'] == executor_type]
print(executor)
executor_py_name = executor[0].get('executor_py_name')
exec_mod_handler = importlib.import_module(executor_py_name)
executor = exec_mod_handler.GovnsibleExecutor(executor_type)
play_tasks = play.play_contents.get('tasks')
for task in play_tasks:
logger.info(task.get('name'))
become = task.get('become')
if become:
elevate(graphical=False)
module_reference = list(task.items())[1]
module_name = module_reference[0]
module_params = module_reference[1]
print(module_params)
print(module_name)
task_results = executor.execute_task(module_name,module_params)
logger.info(task_results)
#
#
# print("Before", play.play_facts)
# play.set_facts_from_play()
# print("After",play.play_facts)
# logger.info(playbook_content)
if __name__ == '__main__':
main()