jumpserver/apps/ops/ansible/runner.py

88 lines
2.7 KiB
Python
Raw Normal View History

2022-09-30 10:49:45 +00:00
import uuid
2022-10-08 11:12:04 +00:00
import os
2017-03-05 12:53:24 +00:00
2022-10-08 11:12:04 +00:00
import ansible_runner
2022-09-30 10:49:45 +00:00
from django.conf import settings
2022-10-08 11:12:04 +00:00
2022-09-30 10:49:45 +00:00
from .callback import DefaultCallback
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
class AdHocRunner:
cmd_modules_choices = ('shell', 'raw', 'command', 'script', 'win_shell')
cmd_blacklist = [
"reboot", 'shutdown', 'poweroff', 'halt', 'dd', 'half', 'top'
]
2017-12-06 10:31:51 +00:00
def __init__(self, inventory, module, module_args='', pattern='*', project_dir='/tmp/', extra_vars={}):
2022-09-30 10:49:45 +00:00
self.id = uuid.uuid4()
2017-12-10 16:29:25 +00:00
self.inventory = inventory
2022-09-30 10:49:45 +00:00
self.pattern = pattern
self.module = module
self.module_args = module_args
self.project_dir = project_dir
self.cb = DefaultCallback()
self.runner = None
self.extra_vars = extra_vars
2022-09-30 10:49:45 +00:00
def check_module(self):
if self.module not in self.cmd_modules_choices:
return
if self.module_args and self.module_args.split()[0] in self.cmd_blacklist:
raise Exception("command not allowed: {}".format(self.module_args[0]))
def run(self, verbosity=0, **kwargs):
self.check_module()
if verbosity is None and settings.DEBUG:
verbosity = 1
2022-10-08 11:12:04 +00:00
if not os.path.exists(self.project_dir):
os.mkdir(self.project_dir, 0o755)
2022-09-30 10:49:45 +00:00
ansible_runner.run(
extravars=self.extra_vars,
2022-09-30 10:49:45 +00:00
host_pattern=self.pattern,
private_data_dir=self.project_dir,
2017-03-05 15:30:14 +00:00
inventory=self.inventory,
2022-09-30 10:49:45 +00:00
module=self.module,
module_args=self.module_args,
verbosity=verbosity,
event_handler=self.cb.event_handler,
status_handler=self.cb.status_handler,
**kwargs
2017-12-06 10:31:51 +00:00
)
2022-09-30 10:49:45 +00:00
return self.cb
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
class PlaybookRunner:
2022-10-12 10:08:57 +00:00
def __init__(self, inventory, playbook, project_dir='/tmp/', callback=None):
2022-09-30 10:49:45 +00:00
self.id = uuid.uuid4()
2017-12-10 16:29:25 +00:00
self.inventory = inventory
2022-09-30 10:49:45 +00:00
self.playbook = playbook
self.project_dir = project_dir
2022-10-12 10:08:57 +00:00
if not callback:
callback = DefaultCallback()
self.cb = callback
2017-03-06 15:34:54 +00:00
2022-09-30 10:49:45 +00:00
def run(self, verbosity=0, **kwargs):
if verbosity is None and settings.DEBUG:
verbosity = 1
2018-03-30 14:03:43 +00:00
2022-09-30 10:49:45 +00:00
ansible_runner.run(
private_data_dir=self.project_dir,
2017-03-05 15:30:14 +00:00
inventory=self.inventory,
2022-09-30 10:49:45 +00:00
playbook=self.playbook,
verbosity=verbosity,
event_handler=self.cb.event_handler,
status_handler=self.cb.status_handler,
**kwargs
2017-03-05 15:30:14 +00:00
)
2022-09-30 10:49:45 +00:00
return self.cb
2022-10-08 08:55:14 +00:00
class CommandRunner(AdHocRunner):
def __init__(self, inventory, command, pattern='*', project_dir='/tmp/'):
super().__init__(inventory, 'shell', command, pattern, project_dir)
def run(self, verbosity=0, **kwargs):
return super().run(verbosity, **kwargs)