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(