From 79965ba194d70f50e7d8ea99292f495391490caf Mon Sep 17 00:00:00 2001 From: Will Rouesnel Date: Wed, 25 Oct 2023 15:57:14 +1100 Subject: [PATCH] Add terminal_initial_newline to network_cli module `terminal_initial_newline`, when set to true sends a single newline character to the remote device immediately after initially connecting. This allows network_cli to work with some types of equipment which fail to send any data when SSH connectivity is initially established, such as serial-port multiplexers. --- .../fragments/terminal_initial_newline.yaml | 2 ++ plugins/connection/network_cli.py | 21 +++++++++++++++++++ plugins/plugin_utils/terminal_base.py | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 changelogs/fragments/terminal_initial_newline.yaml diff --git a/changelogs/fragments/terminal_initial_newline.yaml b/changelogs/fragments/terminal_initial_newline.yaml new file mode 100644 index 000000000..2cc78bdad --- /dev/null +++ b/changelogs/fragments/terminal_initial_newline.yaml @@ -0,0 +1,2 @@ +minor_changes: + - network_cli - add `terminal_initial_newline` parameter as an option for terminal plugins. \ No newline at end of file diff --git a/plugins/connection/network_cli.py b/plugins/connection/network_cli.py index 5063b43e0..6da0485e9 100644 --- a/plugins/connection/network_cli.py +++ b/plugins/connection/network_cli.py @@ -198,6 +198,16 @@ response, for example I('re.I'). vars: - name: ansible_terminal_stderr_re + terminal_initial_newline: + type: boolean + description: + - This boolean flag, that when set to I(True) will send newline on initial + connection establishment to the remote device. + - This can be useful for equipment which does not send an initial header until + it receives some input, like Serial-to-SSH multiplexer hardware. + default: false + vars: + - name: ansible_terminal_inital_newline terminal_initial_prompt: type: list elements: string @@ -656,6 +666,10 @@ def _connect(self): "loaded terminal plugin for network_os %s" % self._network_os, ) + terminal_initial_newline = ( + self.get_option("terminal_initial_newline") or self._terminal.terminal_initial_newline + ) + terminal_initial_prompt = ( self.get_option("terminal_initial_prompt") or self._terminal.terminal_initial_prompt ) @@ -668,6 +682,13 @@ def _connect(self): ) check_all = self.get_option("terminal_initial_prompt_checkall") or False + if terminal_initial_newline: + self.send( + command=b"", + sendonly=True, + newline=True + ) + self.receive( prompts=terminal_initial_prompt, answer=terminal_initial_answer, diff --git a/plugins/plugin_utils/terminal_base.py b/plugins/plugin_utils/terminal_base.py index de6676cee..c6c8f7399 100644 --- a/plugins/plugin_utils/terminal_base.py +++ b/plugins/plugin_utils/terminal_base.py @@ -40,6 +40,9 @@ class TerminalBase(TerminalBaseBase): re.compile(rb"\x1b\[m"), # ANSI reset code ] + #: Send newline after initial session connection + terminal_initial_newline = False + #: terminal initial prompt terminal_initial_prompt = None