# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION='''
---
module: digital_ocean_droplet
short_description: Create/delete a droplet in DigitalOcean
description:
- Create/delete a droplet in DigitalOcean and optionally waits for it to be 'running'.
version_added: "1.6"
options:
state:
description:
- Indicate desired state of the target.
default: present
choices: ['present', 'absent']
client_id:
description:
- Digital Ocean manager id.
api_key:
description:
- Digital Ocean api key.
id:
description:
- Numeric, the droplet id you want to operate on.
name:
description:
- String, this is the name of the droplet - must be formatted by hostname rules.
unique_name:
description:
- Bool, require unique hostnames. By default, digital ocean allows multiple hosts with the same name. Setting this to "yes" allows only one host per name. Useful for idempotence.
default: "no"
choices: [ "yes", "no" ]
size_id:
description:
- Numeric, this is the id of the size you would like the droplet created at.
image_id:
description:
- Numeric, this is the id of the image you would like the droplet created with.
region_id:
description:
- "Numeric, this is the id of the region you would like your server"
ssh_key_ids:
description:
- Optional, comma separated list of ssh_key_ids that you would like to be added to the server
wait:
description:
- Wait for the droplet to be in state 'running' before returning. If wait is "no" an ip_address may not be returned.
default: "yes"
choices: [ "yes", "no" ]
wait_timeout:
description:
- How long before wait gives up, in seconds.
default: 300
notes:
- Two environment variables can be used, DO_CLIENT_ID and DO_API_KEY.
'''
EXAMPLES='''
# Create a new Droplet
# Will return the droplet details including the droplet id (used for idempotence)
- digital_ocean_droplet: >
state=present
name=my_new_droplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
wait_timeout=500
register: my_droplet
- debug: msg="ID is {{ my_droplet.droplet.id }}"
- debug: msg="IP is {{ my_droplet.droplet.ip_address }}"
# Ensure a droplet is present
# If droplet id already exist, will return the droplet details and changed = False
# If no droplet matches the id, a new droplet will be created and the droplet details (including the new id) are returned, changed = True.
- digital_ocean_droplet: >
state=present
id=123
name=my_new_droplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
wait_timeout=500
# Create a droplet with ssh key
# The ssh key id can be passed as argument at the creation of a droplet (see ssh_key_ids).
# Several keys can be added to ssh_key_ids as id1,id2,id3
# The keys are used to connect as root to the droplet.
- digital_ocean_droplet: >
state=present
ssh_key_ids=id1,id2
name=my_new_droplet
client_id=XXX
api_key=XXX
size_id=1
region_id=2
image_id=3
'''
importsys
importos
importtime
try:
fromdopy.managerimportDoError,DoManager
exceptImportErrorase:
print"failed=True msg='dopy required for this module'"
sys.exit(1)
classTimeoutError(DoError):
def__init__(self,msg,id):
super(TimeoutError,self).__init__(msg)
self.id=id
classJsonfyMixIn(object):
defto_json(self):
returnself.__dict__
classDroplet(JsonfyMixIn):
manager=None
def__init__(self,droplet_json):
self.status='new'
self.__dict__.update(droplet_json)
defis_powered_on(self):
returnself.status=='active'
defupdate_attr(self,attrs=None):
ifattrs:
fork,vinattrs.iteritems():
setattr(self,k,v)
else:
json=self.manager.show_droplet(self.id)
ifjson['ip_address']:
self.update_attr(json)
defpower_on(self):
assertself.status=='off','Can only power on a closed one.'