2013-12-12 05:23:58 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ec2_key
|
2013-12-13 01:59:52 +01:00
|
|
|
version_added: "1.5"
|
2013-12-12 05:23:58 +01:00
|
|
|
short_description: maintain an ec2 key pair.
|
|
|
|
description:
|
|
|
|
- maintains ec2 key pairs. This module has a dependency on python-boto >= 2.5
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the key pair.
|
|
|
|
required: true
|
|
|
|
key_material:
|
|
|
|
description:
|
|
|
|
- Public key material.
|
|
|
|
required: false
|
|
|
|
region:
|
|
|
|
description:
|
|
|
|
- the EC2 region to use
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
state:
|
|
|
|
description:
|
2013-12-13 03:10:44 +01:00
|
|
|
- create or delete keypair
|
2013-12-12 05:23:58 +01:00
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
aliases: []
|
2014-04-02 05:14:35 +02:00
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- Wait for the specified action to complete before returning.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
aliases: []
|
|
|
|
version_added: "1.6"
|
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
- How long before wait gives up, in seconds
|
|
|
|
required: false
|
|
|
|
default: 300
|
|
|
|
aliases: []
|
|
|
|
version_added: "1.6"
|
2013-12-12 05:23:58 +01:00
|
|
|
|
2014-04-09 08:43:55 +02:00
|
|
|
extends_documentation_fragment: aws
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Vincent Viallet (@zbal)"
|
2013-12-12 05:23:58 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-12-13 03:10:44 +01:00
|
|
|
# Note: None of these examples set aws_access_key, aws_secret_key, or region.
|
|
|
|
# It is assumed that their matching environment variables are set.
|
|
|
|
|
2013-12-12 05:23:58 +01:00
|
|
|
# Creates a new ec2 key pair named `example` if not present, returns generated
|
|
|
|
# private key
|
|
|
|
- name: example ec2 key
|
2014-12-01 21:14:57 +01:00
|
|
|
ec2_key:
|
2013-12-12 05:23:58 +01:00
|
|
|
name: example
|
|
|
|
|
|
|
|
# Creates a new ec2 key pair named `example` if not present using provided key
|
2014-12-01 21:14:57 +01:00
|
|
|
# material. This could use the 'file' lookup plugin to pull this off disk.
|
2013-12-12 05:23:58 +01:00
|
|
|
- name: example2 ec2 key
|
2014-12-01 21:14:57 +01:00
|
|
|
ec2_key:
|
2013-12-12 05:23:58 +01:00
|
|
|
name: example2
|
|
|
|
key_material: 'ssh-rsa AAAAxyz...== me@example.com'
|
|
|
|
state: present
|
|
|
|
|
|
|
|
# Creates a new ec2 key pair named `example` if not present using provided key
|
|
|
|
# material
|
|
|
|
- name: example3 ec2 key
|
2014-12-01 21:14:57 +01:00
|
|
|
ec2_key:
|
2013-12-12 05:23:58 +01:00
|
|
|
name: example3
|
|
|
|
key_material: "{{ item }}"
|
|
|
|
with_file: /path/to/public_key.id_rsa.pub
|
|
|
|
|
|
|
|
# Removes ec2 key pair by name
|
|
|
|
- name: remove example key
|
2014-12-01 21:14:57 +01:00
|
|
|
ec2_key:
|
2013-12-12 05:23:58 +01:00
|
|
|
name: example
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2013-12-12 05:23:58 +01:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
2013-12-12 05:23:58 +01:00
|
|
|
|
2014-04-03 19:53:43 +02:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
|
|
|
|
2013-12-12 05:23:58 +01:00
|
|
|
def main():
|
2014-02-05 12:11:06 +01:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2013-12-12 05:23:58 +01:00
|
|
|
name=dict(required=True),
|
|
|
|
key_material=dict(required=False),
|
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
2014-04-02 05:14:35 +02:00
|
|
|
wait = dict(type='bool', default=False),
|
|
|
|
wait_timeout = dict(default=300),
|
2014-02-05 12:11:06 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2013-12-12 05:23:58 +01:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2013-12-12 05:23:58 +01:00
|
|
|
name = module.params['name']
|
|
|
|
state = module.params.get('state')
|
|
|
|
key_material = module.params.get('key_material')
|
2014-04-02 05:14:35 +02:00
|
|
|
wait = module.params.get('wait')
|
|
|
|
wait_timeout = int(module.params.get('wait_timeout'))
|
2013-12-12 05:23:58 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2013-12-13 03:10:44 +01:00
|
|
|
ec2 = ec2_connect(module)
|
2013-12-12 05:23:58 +01:00
|
|
|
|
|
|
|
# find the key if present
|
|
|
|
key = ec2.get_key_pair(name)
|
|
|
|
|
|
|
|
# Ensure requested key is absent
|
|
|
|
if state == 'absent':
|
|
|
|
if key:
|
|
|
|
'''found a match, delete it'''
|
2015-07-16 17:36:40 +02:00
|
|
|
if not module.check_mode:
|
|
|
|
try:
|
|
|
|
key.delete()
|
|
|
|
if wait:
|
|
|
|
start = time.time()
|
|
|
|
action_complete = False
|
|
|
|
while (time.time() - start) < wait_timeout:
|
|
|
|
if not ec2.get_key_pair(name):
|
|
|
|
action_complete = True
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
if not action_complete:
|
|
|
|
module.fail_json(msg="timed out while waiting for the key to be removed")
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="Unable to delete key pair '%s' - %s" % (key, e))
|
|
|
|
key = None
|
|
|
|
changed = True
|
2013-12-12 05:23:58 +01:00
|
|
|
|
2013-12-12 05:30:22 +01:00
|
|
|
# Ensure requested key is present
|
2013-12-12 05:23:58 +01:00
|
|
|
elif state == 'present':
|
|
|
|
if key:
|
2014-04-03 19:53:43 +02:00
|
|
|
# existing key found
|
|
|
|
if key_material:
|
|
|
|
# EC2's fingerprints are non-trivial to generate, so push this key
|
|
|
|
# to a temporary name and make ec2 calculate the fingerprint for us.
|
|
|
|
#
|
|
|
|
# http://blog.jbrowne.com/?p=23
|
|
|
|
# https://forums.aws.amazon.com/thread.jspa?messageID=352828
|
|
|
|
|
|
|
|
# find an unused name
|
|
|
|
test = 'empty'
|
|
|
|
while test:
|
|
|
|
randomchars = [random.choice(string.ascii_letters + string.digits) for x in range(0,10)]
|
|
|
|
tmpkeyname = "ansible-" + ''.join(randomchars)
|
|
|
|
test = ec2.get_key_pair(tmpkeyname)
|
|
|
|
|
|
|
|
# create tmp key
|
|
|
|
tmpkey = ec2.import_key_pair(tmpkeyname, key_material)
|
|
|
|
# get tmp key fingerprint
|
|
|
|
tmpfingerprint = tmpkey.fingerprint
|
|
|
|
# delete tmp key
|
|
|
|
tmpkey.delete()
|
|
|
|
|
|
|
|
if key.fingerprint != tmpfingerprint:
|
|
|
|
if not module.check_mode:
|
|
|
|
key.delete()
|
|
|
|
key = ec2.import_key_pair(name, key_material)
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
start = time.time()
|
|
|
|
action_complete = False
|
|
|
|
while (time.time() - start) < wait_timeout:
|
|
|
|
if ec2.get_key_pair(name):
|
|
|
|
action_complete = True
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
if not action_complete:
|
|
|
|
module.fail_json(msg="timed out while waiting for the key to be re-created")
|
|
|
|
|
|
|
|
changed = True
|
2013-12-12 05:23:58 +01:00
|
|
|
pass
|
|
|
|
|
2013-12-12 05:30:22 +01:00
|
|
|
# if the key doesn't exist, create it now
|
2013-12-12 05:23:58 +01:00
|
|
|
else:
|
|
|
|
'''no match found, create it'''
|
|
|
|
if not module.check_mode:
|
|
|
|
if key_material:
|
|
|
|
'''We are providing the key, need to import'''
|
|
|
|
key = ec2.import_key_pair(name, key_material)
|
|
|
|
else:
|
|
|
|
'''
|
|
|
|
No material provided, let AWS handle the key creation and
|
|
|
|
retrieve the private key
|
|
|
|
'''
|
|
|
|
key = ec2.create_key_pair(name)
|
2014-04-02 05:14:35 +02:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
start = time.time()
|
|
|
|
action_complete = False
|
|
|
|
while (time.time() - start) < wait_timeout:
|
|
|
|
if ec2.get_key_pair(name):
|
|
|
|
action_complete = True
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
if not action_complete:
|
|
|
|
module.fail_json(msg="timed out while waiting for the key to be created")
|
|
|
|
|
2013-12-12 05:23:58 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
if key:
|
|
|
|
data = {
|
|
|
|
'name': key.name,
|
|
|
|
'fingerprint': key.fingerprint
|
|
|
|
}
|
|
|
|
if key.material:
|
|
|
|
data.update({'private_key': key.material})
|
|
|
|
|
|
|
|
module.exit_json(changed=changed, key=data)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=changed, key=None)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
main()
|