napalm-automation / napalm-salt

Modules for event-driven network automation and orchestration using Salt
Apache License 2.0
127 stars 36 forks source link

napalm.netmiko_commands ignore delay_factor max_loops and except_string #60

Open Obus20 opened 5 years ago

Obus20 commented 5 years ago

Hello,

I try to update a switch via napalm.netmiko_commands. My command looks like that:

salt 'SW' napalm.netmiko_commands "archive download-sw /overwrite http://10.100.31.110/c3560cx-universalk9-tar.152-4.E7.tar" delay_factor=20 max_loops=1000 expect_string="All software images installed."

When I try that I always get back an error:

The minion function caused an exception: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/salt/minion.py", line 1660, in _thread_return return_data = minion_instance.executors[fname](opts, data, func, args, kwargs) File "/usr/lib/python3/dist-packages/salt/executors/direct_call.py", line 12, in execute return func(*args, *kwargs) File "/usr/lib/python3/dist-packages/salt/utils/napalm.py", line 480, in func_wrapper ret = func(args, **kwargs) File "/usr/lib/python3/dist-packages/salt/modules/napalm_mod.py", line 627, in netmiko_commands ret.append(conn.send_command(cmd)) File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 1188, in send_command search_pattern)) OSError: Search pattern never detected in send_command_expect: SW

But when I try it on the same switch with a netmiko proxy and the command netmiko.send_command it is working correct. That is the reason why I think that the arguments delay_factor max_loops and except_string are ignored.

Can you please help me?

Thank you!

mirceaulinic commented 5 years ago

Hi @Obus20. Thanks for pointing this out.

Would you be able to apply the following patch and confirm it works well:

diff --git a/salt/modules/napalm_mod.py b/salt/modules/napalm_mod.py
index fae9ecf032..b1e9c937e2 100644
--- a/salt/modules/napalm_mod.py
+++ b/salt/modules/napalm_mod.py
@@ -21,6 +21,7 @@ from salt.utils.napalm import proxy_napalm_wrap
 from salt.ext import six
 from salt.utils.decorators import depends
 from salt.exceptions import CommandExecutionError
+from salt.modules.netmiko_mod import _prepare_connection

 try:
     from netmiko import BaseConnection
@@ -621,10 +622,10 @@ def netmiko_commands(*commands, **kwargs):

         salt '*' napalm.netmiko_commands 'show version' 'show interfaces'
     '''
-    conn = netmiko_conn(**kwargs)
+    conn, kwargs = _prepare_connection(**kwargs)
     ret = []
     for cmd in commands:
-        ret.append(conn.send_command(cmd))
+        ret.append(conn.send_command(cmd, **kwargs))
     return ret

You can, for example, download the napalm_mod.py module from https://raw.githubusercontent.com/saltstack/salt/develop/salt/modules/napalm_mod.py and put it under your salt://_modules path then apply the patch above.

Obus20 commented 5 years ago

Hi @mirceaulinic. Thank you for your answer. I changed the code like you described, but unfortunately I get a new error: The minion function caused an exception: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/salt/utils/templates.py", line 392, in render_jinja_tmpl output = template.render(**decoded_context) File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1008, in render return self.environment.handle_exception(exc_info, True) File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/lib/python3/dist-packages/jinja2/_compat.py", line 37, in reraise raise value.with_traceback(tb) File "<template>", line 13, in top-level template code File "/usr/lib/python3/dist-packages/salt/utils/napalm.py", line 480, in func_wrapper ret = func(*args, **kwargs) File "/usr/lib/python3/dist-packages/salt/modules/napalm_mod.py", line 625, in netmiko_commands conn, kwargs = _prepare_connection(**kwargs) File "/usr/lib/python3/dist-packages/salt/modules/netmiko_mod.py", line 248, in _prepare_connection netmiko_kwargs = __salt__['config.get']('netmiko', {}) NameError: name '__salt__' is not defined

Can you please help me with that?