Question

I'm using python-digitalocean to create droplets on DO. I cannot attach ssh keys to droplets. Any ideea why ?

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 !

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Did something change to the API or … ?

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>]

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 integer
  • b6: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 account

If 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.