From 913b1b01947dca49b6db2f5e182430d583a2c99f Mon Sep 17 00:00:00 2001 From: Deepak Agrawal Date: Mon, 7 Jan 2019 23:37:02 +0530 Subject: [PATCH] Backport/2.7/49922: network_cli - ansible_command_timeout not working as expected (#50068) * network_cli - ansible_command_timeout not working as expected (#49922) * fix for command_timeout Signed-off-by: Deepak Agrawal * fix ci warning Signed-off-by: Deepak Agrawal * fix review comments Signed-off-by: Deepak Agrawal (cherry picked from commit 0f2f38b044efa1de30a28eefa71882ec189d6a61) * changelog entry for 2.7 backport Signed-off-by: Deepak Agrawal --- changelogs/fragments/49922-network-cli-timeout.yaml | 2 ++ lib/ansible/plugins/connection/network_cli.py | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/49922-network-cli-timeout.yaml diff --git a/changelogs/fragments/49922-network-cli-timeout.yaml b/changelogs/fragments/49922-network-cli-timeout.yaml new file mode 100644 index 00000000000..4cb961f9788 --- /dev/null +++ b/changelogs/fragments/49922-network-cli-timeout.yaml @@ -0,0 +1,2 @@ +bugfixes: + - fix for network_cli - ansible_command_timeout not working as expected (#49466) diff --git a/lib/ansible/plugins/connection/network_cli.py b/lib/ansible/plugins/connection/network_cli.py index 63f7eb91dc0..a7b075e644b 100644 --- a/lib/ansible/plugins/connection/network_cli.py +++ b/lib/ansible/plugins/connection/network_cli.py @@ -373,8 +373,11 @@ class Connection(NetworkConnectionBase): command_prompt_matched = False matched_prompt_window = window_count = 0 + cache_socket_timeout = self._ssh_shell.gettimeout() command_timeout = self.get_option('persistent_command_timeout') self._validate_timeout_value(command_timeout, "persistent_command_timeout") + if cache_socket_timeout != command_timeout: + self._ssh_shell.settimeout(command_timeout) buffer_read_timeout = self.get_option('persistent_buffer_read_timeout') self._validate_timeout_value(buffer_read_timeout, "persistent_buffer_read_timeout") @@ -396,6 +399,8 @@ class Connection(NetworkConnectionBase): signal.alarm(command_timeout) except AnsibleCmdRespRecv: + # reset socket timeout to global timeout + self._ssh_shell.settimeout(cache_socket_timeout) return self._command_response else: data = self._ssh_shell.recv(256) @@ -426,6 +431,8 @@ class Connection(NetworkConnectionBase): resp = self._strip(self._last_response) self._command_response = self._sanitize(resp, command) if buffer_read_timeout == 0.0: + # reset socket timeout to global timeout + self._ssh_shell.settimeout(cache_socket_timeout) return self._command_response else: command_prompt_matched = True @@ -448,7 +455,8 @@ class Connection(NetworkConnectionBase): return to_text(response, errors='surrogate_or_strict') except (socket.timeout, AttributeError): display.vvvv(traceback.format_exc(), host=self._play_context.remote_addr) - raise AnsibleConnectionFailure("timeout trying to send command: %s" % command.strip()) + raise AnsibleConnectionFailure("timeout value %s seconds reached while trying to send command: %s" + % (self._ssh_shell.gettimeout(), command.strip())) def _handle_buffer_read_timeout(self, signum, frame): display.vvvv("Response received, triggered 'persistent_buffer_read_timeout' timer of %s seconds"