Skip to content

Commit

Permalink
Make setting ansible_network_os optional for network_cli and netconf
Browse files Browse the repository at this point in the history
*  Add `load_platform_plugins` method in network_cli and netconf
   connection to handle loading of sub-plugins when either invoked
   by setting `ansible_network_os` value of by calling it from
   the module code.
  • Loading branch information
ganeshrn committed Oct 20, 2021
1 parent 71900b5 commit 2b04d0d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Make ansible_network_os as optional param for network_cli and netconf connection plugins.
31 changes: 22 additions & 9 deletions plugins/connection/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand All @@ -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
Expand Down
43 changes: 30 additions & 13 deletions plugins/connection/network_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b04d0d

Please sign in to comment.