From 14b854587a62d734b9372c95335ac4be4bafe7d2 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 13 Nov 2020 21:04:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(ops):=20=E4=BF=AE=E5=A4=8D=E5=9B=A0?= =?UTF-8?q?=E4=B8=BA=E6=9B=B4=E6=94=B9controlmaster=E5=BC=95=E8=B5=B7?= =?UTF-8?q?=E7=9A=84=E8=BF=9E=E6=8E=A5=E4=B8=8D=E6=9C=8D=E7=94=A8=E7=BB=B4?= =?UTF-8?q?=E5=86=85=E6=89=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible/callback.py | 8 +++++++- apps/ops/ansible/runner.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/ops/ansible/callback.py b/apps/ops/ansible/callback.py index 3c2fe1b90..adba3e81b 100644 --- a/apps/ops/ansible/callback.py +++ b/apps/ops/ansible/callback.py @@ -65,6 +65,8 @@ class AdHocResultCallback(CallbackMixin, CallbackModule, CMDCallBackModule): """ Task result Callback """ + context = None + def clean_result(self, t, host, task_name, task_result): contacted = self.results_summary["contacted"] dark = self.results_summary["dark"] @@ -133,7 +135,11 @@ class AdHocResultCallback(CallbackMixin, CallbackModule, CMDCallBackModule): pass def set_play_context(self, context): - context.ssh_args = '-C -o ControlMaster=no' + # for k, v in context._attributes.items(): + # print("{} ==> {}".format(k, v)) + if self.context and isinstance(self.context, dict): + for k, v in self.context.items(): + setattr(context, k, v) class CommandResultCallback(AdHocResultCallback): diff --git a/apps/ops/ansible/runner.py b/apps/ops/ansible/runner.py index 4486bdf1a..e741d8f8b 100644 --- a/apps/ops/ansible/runner.py +++ b/apps/ops/ansible/runner.py @@ -182,6 +182,13 @@ class AdHocRunner: _options.update(options) return _options + def set_control_master_if_need(self, cleaned_tasks): + modules = [task.get('action', {}).get('module') for task in cleaned_tasks] + if {'ping', 'win_ping'} & set(modules): + self.results_callback.context = { + 'ssh_args': '-C -o ControlMaster=no' + } + def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no'): """ :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ] @@ -193,6 +200,7 @@ class AdHocRunner: self.check_pattern(pattern) self.results_callback = self.get_result_callback() cleaned_tasks = self.clean_tasks(tasks) + self.set_control_master_if_need(cleaned_tasks) context.CLIARGS = ImmutableDict(self.options) play_source = dict(