fix: 修复执行快捷命令时 local_connection 没有被正确设置

pull/13123/head
Aaron3S 2024-04-23 18:59:39 +08:00 committed by Bryan
parent 2acc84dc69
commit 9b20b67039
3 changed files with 12 additions and 4 deletions

View File

@ -14,8 +14,10 @@ __all__ = ['AdHocRunner', 'PlaybookRunner', 'SuperPlaybookRunner', 'UploadFileRu
class AdHocRunner:
cmd_modules_choices = ('shell', 'raw', 'command', 'script', 'win_shell')
need_local_connection_modules_choices = ("mysql", "postgresql", "sqlserver", "huawei")
def __init__(self, inventory, module, module_args='', pattern='*', project_dir='/tmp/', extra_vars=None,
def __init__(self, inventory, job_module, module, module_args='', pattern='*', project_dir='/tmp/',
extra_vars=None,
dry_run=False, timeout=-1):
if extra_vars is None:
extra_vars = {}
@ -23,6 +25,7 @@ class AdHocRunner:
self.inventory = inventory
self.pattern = pattern
self.module = module
self.job_module = job_module
self.module_args = module_args
self.project_dir = project_dir
self.cb = DefaultCallback()
@ -30,8 +33,7 @@ class AdHocRunner:
self.extra_vars = extra_vars
self.dry_run = dry_run
self.timeout = timeout
# enable local connection
self.extra_vars.update({"LOCAL_CONNECTION_ENABLED": "1"})
self.envs = {}
def check_module(self):
if self.module not in self.cmd_modules_choices:
@ -40,8 +42,13 @@ class AdHocRunner:
raise CommandInBlackListException(
"Command is rejected by black list: {}".format(self.module_args.split()[0]))
def set_local_connection(self):
if self.job_module in self.need_local_connection_modules_choices:
self.envs.update({"LOCAL_CONNECTION_ENABLED": "1"})
def run(self, verbosity=0, **kwargs):
self.check_module()
self.set_local_connection()
verbosity = get_ansible_log_verbosity(verbosity)
if not os.path.exists(self.project_dir):
@ -53,6 +60,7 @@ class AdHocRunner:
interface.run(
timeout=self.timeout if self.timeout > 0 else None,
extravars=self.extra_vars,
envvars=self.envs,
host_pattern=self.pattern,
private_data_dir=self.project_dir,
inventory=self.inventory,

View File

@ -334,6 +334,7 @@ class JobExecution(JMSOrgBaseModel):
runner = AdHocRunner(
self.inventory_path,
self.job.module,
module,
timeout=self.current_job.timeout,
module_args=args,

View File

@ -75,7 +75,6 @@ class ReceptorService:
print("\n- PID file is corrupted, starting Receptor...")
os.remove(self.pid_file)
os.environ.update({'LOCAL_CONNECTION_ENABLED': '1'})
os.environ.setdefault('ANSIBLE_LIBRARY', DEFAULT_ANSIBLE_MODULES_DIR)
os.environ.update({'PYTHONPATH': APPS_DIR})
process = subprocess.Popen(self.receptor_command)