By sorinsrn7
I’m trying to create droplets with SSH key using the python-digitalocean library. For some reasons the ssh key is not attached to the newly created droplet.
Any idea what can I do ?| Thanks !
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
python-digitalocean accepts SSH keys in multiple formats when creating a new Droplet. One important thing to remember is that even if you are only adding one key, the attribute needs to be in list form.
Here’s a quick example:
import os
import digitalocean
token = os.getenv('DO_TOKEN')
client = digitalocean.Manager(token=token)
keys = client.get_all_sshkeys()
droplet = digitalocean.Droplet(name='ssh-key-example-01',
region='nyc3',
size='s-1vcpu-1gb',
image='ubuntu-18-04-x64',
ssh_keys=[keys[0],
3939600,
'b6:2e:fa:ea:96:42:14:1b:62:e2:76:4b:6f:60:1a:e6'],
token=token)
This example highlights the three forms that are accepted:
keys[0] is an SSHKey object that was retrieved using client.get_all_sshkeys()3939600 is the ID of an SSH key on my DigitalOcean account as an integerb6:2e:fa:ea:96:42:14:1b:62:e2:76:4b:6f:60:1a:e6 is the fingerprint of an SSH key on my DigitalOcean accountIf you’d like to add all of the keys on you account to a Droplet, the easiest way would be:
keys = client.get_all_sshkeys()
droplet = digitalocean.Droplet(name='ssh-key-example-01',
region='nyc3',
size='s-1vcpu-1gb',
image='ubuntu-18-04-x64',
ssh_keys=keys,
token=token)
This works since client.get_all_sshkeys() already returns them in a list.
I’ve already tried the second approach presented and doesn’t work.
[nav] In [80]: try:
...: keys = manager.get_all_sshkeys()
...: droplet = digitalocean.Droplet(token='XXXXXXXXXXXXXXXXXXX', name='SRV00', region='ams3', image='ubuntu-16-04-x32', ssh_keys=keys , size_slug='512mb', backups=False)
...: droplet.create()
...: except Exception as e:
...: print('Err: {}'.format(e))
Then:
[nav] In [81]: for d in manager.get_all_droplets():
...: print('Droplet IP: {}, SSH KEYS: {} ...'.format(d.ip_address, d.ssh_keys ))
Result:
Droplet IP: AA.BB.CC.DD, SSH KEYS: [] …
I do have a key on DO:
[ins] In [83]: manager.get_all_sshkeys()
Out[83]: [<SSHKey: 24341650 srv00>]
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.