Trying to add secrets with pynetbox library #13409
Replies: 3 comments 5 replies
-
Is this due to the fact that |
Beta Was this translation helpful? Give feedback.
-
You are using an older version of pynetbox with the newer version of secrets and also expecting the code from v2 NetBox to work with v3 NetBox. The approach is wrong in all fronts here. First of all v1.8.3 is not the latest version. We had released v1.8.5 almost 2 months ago and it the most current version as of now. Secondly, there has been several changes in the API as Here is a sample code, beyond this you will have to tailor the code for you own requirements (Note: I have written this code by reading the Secrets API schema and the pynetbox documentation) import pynetbox
import requests
nb = pynetbox.api(...)
session_key = nb.plugins.secrets.session_keys.create(preserve_key=True, private_key='--- KEY ---')
session = requests.Session()
session.headers = {'X-Session-Key': session_key.session_key}
nb.http_session = session
nb.plugins.secrets.secrets.create(assigned_object_type='dcim.device', assigned_object_id=device.id, plaintext='pass') |
Beta Was this translation helpful? Give feedback.
-
I had the same kind of problem and I came up with the conclusion that nb.plugins.secrets.session_keys.create() is simply not working so went back to requests directly : class NetBoxTokenContext:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I have the last version of netbox 3.5.7) installed in my virtual machine (with ubuntu 22.04) and the last version of netbox secrets plugin (1.8.3) and i am trying to migrate the secrets of an older version of netbox (2.8.6). I want to migrate the information using a script in python that uses pynetbox library to interact with the netbox API and i would like to know how could i add a secret from the script (the script will take the users and passwords from an excel and should add the secrets into the plugin of the new netbox version). I have created an user with a ssesion key in the new version and i have the private key of the user which i can add secrets. I have the script done (it should work because i used this script with the older netbox version and it worked), but the problem es that when i try to add the secret appears the following error:
Traceback (most recent call last):
File "prueba.py", line 50, in
r=nb.plugins.secrets.secrets.get(id=1)
File "/usr/local/lib/python3.6/dist-packages/pynetbox/core/app.py", line 67, in getattr
self._set_session_key()
File "/usr/local/lib/python3.6/dist-packages/pynetbox/core/app.py", line 78, in _set_session_key
http_session=self.api.http_session,
File "/usr/local/lib/python3.6/dist-packages/pynetbox/core/query.py", line 225, in get_session_key
raise RequestError(req)
pynetbox.core.query.RequestError: The requested url: http://192.168.27.203:8000/api/secrets/get-session-key/?preserve_key=True could not be found.
I have checked the url and everything is okey (to add secrets i have to use the url api/plugins/secrets/secrets and the code is something really easy. Only we have to create the connection with the api (nb= pynetbox.api(url=url, token=token, private_key=secret_key)) and after that create the secret: nb.plugins.secrets.secrets.create( and there the secret required fields, like object_id....)
Can you help me?
Thank you so much.
Best Regards.
Pedro
Beta Was this translation helpful? Give feedback.
All reactions