Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setting ansible_network_os optional for network_cli and netconf connections #316

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 21 additions & 8 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,19 @@ 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
)

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 +394,21 @@ 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._network_os = platform_type
self.queue_message("log", "platform_type is set to %s" % platform_type)

@property
def ssh_type_conn(self):
Expand Down