From a64418c2b3e2401953d1ab0f87425500d8c3a4a4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 9 Jun 2020 11:59:38 -0400 Subject: [PATCH] avoid constant checking controlpersist (#69910) * avoid constant checking controlpersist --- changelogs/fragments/only_be_smart_once.yml | 2 ++ lib/ansible/config/base.yml | 3 ++- lib/ansible/constants.py | 2 +- lib/ansible/executor/playbook_executor.py | 4 ++-- lib/ansible/utils/ssh_functions.py | 16 ++++++++++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/only_be_smart_once.yml diff --git a/changelogs/fragments/only_be_smart_once.yml b/changelogs/fragments/only_be_smart_once.yml new file mode 100644 index 00000000000..5b9a20e801b --- /dev/null +++ b/changelogs/fragments/only_be_smart_once.yml @@ -0,0 +1,2 @@ +bugfixes: + - optimize 'smart' detection from being run over and over and preferably do it at config time. diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index ff61e99def7..725548cc7d1 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -135,7 +135,7 @@ ANSIBLE_SSH_CONTROL_PATH_DIR: - {key: control_path_dir, section: ssh_connection} yaml: {key: ssh_connection.control_path_dir} ANSIBLE_SSH_EXECUTABLE: - # TODO: move to ssh plugin + # TODO: move to ssh plugin, note that ssh_utils refs this and needs to be updated if removed default: ssh description: - This defines the location of the ssh binary. It defaults to `ssh` which will use the first ssh binary available in $PATH. @@ -1162,6 +1162,7 @@ DEFAULT_TIMEOUT: - {key: timeout, section: defaults} type: integer DEFAULT_TRANSPORT: + # note that ssh_utils refs this and needs to be updated if removed name: Connection plugin default: smart description: "Default connection plugin to use, the 'smart' option will toggle between 'ssh' and 'paramiko' depending on controller OS and ssh versions" diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index d94fabc1962..5e9270b910e 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -12,11 +12,11 @@ from ast import literal_eval from jinja2 import Template from string import ascii_letters, digits +from ansible.config.manager import ConfigManager, ensure_type, get_ini_config_value from ansible.module_utils._text import to_text from ansible.module_utils.common.collections import Sequence from ansible.module_utils.parsing.convert_bool import boolean, BOOLEANS_TRUE from ansible.module_utils.six import string_types -from ansible.config.manager import ConfigManager, ensure_type, get_ini_config_value def _warning(msg): diff --git a/lib/ansible/executor/playbook_executor.py b/lib/ansible/executor/playbook_executor.py index f8efee052c0..aacf1353d50 100644 --- a/lib/ansible/executor/playbook_executor.py +++ b/lib/ansible/executor/playbook_executor.py @@ -31,7 +31,7 @@ from ansible.playbook import Playbook from ansible.template import Templar from ansible.utils.helpers import pct_to_int from ansible.utils.path import makedirs_safe -from ansible.utils.ssh_functions import check_for_controlpersist +from ansible.utils.ssh_functions import set_default_transport from ansible.utils.display import Display display = Display() @@ -70,7 +70,7 @@ class PlaybookExecutor: # in inventory is also cached. We can't do this caching at the point # where it is used (in task_executor) because that is post-fork and # therefore would be discarded after every task. - check_for_controlpersist(C.ANSIBLE_SSH_EXECUTABLE) + set_default_transport() def run(self): ''' diff --git a/lib/ansible/utils/ssh_functions.py b/lib/ansible/utils/ssh_functions.py index a5028c8877b..11ab7e134cb 100644 --- a/lib/ansible/utils/ssh_functions.py +++ b/lib/ansible/utils/ssh_functions.py @@ -22,7 +22,9 @@ __metaclass__ = type import subprocess +from ansible import constants as C from ansible.module_utils._text import to_bytes +from ansible.module_utils.compat.paramiko import paramiko _HAS_CONTROLPERSIST = {} @@ -47,3 +49,17 @@ def check_for_controlpersist(ssh_executable): _HAS_CONTROLPERSIST[ssh_executable] = has_cp return has_cp + + +def set_default_transport(): + + # deal with 'smart' connection .. one time .. + if C.DEFAULT_TRANSPORT == 'smart': + # TODO: check if we can deprecate this as ssh w/o control persist should + # not be as common anymore. + + # see if SSH can support ControlPersist if not use paramiko + if not check_for_controlpersist(C.ANSIBLE_SSH_EXECUTABLE) and paramiko is not None: + C.DEFAULT_TRANSPORT = "paramiko" + else: + C.DEFAULT_TRANSPORT = "ssh"