83
83
token:
84
84
required: False
85
85
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
88
90
env:
89
91
# in order of precedence
90
92
- name: NETBOX_TOKEN
336
338
# - "time_zone_utc_minus_7"
337
339
# - "time_zone_utc_plus_1"
338
340
# - "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
339
349
"""
340
350
341
351
import json
@@ -1975,11 +1985,7 @@ def main(self):
1975
1985
host = hostname ,
1976
1986
)
1977
1987
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 ):
1983
1989
# NetBox access
1984
1990
if version .parse (ansible_version ) < version .parse ("2.11" ):
1985
1991
token = self .get_option ("token" )
@@ -1988,6 +1994,19 @@ def parse(self, inventory, loader, path, cache=True):
1988
1994
token = self .templar .template (
1989
1995
self .get_option ("token" ), fail_on_undefined = False
1990
1996
)
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
1991
2010
1992
2011
# Handle extra "/" from api_endpoint configuration and trim if necessary, see PR#49943
1993
2012
self .api_endpoint = self .get_option ("api_endpoint" ).strip ("/" )
@@ -2013,8 +2032,8 @@ def parse(self, inventory, loader, path, cache=True):
2013
2032
self .cert = self .get_option ("cert" )
2014
2033
self .key = self .get_option ("key" )
2015
2034
self .ca_path = self .get_option ("ca_path" )
2016
- if token :
2017
- self .headers . update ({ "Authorization" : "Token %s" % token } )
2035
+
2036
+ self ._set_authorization ( )
2018
2037
2019
2038
# Filter and group_by options
2020
2039
self .group_by = self .get_option ("group_by" )
0 commit comments