1- from netnir .constants import (
2- HOSTVARS ,
3- GROUPVARS ,
4- TEMPLATES ,
5- DOMAIN ,
6- NETNIR_USER ,
7- NETNIR_PASS ,
8- )
9- from netnir .helpers import device_mapper
101from nornir .core .deserializer .inventory import Inventory
112import os
123import yaml
134
145
15- """dynamic inventory builder class
16- """
6+ """dynamic inventory builder class"""
177
188
199class NornirInventory (Inventory ):
@@ -31,7 +21,13 @@ def __init__(self, **kwargs):
3121 )
3222
3323 def nhosts (self ):
24+ from netnir .helpers import device_mapper
3425 from netnir .core .credentials import Credentials
26+ from netnir .constants import (
27+ HOSTVARS ,
28+ TEMPLATES ,
29+ DOMAIN ,
30+ )
3531
3632 """
3733 load devices from host_vars and load them into the nornir inventory schema
@@ -48,8 +44,8 @@ def nhosts(self):
4844 )
4945 data [host ] = {
5046 "hostname" : f"{ host } .{ domain } " if domain else host ,
51- "username" : os . environ . get ( NETNIR_USER , creds ["username" ]) ,
52- "password" : os . environ . get ( NETNIR_PASS , creds ["password" ]) ,
47+ "username" : creds ["username" ],
48+ "password" : creds ["password" ],
5349 "port" : host_vars .get ("port" , 22 ),
5450 "platform" : device_mapper (host_vars ["os" ]),
5551 "groups" : host_vars .get ("groups" , list ()),
@@ -68,8 +64,8 @@ def nhosts(self):
6864 "connection_options" : {
6965 "netconf" : {
7066 "hostname" : f"{ host } .{ domain } " if domain else host ,
71- "username" : os . environ . get ( NETNIR_USER , creds ["username" ]) ,
72- "password" : os . environ . get ( NETNIR_PASS , creds ["password" ]) ,
67+ "username" : creds ["username" ],
68+ "password" : creds ["password" ],
7369 "platform" : host_vars .get ("os" ),
7470 "port" : host_vars .get ("port" , 830 ),
7571 "extras" : {"hostkey_verify" : False },
@@ -83,6 +79,8 @@ def ngroups(self):
8379 """
8480 load groups from group_vars and load them into the nornir inventory schema
8581 """
82+ from netnir .constants import GROUPVARS
83+
8684 data = dict ()
8785 groups = os .listdir (os .path .expanduser (GROUPVARS ))
8886 if "all" in groups :
@@ -101,6 +99,8 @@ def ndefaults(self):
10199 """
102100 load the defaults from group_vars/all and load them into the nornir inventory schema
103101 """
102+ from netnir .constants import GROUPVARS
103+
104104 if os .path .isfile (os .path .expanduser (GROUPVARS ) + "/all" ):
105105 default_vars = yaml .load (
106106 open (os .path .expanduser (GROUPVARS ) + "/all" ), Loader = yaml .SafeLoader ,
0 commit comments