Package: droplets
dropletsdropletsaids in the management of Digital Ocean (DO) droplets.dropletsalso has the
ability to act as an inventory script for an Ansible playbook.UsingdropletsThe basic command line usage allows you to build your own script to connect to your
DO environments. The script serve two purposes. First, the creation and
destruction of droplets based on defined blueprint. Second, is to act as an
Ansible invetory script for those droplets.Creating the scriptUsingdropletsyou will define your own script for your different DO deployments.
In your script you will define ablueprint.Calling mainmainrequires a blueprint and also accepts some optional arguments.image(default: ubuntu-14-04-x64)size(default: 512mb)region(default: nyc3)prefix(default: '')api_token(default: None)The defaults will be used for every instance created unless you explicitly override
the arguments in your call to main or in the group definition of your blueprint.prefixis useful when creating scripts for different DO envrionments.prefixwill
be used when generating the names for the instances of your groups. For example if you
callmainwithprefix=staging-and you have anappgroup defined in your blueprint,
your instances will have the namestaging-app00,staging-app01, etc...Defining a blueprintA blueprint is a standard python dictionary. A blueprint should contain a key entry for
every group. The value of each group should be another dictionary that will act
as a key/value store for hostvars that will be passed to an ansible playbook.A group can contain some special keys which override the default values ofmain.size(default: 512mb)image(default: ubuntu-14-04-x64)region(default: nyc3)n(number of instances, default: 1)Typical group names are things likeapp,db, andloadbalancer. Thedropletscript
will use your group name along with theprefix(see below) when creating instance namesAPI TokenYou must also provide an API token. There are two ways to do this. First, you can pass
theapi_tokenkeyword argument to themaincall. Second, you can set the environment
variable 'DIGITAL_OCEAN_TOKEN' to your token value. The later option is useful if you
intend to store your droplet script in a public repository.ExampleHere is a very simple example of a single application server and single database server blueprint.
They override none of the default values and theappgroup passes the hostvargit_branchto the playbook.#!/usr/bin/env pythonimportsysfromdropletsimportmainblueprint={'app':{'git_branch':'master',},'db':{},}if__name__=='__main__':main(blueprint,api_token='XXX',prefix='staging-')Running the scriptNow with this staging script defined, we can issue commands on it directly. For
example./staging --help

Options:
    -h --help    Show this screen
    --list       Outputs inventory in json.
    --human      Outputs inventory in format suitable for human beings.
    --reconcile  Creates/deletes droplets as necessary to match blueprint.
    --hostkeys   Gets ssh host keys for hosts in inventory and install them in
                ~/.ssh/known_hosts
    --destroy=<group>    Destroys the droplets in the specified group.By default, the script will execute as if you have passed in the--listoption. This allows the
droplets script to at as an inventory script for the ansible-playbook command.If we wanted to create these instances to prepare for a playbook run we would use the--reconcileoption. It is
worth mentioning that--reconcilewill not retroactively reconcile changes tosize,image, orregion.
You will need to login to DO to make adjustments to size OR use--destroyon the group and then--reconsile.Running with ansible-playbookNow that you have your DO instances deployed how you want (using--reconcile). You can use your droplets script
as an inventory script for your playbook.ansible-playbook -i staging deploy-staging.yaml