-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathnapalm_mod.py
1957 lines (1490 loc) · 61.8 KB
/
napalm_mod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
'''
NAPALM helpers
==============
Helpers for the NAPALM modules.
.. versionadded:: 2017.7.0
'''
from __future__ import absolute_import, unicode_literals, print_function
# Import python stdlib
import inspect
import logging
# import NAPALM utils
import salt.utils.napalm
from salt.utils.napalm import proxy_napalm_wrap
# Import Salt modules
from salt.ext import six
from salt.utils.decorators import depends
from salt.exceptions import CommandExecutionError
try:
from netmiko import BaseConnection
HAS_NETMIKO = True
except ImportError:
HAS_NETMIKO = False
try:
import napalm.base.netmiko_helpers
HAS_NETMIKO_HELPERS = True
except ImportError:
HAS_NETMIKO_HELPERS = False
try:
import jxmlease # pylint: disable=unused-import
HAS_JXMLEASE = True
except ImportError:
HAS_JXMLEASE = False
try:
import ciscoconfparse # pylint: disable=unused-import
HAS_CISCOCONFPARSE = True
except ImportError:
HAS_CISCOCONFPARSE = False
try:
import scp # pylint: disable=unused-import
HAS_SCP = True
except ImportError:
HAS_SCP = False
# ----------------------------------------------------------------------------------------------------------------------
# module properties
# ----------------------------------------------------------------------------------------------------------------------
__virtualname__ = 'napalm'
__proxyenabled__ = ['*']
# uses NAPALM-based proxy to interact with network devices
log = logging.getLogger(__file__)
# ----------------------------------------------------------------------------------------------------------------------
# property functions
# ----------------------------------------------------------------------------------------------------------------------
def __virtual__():
'''
NAPALM library must be installed for this module to work and run in a (proxy) minion.
'''
return salt.utils.napalm.virtual(__opts__, __virtualname__, __file__)
# ----------------------------------------------------------------------------------------------------------------------
# helper functions -- will not be exported
# ----------------------------------------------------------------------------------------------------------------------
def _get_netmiko_args(optional_args):
'''
Check for Netmiko arguments that were passed in as NAPALM optional arguments.
Return a dictionary of these optional args that will be passed into the
Netmiko ConnectHandler call.
.. note::
This is a port of the NAPALM helper for backwards compatibility with
older versions of NAPALM, and stability across Salt features.
If the netmiko helpers module is available however, it will prefer that
implementation nevertheless.
'''
if HAS_NETMIKO_HELPERS:
return napalm.base.netmiko_helpers.netmiko_args(optional_args)
# Older version don't have the netmiko_helpers module, the following code is
# simply a port from the NAPALM code base, for backwards compatibility:
# https://github.com/napalm-automation/napalm/blob/develop/napalm/base/netmiko_helpers.py
netmiko_args, _, _, netmiko_defaults = inspect.getargspec(BaseConnection.__init__)
check_self = netmiko_args.pop(0)
if check_self != 'self':
raise ValueError('Error processing Netmiko arguments')
netmiko_argument_map = dict(six.moves.zip(netmiko_args, netmiko_defaults))
# Netmiko arguments that are integrated into NAPALM already
netmiko_filter = ['ip', 'host', 'username', 'password', 'device_type', 'timeout']
# Filter out all of the arguments that are integrated into NAPALM
for k in netmiko_filter:
netmiko_argument_map.pop(k)
# Check if any of these arguments were passed in as NAPALM optional_args
netmiko_optional_args = {}
for k, v in six.iteritems(netmiko_argument_map):
try:
netmiko_optional_args[k] = optional_args[k]
except KeyError:
pass
# Return these arguments for use with establishing Netmiko SSH connection
return netmiko_optional_args
def _inject_junos_proxy(napalm_device):
'''
Inject the junos.conn key into the __proxy__, reusing the existing NAPALM
connection to the Junos device.
'''
def _ret_device():
return napalm_device['DRIVER'].device
__proxy__['junos.conn'] = _ret_device
# Injecting the junos.conn key into the __proxy__ object, we can then
# access the features that already exist into the junos module, as long
# as the rest of the dependencies are installed (jxmlease).
# junos-eznc is already installed, as part of NAPALM, and the napalm
# driver for junos already makes use of the Device class from this lib.
# So pointing the __proxy__ object to this object already loaded into
# memory, we can go and re-use the features from the existing junos
# Salt module.
def _junos_prep_fun(napalm_device):
'''
Prepare the Junos function.
'''
if __grains__['os'] != 'junos':
return {
'out': None,
'result': False,
'comment': 'This function is only available on Junos'
}
if not HAS_JXMLEASE:
return {
'out': None,
'result': False,
'comment': 'Please install jxmlease (``pip install jxmlease``) to be able to use this function.'
}
_inject_junos_proxy(napalm_device)
return {
'result': True
}
# ----------------------------------------------------------------------------------------------------------------------
# callable functions
# ----------------------------------------------------------------------------------------------------------------------
@proxy_napalm_wrap
def alive(**kwargs): # pylint: disable=unused-argument
'''
Returns the alive status of the connection layer.
The output is a dictionary under the usual dictionary
output of the NAPALM modules.
CLI Example:
.. code-block:: bash
salt '*' napalm.alive
Output Example:
.. code-block:: yaml
result: True
out:
is_alive: False
comment: ''
'''
return salt.utils.napalm.call(
napalm_device, # pylint: disable=undefined-variable
'is_alive',
**{}
)
@proxy_napalm_wrap
def reconnect(force=False, **kwargs): # pylint: disable=unused-argument
'''
Reconnect the NAPALM proxy when the connection
is dropped by the network device.
The connection can be forced to be restarted
using the ``force`` argument.
.. note::
This function can be used only when running proxy minions.
CLI Example:
.. code-block:: bash
salt '*' napalm.reconnect
salt '*' napalm.reconnect force=True
'''
default_ret = {
'out': None,
'result': True,
'comment': 'Already alive.'
}
if not salt.utils.napalm.is_proxy(__opts__):
# regular minion is always alive
# otherwise, the user would not be able to execute this command
return default_ret
is_alive = alive()
log.debug('Is alive fetch:')
log.debug(is_alive)
if not is_alive.get('result', False) or\
not is_alive.get('out', False) or\
not is_alive.get('out', {}).get('is_alive', False) or\
force: # even if alive, but the user wants to force a restart
proxyid = __opts__.get('proxyid') or __opts__.get('id')
# close the connection
log.info('Closing the NAPALM proxy connection with %s', proxyid)
salt.utils.napalm.call(
napalm_device, # pylint: disable=undefined-variable
'close',
**{}
)
# and re-open
log.info('Re-opening the NAPALM proxy connection with %s', proxyid)
salt.utils.napalm.call(
napalm_device, # pylint: disable=undefined-variable
'open',
**{}
)
default_ret.update({
'comment': 'Connection restarted!'
})
return default_ret
# otherwise, I have nothing to do here:
return default_ret
@proxy_napalm_wrap
def call(method, *args, **kwargs):
'''
Execute arbitrary methods from the NAPALM library.
To see the expected output, please consult the NAPALM documentation.
.. note::
This feature is not recommended to be used in production.
It should be used for testing only!
CLI Example:
.. code-block:: bash
salt '*' napalm.call get_lldp_neighbors
salt '*' napalm.call get_firewall_policies
salt '*' napalm.call get_bgp_config group='my-group'
'''
clean_kwargs = {}
for karg, warg in six.iteritems(kwargs):
# remove the __pub args
if not karg.startswith('__pub_'):
clean_kwargs[karg] = warg
return salt.utils.napalm.call(
napalm_device, # pylint: disable=undefined-variable
method,
*args,
**clean_kwargs
)
@proxy_napalm_wrap
def compliance_report(filepath=None,
string=None,
renderer='jinja|yaml',
**kwargs):
'''
Return the compliance report.
filepath
The absolute path to the validation file.
.. versionchanged:: 2019.2.0
Beginning with release codename ``2019.2.0``, this function has been
enhanced, to be able to leverage the multi-engine template rendering
of Salt, besides the possibility to retrieve the file source from
remote systems, the URL schemes supported being:
- ``salt://``
- ``http://`` and ``https://``
- ``ftp://``
- ``s3://``
- ``swift:/``
Or on the local file system (on the Minion).
.. note::
The rendering result does not necessarily need to be YAML, instead
it can be any format interpreted by Salt's rendering pipeline
(including pure Python).
string
.. versionadded:: 2019.2.0
The compliance report send as inline string, to be used as the file to
send through the renderer system. Note, not all renderer modules can
work with strings; the 'py' renderer requires a file, for example.
renderer: ``jinja|yaml``
.. versionadded:: 2019.2.0
The renderer pipe to send the file through; this is overridden by a
"she-bang" at the top of the file.
kwargs
.. versionchanged:: 2019.2.0
Keyword args to pass to Salt's compile_template() function.
CLI Example:
.. code-block:: bash
salt '*' napalm.compliance_report ~/validate.yml
salt '*' napalm.compliance_report salt://path/to/validator.sls
Validation File Example (pure YAML):
.. code-block:: yaml
- get_facts:
os_version: 4.17
- get_interfaces_ip:
Management1:
ipv4:
10.0.2.14:
prefix_length: 24
_mode: strict
Validation File Example (as Jinja + YAML):
.. code-block:: yaml
- get_facts:
os_version: {{ grains.version }}
- get_interfaces_ip:
Loopback0:
ipv4:
{{ grains.lo0.ipv4 }}:
prefix_length: 24
_mode: strict
- get_bgp_neighbors: {{ pillar.bgp.neighbors }}
Output Example:
.. code-block:: yaml
device1:
----------
comment:
out:
----------
complies:
False
get_facts:
----------
complies:
False
extra:
missing:
present:
----------
os_version:
----------
actual_value:
15.1F6-S1.4
complies:
False
nested:
False
get_interfaces_ip:
----------
complies:
False
extra:
missing:
- Management1
present:
----------
skipped:
result:
True
'''
validation_string = __salt__['slsutil.renderer'](path=filepath,
string=string,
default_renderer=renderer,
**kwargs)
return salt.utils.napalm.call(
napalm_device, # pylint: disable=undefined-variable
'compliance_report',
validation_source=validation_string
)
@proxy_napalm_wrap
def netmiko_args(**kwargs):
'''
.. versionadded:: 2019.2.0
Return the key-value arguments used for the authentication arguments for
the netmiko module.
When running in a non-native NAPALM driver (e.g., ``panos``, `f5``, ``mos`` -
either from https://github.com/napalm-automation-community or defined in
user's own environment, one can specify the Netmiko device type (the
``device_type`` argument) via the ``netmiko_device_type_map`` configuration
option / Pillar key, e.g.,
.. code-block:: yaml
netmiko_device_type_map:
f5: f5_ltm
dellos10: dell_os10
The configuration above defines the mapping between the NAPALM ``os`` Grain
and the Netmiko ``device_type``, e.g., when the NAPALM Grain is ``f5``, it
would use the ``f5_ltm`` SSH Netmiko driver to execute commands over SSH on
the remote network device.
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_args
'''
if not HAS_NETMIKO:
raise CommandExecutionError('Please install netmiko to be able to use this feature.')
kwargs = {}
napalm_opts = salt.utils.napalm.get_device_opts(__opts__, salt_obj=__salt__)
optional_args = napalm_opts['OPTIONAL_ARGS']
netmiko_args = _get_netmiko_args(optional_args)
kwargs['host'] = napalm_opts['HOSTNAME']
kwargs['username'] = napalm_opts['USERNAME']
kwargs['password'] = napalm_opts['PASSWORD']
kwargs['timeout'] = napalm_opts['TIMEOUT']
kwargs.update(netmiko_args)
netmiko_device_type_map = {
'junos': 'juniper_junos',
'ios': 'cisco_ios',
'iosxr': 'cisco_xr',
'eos': 'arista_eos',
'nxos_ssh': 'cisco_nxos',
'asa': 'cisco_asa',
'fortios': 'fortinet',
'panos': 'paloalto_panos',
'aos': 'alcatel_aos',
'vyos': 'vyos',
'f5': 'f5_ltm',
'ce': 'huawei',
's350': 'cisco_s300'
}
# If you have a device type that is not listed here, please submit a PR
# to add it, and/or add the map into your opts/Pillar: netmiko_device_type_map
# Example:
#
# netmiko_device_type_map:
# junos: juniper_junos
# ios: cisco_ios
#
#etc.
netmiko_device_type_map.update(__salt__['config.get']('netmiko_device_type_map', {}))
kwargs['device_type'] = netmiko_device_type_map[__grains__['os']]
return kwargs
@proxy_napalm_wrap
def netmiko_fun(fun, *args, **kwargs):
'''
.. versionadded:: 2019.2.0
Call an arbitrary function from the :mod:`Netmiko<salt.modules.netmiko_mod>`
module, passing the authentication details from the existing NAPALM
connection.
fun
The name of the function from the :mod:`Netmiko<salt.modules.netmiko_mod>`
to invoke.
args
List of arguments to send to the execution function specified in
``fun``.
kwargs
Key-value arguments to send to the execution function specified in
``fun``.
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_fun send_command 'show version'
'''
if 'netmiko.' not in fun:
fun = 'netmiko.{fun}'.format(fun=fun)
netmiko_kwargs = netmiko_args()
kwargs.update(netmiko_kwargs)
return __salt__[fun](*args, **kwargs)
@proxy_napalm_wrap
def netmiko_call(method, *args, **kwargs):
'''
.. versionadded:: 2019.2.0
Execute an arbitrary Netmiko method, passing the authentication details from
the existing NAPALM connection.
method
The name of the Netmiko method to execute.
args
List of arguments to send to the Netmiko method specified in ``method``.
kwargs
Key-value arguments to send to the execution function specified in
``method``.
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_call send_command 'show version'
'''
netmiko_kwargs = netmiko_args()
kwargs.update(netmiko_kwargs)
return __salt__['netmiko.call'](method, *args, **kwargs)
@proxy_napalm_wrap
def netmiko_multi_call(*methods, **kwargs):
'''
.. versionadded:: 2019.2.0
Execute a list of arbitrary Netmiko methods, passing the authentication
details from the existing NAPALM connection.
methods
List of dictionaries with the following keys:
- ``name``: the name of the Netmiko function to invoke.
- ``args``: list of arguments to send to the ``name`` method.
- ``kwargs``: key-value arguments to send to the ``name`` method.
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_multi_call "{'name': 'send_command', 'args': ['show version']}" "{'name': 'send_command', 'args': ['show interfaces']}"
'''
netmiko_kwargs = netmiko_args()
kwargs.update(netmiko_kwargs)
return __salt__['netmiko.multi_call'](*methods, **kwargs)
@proxy_napalm_wrap
def netmiko_commands(*commands, **kwargs):
'''
.. versionadded:: 2019.2.0
Invoke one or more commands to be executed on the remote device, via Netmiko.
Returns a list of strings, with the output from each command.
commands
A list of commands to be executed.
expect_string
Regular expression pattern to use for determining end of output.
If left blank will default to being based on router prompt.
delay_factor: ``1``
Multiplying factor used to adjust delays (default: ``1``).
max_loops: ``500``
Controls wait time in conjunction with delay_factor. Will default to be
based upon self.timeout.
auto_find_prompt: ``True``
Whether it should try to auto-detect the prompt (default: ``True``).
strip_prompt: ``True``
Remove the trailing router prompt from the output (default: ``True``).
strip_command: ``True``
Remove the echo of the command from the output (default: ``True``).
normalize: ``True``
Ensure the proper enter is sent at end of command (default: ``True``).
use_textfsm: ``False``
Process command output through TextFSM template (default: ``False``).
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_commands 'show version' 'show interfaces'
'''
conn = netmiko_conn(**kwargs)
ret = []
for cmd in commands:
ret.append(conn.send_command(cmd))
return ret
@proxy_napalm_wrap
def netmiko_config(*config_commands, **kwargs):
'''
.. versionadded:: 2019.2.0
Load a list of configuration commands on the remote device, via Netmiko.
.. warning::
Please remember that ``netmiko`` does not have any rollback safeguards
and any configuration change will be directly loaded into the running
config if the platform doesn't have the concept of ``candidate`` config.
On Junos, or other platforms that have this capability, the changes will
not be loaded into the running config, and the user must set the
``commit`` argument to ``True`` to transfer the changes from the
candidate into the running config before exiting.
config_commands
A list of configuration commands to be loaded on the remote device.
config_file
Read the configuration commands from a file. The file can equally be a
template that can be rendered using the engine of choice (see
``template_engine``).
This can be specified using the absolute path to the file, or using one
of the following URL schemes:
- ``salt://``, to fetch the file from the Salt fileserver.
- ``http://`` or ``https://``
- ``ftp://``
- ``s3://``
- ``swift://``
exit_config_mode: ``True``
Determines whether or not to exit config mode after complete.
delay_factor: ``1``
Factor to adjust delays.
max_loops: ``150``
Controls wait time in conjunction with delay_factor (default: ``150``).
strip_prompt: ``False``
Determines whether or not to strip the prompt (default: ``False``).
strip_command: ``False``
Determines whether or not to strip the command (default: ``False``).
config_mode_command
The command to enter into config mode.
commit: ``False``
Commit the configuration changes before exiting the config mode. This
option is by default disabled, as many platforms don't have this
capability natively.
CLI Example:
.. code-block:: bash
salt '*' napalm.netmiko_config 'set system ntp peer 1.2.3.4' commit=True
salt '*' napalm.netmiko_config https://bit.ly/2sgljCB
'''
netmiko_kwargs = netmiko_args()
kwargs.update(netmiko_kwargs)
return __salt__['netmiko.send_config'](config_commands=config_commands,
**kwargs)
@proxy_napalm_wrap
def netmiko_conn(**kwargs):
'''
.. versionadded:: 2019.2.0
Return the connection object with the network device, over Netmiko, passing
the authentication details from the existing NAPALM connection.
.. warning::
This function is not suitable for CLI usage, more rather to be used
in various Salt modules.
USAGE Example:
.. code-block:: python
conn = __salt__['napalm.netmiko_conn']()
res = conn.send_command('show interfaces')
conn.disconnect()
'''
netmiko_kwargs = netmiko_args()
kwargs.update(netmiko_kwargs)
return __salt__['netmiko.get_connection'](**kwargs)
@proxy_napalm_wrap
def junos_rpc(cmd=None, dest=None, format=None, **kwargs):
'''
.. versionadded:: 2019.2.0
Execute an RPC request on the remote Junos device.
cmd
The RPC request to the executed. To determine the RPC request, you can
check the from the command line of the device, by executing the usual
command followed by ``| display xml rpc``, e.g.,
``show lldp neighbors | display xml rpc``.
dest
Destination file where the RPC output is stored. Note that the file will
be stored on the Proxy Minion. To push the files to the Master, use
:mod:`cp.push <salt.modules.cp.push>` Execution function.
format: ``xml``
The format in which the RPC reply is received from the device.
dev_timeout: ``30``
The NETCONF RPC timeout.
filter
Used with the ``get-config`` RPC request to filter out the config tree.
terse: ``False``
Whether to return terse output.
.. note::
Some RPC requests may not support this argument.
interface_name
Name of the interface to query.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_rpc get-lldp-neighbors-information
salt '*' napalm.junos_rcp get-config <configuration><system><ntp/></system></configuration>
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
if not format:
format = 'xml'
rpc_ret = __salt__['junos.rpc'](cmd=cmd,
dest=dest,
format=format,
**kwargs)
rpc_ret['comment'] = rpc_ret.pop('message', '')
rpc_ret['result'] = rpc_ret.pop('out', False)
rpc_ret['out'] = rpc_ret.pop('rpc_reply', None)
# The comment field is "message" in the Junos module
return rpc_ret
@proxy_napalm_wrap
def junos_commit(**kwargs):
'''
.. versionadded:: 2019.2.0
Commit the changes loaded in the candidate configuration.
dev_timeout: ``30``
The NETCONF RPC timeout (in seconds).
comment
Provide a comment for the commit.
confirm
Provide time in minutes for commit confirmation. If this option is
specified, the commit will be rolled back in the specified amount of time
unless the commit is confirmed.
sync: ``False``
When ``True``, on dual control plane systems, requests that the candidate
configuration on one control plane be copied to the other control plane,
checked for correct syntax, and committed on both Routing Engines.
force_sync: ``False``
When ``True``, on dual control plane systems, force the candidate
configuration on one control plane to be copied to the other control
plane.
full
When ``True``, requires all the daemons to check and evaluate the new
configuration.
detail
When ``True``, return commit detail.
CLI Examples:
.. code-block:: bash
salt '*' napalm.junos_commit comment='Commitiing via Salt' detail=True
salt '*' napalm.junos_commit dev_timeout=60 confirm=10
salt '*' napalm.junos_commit sync=True dev_timeout=90
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
return __salt__['junos.commit'](**kwargs)
@proxy_napalm_wrap
def junos_install_os(path=None, **kwargs):
'''
.. versionadded:: 2019.2.0
Installs the given image on the device.
path
The image file source. This argument supports the following URIs:
- Absolute path on the Minion.
- ``salt://`` to fetch from the Salt fileserver.
- ``http://`` and ``https://``
- ``ftp://``
- ``swift:/``
- ``s3://``
dev_timeout: ``30``
The NETCONF RPC timeout (in seconds)
reboot: ``False``
Whether to reboot the device after the installation is complete.
no_copy: ``False``
If ``True`` the software package will not be copied to the remote
device.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_install_os salt://images/junos_16_1.tgz reboot=True
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
return __salt__['junos.install_os'](path=path, **kwargs)
@proxy_napalm_wrap
def junos_facts(**kwargs):
'''
.. versionadded:: 2019.2.0
The complete list of Junos facts collected by ``junos-eznc``.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_facts
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
facts = dict(napalm_device['DRIVER'].device.facts) # pylint: disable=undefined-variable
if 'version_info' in facts:
facts['version_info'] = \
dict(facts['version_info'])
# For backward compatibility. 'junos_info' is present
# only of in newer versions of facts.
if 'junos_info' in facts:
for re in facts['junos_info']:
facts['junos_info'][re]['object'] = \
dict(facts['junos_info'][re]['object'])
return facts
@proxy_napalm_wrap
def junos_cli(command, format=None, dev_timeout=None, dest=None, **kwargs):
'''
.. versionadded:: 2019.2.0
Execute a CLI command and return the output in the specified format.
command
The command to execute on the Junos CLI.
format: ``text``
Format in which to get the CLI output (either ``text`` or ``xml``).
dev_timeout: ``30``
The NETCONF RPC timeout (in seconds).
dest
Destination file where the RPC output is stored. Note that the file will
be stored on the Proxy Minion. To push the files to the Master, use
:mod:`cp.push <salt.modules.cp.push>`.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_cli 'show lldp neighbors'
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
return __salt__['junos.cli'](command,
format=format,
dev_timeout=dev_timeout,
dest=dest,
**kwargs)
@proxy_napalm_wrap
def junos_copy_file(src, dst, **kwargs):
'''
.. versionadded:: 2019.2.0
Copies the file on the remote Junos device.
src
The source file path. This argument accepts the usual Salt URIs (e.g.,
``salt://``, ``http://``, ``https://``, ``s3://``, ``ftp://``, etc.).
dst
The destination path on the device where to copy the file.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_copy_file https://example.com/junos.cfg /var/tmp/myjunos.cfg
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
cached_src = __salt__['cp.cache_file'](src)
return __salt__['junos.file_copy'](cached_src, dst)
@proxy_napalm_wrap
def junos_call(fun, *args, **kwargs):
'''
.. versionadded:: 2019.2.0
Execute an arbitrary function from the
:mod:`junos execution module <salt.module.junos>`. To check what ``args``
and ``kwargs`` you must send to the function, please consult the appropriate
documentation.
fun
The name of the function. E.g., ``set_hostname``.
args
List of arguments to send to the ``junos`` function invoked.
kwargs
Dictionary of key-value arguments to send to the ``juno`` function
invoked.
CLI Example:
.. code-block:: bash
salt '*' napalm.junos_fun cli 'show system commit'
'''
prep = _junos_prep_fun(napalm_device) # pylint: disable=undefined-variable
if not prep['result']:
return prep
if 'junos.' not in fun:
mod_fun = 'junos.{}'.format(fun)
else: