Skip to content

Commit 2b04d0d

Browse files
committed
Make setting ansible_network_os optional for network_cli and netconf
* 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.
1 parent 71900b5 commit 2b04d0d

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- Make ansible_network_os as optional param for network_cli and netconf connection plugins.

plugins/connection/netconf.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@
154154
- name: ANSIBLE_NETCONF_SSH_CONFIG
155155
vars:
156156
- name: ansible_netconf_ssh_config
157+
platform_type:
158+
description:
159+
- Set type of platform.
160+
env:
161+
- name: ANSIBLE_PLATFORM_TYPE
162+
vars:
163+
- name: ansible_platform_type
157164
"""
158165

159166
import os
@@ -207,12 +214,20 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
207214
super(Connection, self).__init__(
208215
play_context, new_stdin, *args, **kwargs
209216
)
217+
self._manager = None
218+
self.key_filename = None
219+
self._ssh_config = None
220+
self._platform_type = None
221+
self.load_platform_plugins(self._network_os)
210222

211-
# If network_os is not specified then set the network os to auto
223+
def load_platform_plugins(self, platform_type=None):
224+
# If network_os/platform_type is not specified then set the network os to auto
212225
# This will be used to trigger the use of guess_network_os when connecting.
213-
self._network_os = self._network_os or "auto"
226+
platform_type = (
227+
platform_type or self.get_option("platform_type") or "auto"
228+
)
214229

215-
self.netconf = netconf_loader.get(self._network_os, self)
230+
self.netconf = netconf_loader.get(platform_type, self)
216231
if self.netconf:
217232
self._sub_plugin = {
218233
"type": "netconf",
@@ -225,7 +240,7 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
225240
% (
226241
self.netconf._load_name,
227242
self.netconf._original_path,
228-
self._network_os,
243+
platform_type,
229244
),
230245
)
231246
else:
@@ -238,13 +253,11 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
238253
self.queue_message(
239254
"vvvv",
240255
"unable to load netconf plugin for network_os %s, falling back to default plugin"
241-
% self._network_os,
256+
% platform_type,
242257
)
243258

244-
self.queue_message("log", "network_os is set to %s" % self._network_os)
245-
self._manager = None
246-
self.key_filename = None
247-
self._ssh_config = None
259+
self.queue_message("log", "network_os is set to %s" % platform_type)
260+
self._network_os = platform_type
248261

249262
def exec_command(self, cmd, in_data=None, sudoable=True):
250263
"""Sends the request to the node and returns the reply

plugins/connection/network_cli.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@
274274
- name: ANSIBLE_NETWORK_SINGLE_USER_MODE
275275
vars:
276276
- name: ansible_network_single_user_mode
277+
platform_type:
278+
description:
279+
- Set type of platform.
280+
env:
281+
- name: ANSIBLE_PLATFORM_TYPE
282+
vars:
283+
- name: ansible_platform_type
277284
"""
278285

279286
from functools import wraps
@@ -362,13 +369,29 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
362369
self._single_user_mode = False
363370

364371
if self._network_os:
365-
self._terminal = terminal_loader.get(self._network_os, self)
372+
self.load_platform_plugins(self._network_os)
373+
374+
def load_platform_plugins(self, platform_type=None):
375+
platform_type = platform_type or self.get_option("platform_type")
376+
377+
if platform_type:
378+
self._terminal = terminal_loader.get(platform_type, self)
366379
if not self._terminal:
367380
raise AnsibleConnectionFailure(
368-
"network os %s is not supported" % self._network_os
381+
"network os %s is not supported" % platform_type
382+
)
383+
else:
384+
self.queue_message(
385+
"vvvv",
386+
"loaded terminal plugin %s from path %s for network_os %s"
387+
% (
388+
self.terminal._load_name,
389+
self.terminal._original_path,
390+
platform_type,
391+
),
369392
)
370393

371-
self.cliconf = cliconf_loader.get(self._network_os, self)
394+
self.cliconf = cliconf_loader.get(platform_type, self)
372395
if self.cliconf:
373396
self._sub_plugin = {
374397
"type": "cliconf",
@@ -381,21 +404,20 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
381404
% (
382405
self.cliconf._load_name,
383406
self.cliconf._original_path,
384-
self._network_os,
407+
platform_type,
385408
),
386409
)
387410
else:
388411
self.queue_message(
389412
"vvvv",
390-
"unable to load cliconf for network_os %s"
391-
% self._network_os,
413+
"unable to load cliconf for network_os %s" % platform_type,
392414
)
393415
else:
394416
raise AnsibleConnectionFailure(
395417
"Unable to automatically determine host network os. Please "
396-
"manually configure ansible_network_os value for this host"
418+
"manually configure platform_type value for this host"
397419
)
398-
self.queue_message("log", "network_os is set to %s" % self._network_os)
420+
self.queue_message("log", "platform_type is set to %s" % platform_type)
399421

400422
@property
401423
def ssh_type_conn(self):
@@ -612,11 +634,6 @@ def _connect(self):
612634
if self._ssh_type == "paramiko":
613635
self._ssh_shell.settimeout(command_timeout)
614636

615-
self.queue_message(
616-
"vvvv",
617-
"loaded terminal plugin for network_os %s" % self._network_os,
618-
)
619-
620637
terminal_initial_prompt = (
621638
self.get_option("terminal_initial_prompt")
622639
or self._terminal.terminal_initial_prompt

0 commit comments

Comments
 (0)