Package: python-digitalocean-ssh
Digital ocean -> ssh configInstallationpython3 -m pip install python-digitalocean-sshRunning standalone, this python 3 script will help you keep your ssh config in sync with your digital ocean droplets$python3-mdigitalocean_sshproduction

·Reading/home/alex/.config/python-digitalocean-ssh/production.json
·Parsing/home/alex/.ssh/config
·FetchingdropletsfromDO
·Writingintoyoursshconfigfile

✓Done,11dropletssyncedFeaturesSupports different ssh keys for each droplet, depending on the DO tags of the dropletWorks with different configurations and can write in different sections of your ssh configHow toStep 1: Create the json configuration fileSave this at~/.config/python-digitalocean-ssh/<name>.json, where<name>is how you want to call it, e.g.productionortestingor anything else. For this example I will useproduction.{"token":"DIGITAL_OCEAN_READ_ONLY_TOKEN_HERE","keys":{"tagToKey":{},"default":{"key":"common","priority":0}},"startMark":"# DO production","endMark":"# /DO production","hostPrefix":"do-prod-"}Note: This is the simplest possible configuration file that uses the same key for every droplet and the droplet name asHost, for more options, read on.Generate a new personal DO API read-only access tokenherehostPrefixis what prefix to add in theHostkey in your ssh config for each droplet loaded through this configuration, can be anything you wantStep 2: Add the 2 marks in your ssh configThe above json configuration contains thestartMarkandendMark. These should be somewhere inside your ssh configuration and can be whatever you want (start with#for ssh config comments, though):# DO production
# /DO productionBetween these 2 marks the script willdeleteeverything and add the new entries. Be careful not to add your own hosts between these 2 marks.Step 3: Run the script$python3-mdigitalocean_sshproduction

·Reading/home/alex/.config/python-digitalocean-ssh/production.json
·Parsing/home/alex/.ssh/config
·FetchingdropletsfromDO
·Writingintoyoursshconfigfile

✓Done,11dropletssyncedNow your ssh config will look like this:# DO production
Host do-prod-control-center1517024146
    # control-center1517024146
    Hostname X.X.X.X
    IdentityFile ~/.ssh/common
    User user
Host do-prod-control-center1517027030
    # control-center1517027030
    Hostname X.X.X.X
    IdentityFile ~/.ssh/common
    User user
... 9 more entries
# /DO productionIf you have autogenerated uglyHostnames derived from the droplet names, you can make it work with the droplet tags instead; read on.Use this as a moduleWith the configuration files at the appropriate place, you can use this as a module to create powerful python scripts:fromdigitalocean_sshimportDOimportsysclient=DO(True)# enable debuggingconfig_type=sys.argv[1]# must pass the configuration type as an argument, e.g. 'production'config=client.get_config(config_type)ssh_config=client.parse_ssh_config(config)droplets=client.fetch_droplets(config)print(droplets)# DO droplets with combined ip/tags/ssh config informationI want to use a different ssh key, notcommon!Change thekeys.default.keysettingI want to use a different ssh key per droplet tag!Change thekeys.tagToKeysetting and add in it entries like:"control-center":{"key":"cc_prv","priority":7},"consul-server":{"key":"cs_prv","priority":6},"postgres-master":{"key":"common","priority":5}The final config will look like this:{"token":"DIGITAL_OCEAN_READ_ONLY_TOKEN_HERE","keys":{"tagToKey":{"control-center":{"key":"cc_prv","priority":7},"consul-server":{"key":"cs_prv","priority":6},"postgres-master":{"key":"common","priority":5}},"default":{"key":"common","priority":0}},"startMark":"# DO production","endMark":"# /DO production","hostPrefix":"do-prod-"}Important: A droplet can have more than 1 tag, that's why there's a field calledprioritythere. In the above example, if a droplet has both thecontrol-centerandconsul-servertags, it will use the key with the higher priority (herecontrol-center). If a droplet has no tags or its tags do not appear intagToKey, it will use the default key.For the droplets that match a specific tag, now theHostin the ssh config will have the name of the tag, not the droplet name:# DO production
Host do-prod-control-center
    # control-center1517024146
    Hostname X.X.X.X
    IdentityFile ~/.ssh/cc_prv
    User user
Host do-prod-control-center2
    # control-center1517027030
    Hostname X.X.X.X
    IdentityFile ~/.ssh/cc_prv
    User user
... more entries
# /DO productionThis is convenient for large environments where the droplet names are autogeneratedNote: The droplet name is still visible as a comment in the first line of each entryNote: As shown in the above example, if 2 or more droplets share the same tag, an ascending number is appended to theHostvalue.Now you can see everything easily using ssh's tab completion, and connect anywhere:$ ssh do-prod- <hit TAB key twice>

do-prod-control-center   do-prod-mongodb  do-prod-load-balancer    do-prod-nodejs2          do-prod-postgres-slave   do-prod-blog
do-prod-control-center2  do-prod-landing-page     do-prod-nodejs           do-prod-postgres-master  do-prod-redisI have productionandtesting and I work in 10 different companies!Simply create different configuration files under~/.config/python-digitalocean-ssh/, one for each use case of yours, likeproduction.jsonandtesting.json. It will be useful to have a differenthostPrefixfor each use case.Also, add the different markings in your ssh config file, e.g.:# DO production
# /DO production

# DO testing
# /DO testingNow if you run$python3-mdigitalocean_sshproductionit will go on and read fromproduction.jsonand write in the corresponding marking inside your ssh config. And if you run$python3-mdigitalocean_sshtestingit will go on and read fromtesting.jsonand write in the corresponding marking.Can I safely re-run the script any times I want?Yes, provided that you haven't included any entries of yours between the markings you've specified in the configuration. Everything between the markings is deleted each time the script runs.