11from nornir .core .task import Task , Result
2+ from netnir .helpers import device_mapper
3+ from ncclient import manager
24
35
46def netconf_get_config (
57 task : Task ,
6- source : str = "candidate " ,
8+ source : str = "running " ,
79 nc_filter : str = None ,
810 nc_filter_type : str = None ,
911) -> Result :
@@ -15,19 +17,31 @@ def netconf_get_config(
1517 :params nc_filter_type: type str
1618 :returns: nornir result object
1719 """
18- manager = task .host .get_connection (
19- connection = "netconf" , configuration = task .nornir .config
20- )
21- if nc_filter and nc_filter_type :
22- result = manager .get_config (source = source , filter = (nc_filter_type , nc_filter ))
23- else :
24- result = manager .get_config (source = source )
20+ device_params = {
21+ "host" : task .host .hostname ,
22+ "port" : task .host .port or 830 ,
23+ "username" : task .host .username ,
24+ "password" : task .host .password ,
25+ "hostkey_verify" : False ,
26+ "device_params" : {
27+ "name" : device_mapper (os_type = task .host .data ["os" ], proto = "netconf" )
28+ },
29+ }
30+
31+ with manager .connect (** device_params ) as conn :
32+ if nc_filter and nc_filter_type :
33+ with open (nc_filter ) as xml :
34+ nc_filter = xml .read ()
35+
36+ result = conn .get_config (source = source , filter = (nc_filter_type , nc_filter ))
37+ else :
38+ result = conn .get_config (source = source )
2539
2640 return Result (result = result , host = task .host )
2741
2842
2943def netconf_edit_config (
30- task : Task , target : str = "candidate " , nc_config : str = None
44+ task : Task , target : str = "running " , nc_config : str = None
3145) -> Result :
3246 """nornir netconf edit config task
3347
@@ -36,26 +50,37 @@ def netconf_edit_config(
3650 :params nc_config: type str - yang config model
3751 :returns: nornir result object
3852 """
39- manager = task .host .get_connection (
40- connection = "netconf" , configuration = task .nornir .config
41- )
42- with manager .lock (target = target ):
43- config_response = manager .edit_config (target = target , config = nc_config )
44- config_validate = manager .validate (source = target )
53+ device_params = {
54+ "host" : task .host .hostname ,
55+ "port" : task .host .port or 830 ,
56+ "username" : task .host .username ,
57+ "password" : task .host .password ,
58+ "hostkey_verify" : False ,
59+ "device_params" : {
60+ "name" : device_mapper (os_type = task .host .data ["os" ], proto = "netconf" )
61+ },
62+ }
63+
64+ with manager .connect (** device_params ) as conn :
65+ with open (nc_config ) as xml :
66+ nc_config = xml .read ()
67+
68+ config_response = conn .edit_config (target = target , config = nc_config )
69+ config_validate = conn .validate (source = target )
4570
4671 if config_response .ok and config_validate .ok :
4772 result = {
4873 "config_response" : config_response .ok ,
4974 "config_validate" : config_validate .ok ,
5075 }
5176 failed = False
52- manager .commit ()
77+ conn .commit ()
5378 else :
5479 result = {
5580 "config_response" : config_response .error ,
5681 "config_validate" : config_validate .error ,
5782 }
5883 failed = True
59- manager .discard_changes ()
84+ conn .discard_changes ()
6085
6186 return Result (result = result , host = task .host , failed = failed )
0 commit comments