diff --git a/changelogs/fragments/network_os_optional_network_cli_netconf.yaml b/changelogs/fragments/network_os_optional_network_cli_netconf.yaml new file mode 100644 index 000000000..29273f65b --- /dev/null +++ b/changelogs/fragments/network_os_optional_network_cli_netconf.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Make ansible_network_os as optional param for network_cli and netconf connection plugins. diff --git a/plugins/connection/netconf.py b/plugins/connection/netconf.py index 47da705bc..5113108db 100644 --- a/plugins/connection/netconf.py +++ b/plugins/connection/netconf.py @@ -154,6 +154,13 @@ - name: ANSIBLE_NETCONF_SSH_CONFIG vars: - name: ansible_netconf_ssh_config + platform_type: + description: + - Set type of platform. + env: + - name: ANSIBLE_PLATFORM_TYPE + vars: + - name: ansible_platform_type """ import os @@ -207,12 +214,20 @@ def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__( play_context, new_stdin, *args, **kwargs ) + self._manager = None + self.key_filename = None + self._ssh_config = None + self._platform_type = None + self.load_platform_plugins(self._network_os) - # If network_os is not specified then set the network os to auto + def load_platform_plugins(self, platform_type=None): + # If network_os/platform_type is not specified then set the network os to auto # This will be used to trigger the use of guess_network_os when connecting. - self._network_os = self._network_os or "auto" + platform_type = ( + platform_type or self.get_option("platform_type") or "auto" + ) - self.netconf = netconf_loader.get(self._network_os, self) + self.netconf = netconf_loader.get(platform_type, self) if self.netconf: self._sub_plugin = { "type": "netconf", @@ -225,7 +240,7 @@ def __init__(self, play_context, new_stdin, *args, **kwargs): % ( self.netconf._load_name, self.netconf._original_path, - self._network_os, + platform_type, ), ) else: @@ -238,13 +253,11 @@ def __init__(self, play_context, new_stdin, *args, **kwargs): self.queue_message( "vvvv", "unable to load netconf plugin for network_os %s, falling back to default plugin" - % self._network_os, + % platform_type, ) - self.queue_message("log", "network_os is set to %s" % self._network_os) - self._manager = None - self.key_filename = None - self._ssh_config = None + self.queue_message("log", "network_os is set to %s" % platform_type) + self._network_os = platform_type def exec_command(self, cmd, in_data=None, sudoable=True): """Sends the request to the node and returns the reply diff --git a/plugins/connection/network_cli.py b/plugins/connection/network_cli.py index b642919af..6531823f2 100644 --- a/plugins/connection/network_cli.py +++ b/plugins/connection/network_cli.py @@ -274,6 +274,13 @@ - name: ANSIBLE_NETWORK_SINGLE_USER_MODE vars: - name: ansible_network_single_user_mode + platform_type: + description: + - Set type of platform. + env: + - name: ANSIBLE_PLATFORM_TYPE + vars: + - name: ansible_platform_type """ from functools import wraps @@ -362,13 +369,29 @@ def __init__(self, play_context, new_stdin, *args, **kwargs): self._single_user_mode = False if self._network_os: - self._terminal = terminal_loader.get(self._network_os, self) + self.load_platform_plugins(self._network_os) + + def load_platform_plugins(self, platform_type=None): + platform_type = platform_type or self.get_option("platform_type") + + if platform_type: + self._terminal = terminal_loader.get(platform_type, self) if not self._terminal: raise AnsibleConnectionFailure( - "network os %s is not supported" % self._network_os + "network os %s is not supported" % platform_type + ) + else: + self.queue_message( + "vvvv", + "loaded terminal plugin %s from path %s for network_os %s" + % ( + self.terminal._load_name, + self.terminal._original_path, + platform_type, + ), ) - self.cliconf = cliconf_loader.get(self._network_os, self) + self.cliconf = cliconf_loader.get(platform_type, self) if self.cliconf: self._sub_plugin = { "type": "cliconf", @@ -381,21 +404,20 @@ def __init__(self, play_context, new_stdin, *args, **kwargs): % ( self.cliconf._load_name, self.cliconf._original_path, - self._network_os, + platform_type, ), ) else: self.queue_message( "vvvv", - "unable to load cliconf for network_os %s" - % self._network_os, + "unable to load cliconf for network_os %s" % platform_type, ) else: raise AnsibleConnectionFailure( "Unable to automatically determine host network os. Please " - "manually configure ansible_network_os value for this host" + "manually configure platform_type value for this host" ) - self.queue_message("log", "network_os is set to %s" % self._network_os) + self.queue_message("log", "platform_type is set to %s" % platform_type) @property def ssh_type_conn(self): @@ -612,11 +634,6 @@ def _connect(self): if self._ssh_type == "paramiko": self._ssh_shell.settimeout(command_timeout) - self.queue_message( - "vvvv", - "loaded terminal plugin for network_os %s" % self._network_os, - ) - terminal_initial_prompt = ( self.get_option("terminal_initial_prompt") or self._terminal.terminal_initial_prompt