8383 token:
8484 required: False
8585 description:
86- - NetBox API token to be able to read against NetBox.
87- - This may not be required depending on the NetBox setup.
86+ - NetBox API token to be able to read against NetBox.
87+ - This may not be required depending on the NetBox setup.
88+ - You can provide a "type" and "value" for a token if your NetBox deployment is using a more advanced authentication like OAUTH.
89+ - If you do not provide a "type" and "value" parameter, the HTTP authorization header will be set to "Token", which is the NetBox default
8890 env:
8991 # in order of precedence
9092 - name: NETBOX_TOKEN
336338# - "time_zone_utc_minus_7"
337339# - "time_zone_utc_plus_1"
338340# - "time_zone_utc_plus_10"
341+
342+ # Example of using a token type
343+
344+ plugin: netbox.netbox.nb_inventory
345+ api_endpoint: http://localhost:8000
346+ token:
347+ type: Bearer
348+ value: test123456
339349"""
340350
341351import json
@@ -1975,11 +1985,7 @@ def main(self):
19751985 host = hostname ,
19761986 )
19771987
1978- def parse (self , inventory , loader , path , cache = True ):
1979- super (InventoryModule , self ).parse (inventory , loader , path )
1980- self ._read_config_data (path = path )
1981- self .use_cache = cache
1982-
1988+ def _set_authorization (self ):
19831989 # NetBox access
19841990 if version .parse (ansible_version ) < version .parse ("2.11" ):
19851991 token = self .get_option ("token" )
@@ -1988,6 +1994,19 @@ def parse(self, inventory, loader, path, cache=True):
19881994 token = self .templar .template (
19891995 self .get_option ("token" ), fail_on_undefined = False
19901996 )
1997+ if token :
1998+ # check if token is new format
1999+ if isinstance (token , dict ):
2000+ self .headers .update (
2001+ {"Authorization" : f"{ token ['type' ].capitalize ()} { token ['value' ]} " }
2002+ )
2003+ else :
2004+ self .headers .update ({"Authorization" : "Token %s" % token })
2005+
2006+ def parse (self , inventory , loader , path , cache = True ):
2007+ super (InventoryModule , self ).parse (inventory , loader , path )
2008+ self ._read_config_data (path = path )
2009+ self .use_cache = cache
19912010
19922011 # Handle extra "/" from api_endpoint configuration and trim if necessary, see PR#49943
19932012 self .api_endpoint = self .get_option ("api_endpoint" ).strip ("/" )
@@ -2013,8 +2032,8 @@ def parse(self, inventory, loader, path, cache=True):
20132032 self .cert = self .get_option ("cert" )
20142033 self .key = self .get_option ("key" )
20152034 self .ca_path = self .get_option ("ca_path" )
2016- if token :
2017- self .headers . update ({ "Authorization" : "Token %s" % token } )
2035+
2036+ self ._set_authorization ( )
20182037
20192038 # Filter and group_by options
20202039 self .group_by = self .get_option ("group_by" )
0 commit comments