2013-12-12 04:23:58 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ec2_key
|
2013-12-13 00:59:52 +00:00
|
|
|
version_added: "1.5"
|
2013-12-12 04:23:58 +00: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: []
|
|
|
|
ec2_url:
|
|
|
|
description:
|
|
|
|
- Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints)
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
ec2_secret_key:
|
|
|
|
description:
|
|
|
|
- EC2 secret key
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-12-13 02:10:44 +00:00
|
|
|
aliases: ['aws_secret_key', 'secret_key']
|
2013-12-12 04:23:58 +00:00
|
|
|
ec2_access_key:
|
|
|
|
description:
|
|
|
|
- EC2 access key
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-12-13 02:10:44 +00:00
|
|
|
aliases: ['aws_access_key', 'access_key']
|
2013-12-12 04:23:58 +00:00
|
|
|
state:
|
|
|
|
description:
|
2013-12-13 02:10:44 +00:00
|
|
|
- create or delete keypair
|
2013-12-12 04:23:58 +00:00
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
aliases: []
|
2014-02-13 18:12:08 +00:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
|
|
|
choices: ["yes", "no"]
|
|
|
|
aliases: []
|
|
|
|
version_added: "1.5"
|
2014-02-05 11:11:06 +00:00
|
|
|
profile:
|
|
|
|
description:
|
|
|
|
- uses a boto profile. Only works with boto >= 2.24.0
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-03-11 15:48:16 +00:00
|
|
|
version_added: "1.6"
|
2014-02-05 11:11:06 +00:00
|
|
|
security_token:
|
|
|
|
description:
|
|
|
|
- security token to authenticate against AWS
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-03-11 15:48:16 +00:00
|
|
|
version_added: "1.6"
|
2013-12-12 04:23:58 +00:00
|
|
|
|
|
|
|
requirements: [ "boto" ]
|
|
|
|
author: Vincent Viallet
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-12-13 02:10:44 +00: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 04:23:58 +00:00
|
|
|
# Creates a new ec2 key pair named `example` if not present, returns generated
|
|
|
|
# private key
|
|
|
|
- name: example ec2 key
|
|
|
|
local_action:
|
|
|
|
module: ec2_key
|
|
|
|
name: example
|
|
|
|
|
|
|
|
# Creates a new ec2 key pair named `example` if not present using provided key
|
|
|
|
# material
|
|
|
|
- name: example2 ec2 key
|
|
|
|
local_action:
|
|
|
|
module: ec2_key
|
|
|
|
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
|
|
|
|
local_action:
|
|
|
|
module: ec2_key
|
|
|
|
name: example3
|
|
|
|
key_material: "{{ item }}"
|
|
|
|
with_file: /path/to/public_key.id_rsa.pub
|
|
|
|
|
|
|
|
# Removes ec2 key pair by name
|
|
|
|
- name: remove example key
|
|
|
|
local_action:
|
|
|
|
module: ec2_key
|
|
|
|
name: example
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2
|
|
|
|
except ImportError:
|
|
|
|
print "failed=True msg='boto required for this module'"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def main():
|
2014-02-05 11:11:06 +00:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2013-12-12 04:23:58 +00:00
|
|
|
name=dict(required=True),
|
|
|
|
key_material=dict(required=False),
|
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
2014-02-05 11:11:06 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2013-12-12 04:23:58 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params.get('state')
|
|
|
|
key_material = module.params.get('key_material')
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
2013-12-13 02:10:44 +00:00
|
|
|
ec2 = ec2_connect(module)
|
2013-12-12 04:23:58 +00: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'''
|
|
|
|
try:
|
|
|
|
key.delete()
|
|
|
|
except Exception, e:
|
2013-12-12 04:30:22 +00:00
|
|
|
module.fail_json(msg="Unable to delete key pair '%s' - %s" % (key, e))
|
2013-12-12 04:23:58 +00:00
|
|
|
else:
|
|
|
|
key = None
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
'''no match found, no changes required'''
|
|
|
|
|
2013-12-12 04:30:22 +00:00
|
|
|
# Ensure requested key is present
|
2013-12-12 04:23:58 +00:00
|
|
|
elif state == 'present':
|
|
|
|
if key:
|
|
|
|
'''existing key found'''
|
|
|
|
# Should check if the fingerprint is the same - but lack of info
|
|
|
|
# and different fingerprint provided (pub or private) depending if
|
|
|
|
# the key has been created of imported.
|
|
|
|
pass
|
|
|
|
|
2013-12-12 04:30:22 +00:00
|
|
|
# if the key doesn't exist, create it now
|
2013-12-12 04:23:58 +00: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)
|
|
|
|
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()
|