2015-03-31 20:37:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
|
|
|
|
# Copyright (c) 2013, John Dewey <john@dewey.ws>
|
2017-10-09 16:56:26 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_keypair
|
|
|
|
short_description: Add/Delete a keypair from OpenStack
|
2017-03-09 16:20:25 +00:00
|
|
|
author: "Benno Joy (@bennojoy)"
|
2015-03-31 20:37:07 +00:00
|
|
|
extends_documentation_fragment: openstack
|
|
|
|
version_added: "2.0"
|
|
|
|
description:
|
2015-07-01 12:00:08 +00:00
|
|
|
- Add or Remove key pair from OpenStack
|
2015-03-31 20:37:07 +00:00
|
|
|
options:
|
2015-07-01 12:00:08 +00:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name that has to be given to the key pair
|
|
|
|
required: true
|
|
|
|
public_key:
|
|
|
|
description:
|
|
|
|
- The public key that would be uploaded to nova and injected into VMs
|
|
|
|
upon creation.
|
|
|
|
public_key_file:
|
|
|
|
description:
|
|
|
|
- Path to local file containing ssh public key. Mutually exclusive
|
|
|
|
with public_key.
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
2017-02-17 20:49:03 +00:00
|
|
|
availability_zone:
|
|
|
|
description:
|
2017-06-12 06:55:19 +00:00
|
|
|
- Ignored. Present for backwards compatibility
|
2015-03-31 20:37:07 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Creates a key pair with the running users public key
|
|
|
|
- os_keypair:
|
|
|
|
cloud: mordred
|
|
|
|
state: present
|
|
|
|
name: ansible_key
|
2015-06-25 16:19:20 +00:00
|
|
|
public_key_file: /home/me/.ssh/id_rsa.pub
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
# Creates a new key pair and the private key returned after the run.
|
|
|
|
- os_keypair:
|
|
|
|
cloud: rax-dfw
|
|
|
|
state: present
|
|
|
|
name: ansible_key
|
|
|
|
'''
|
|
|
|
|
2015-06-29 19:55:15 +00:00
|
|
|
RETURN = '''
|
|
|
|
id:
|
|
|
|
description: Unique UUID.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
name:
|
|
|
|
description: Name given to the keypair.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
public_key:
|
|
|
|
description: The public key value for the keypair.
|
|
|
|
returned: success
|
|
|
|
type: string
|
|
|
|
private_key:
|
|
|
|
description: The private key value for the keypair.
|
|
|
|
returned: Only when a keypair is generated for the user (e.g., when creating one
|
|
|
|
and a public key is not specified).
|
|
|
|
type: string
|
|
|
|
'''
|
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-02-15 14:20:49 +00:00
|
|
|
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
|
2017-10-09 16:56:26 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2015-06-25 16:19:20 +00:00
|
|
|
def _system_state_change(module, keypair):
|
|
|
|
state = module.params['state']
|
|
|
|
if state == 'present' and not keypair:
|
|
|
|
return True
|
|
|
|
if state == 'absent' and keypair:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
2017-12-07 16:27:06 +00:00
|
|
|
name=dict(required=True),
|
|
|
|
public_key=dict(default=None),
|
|
|
|
public_key_file=dict(default=None),
|
|
|
|
state=dict(default='present',
|
|
|
|
choices=['absent', 'present']),
|
2015-03-31 20:37:07 +00:00
|
|
|
)
|
2015-06-25 16:19:20 +00:00
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
module_kwargs = openstack_module_kwargs(
|
|
|
|
mutually_exclusive=[['public_key', 'public_key_file']])
|
2015-06-25 16:19:20 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
**module_kwargs)
|
|
|
|
|
2015-06-18 11:59:32 +00:00
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
public_key = module.params['public_key']
|
|
|
|
|
2015-03-31 20:37:07 +00:00
|
|
|
if module.params['public_key_file']:
|
|
|
|
public_key = open(module.params['public_key_file']).read()
|
2015-06-25 16:19:20 +00:00
|
|
|
public_key = public_key.rstrip()
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
sdk, cloud = openstack_cloud_from_module(module)
|
2015-03-31 20:37:07 +00:00
|
|
|
try:
|
2015-06-25 16:19:20 +00:00
|
|
|
keypair = cloud.get_keypair(name)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=_system_state_change(module, keypair))
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
if state == 'present':
|
2015-06-25 16:19:20 +00:00
|
|
|
if keypair and keypair['name'] == name:
|
|
|
|
if public_key and (public_key != keypair['public_key']):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Key name %s present but key hash not the same"
|
|
|
|
" as offered. Delete key first." % name
|
|
|
|
)
|
|
|
|
else:
|
2015-10-22 17:59:45 +00:00
|
|
|
changed = False
|
|
|
|
else:
|
|
|
|
keypair = cloud.create_keypair(name, public_key)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed,
|
|
|
|
key=keypair,
|
|
|
|
id=keypair['id'])
|
2015-03-31 20:37:07 +00:00
|
|
|
|
|
|
|
elif state == 'absent':
|
2015-06-25 16:19:20 +00:00
|
|
|
if keypair:
|
|
|
|
cloud.delete_keypair(name)
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
module.exit_json(changed=False)
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2018-05-26 01:40:39 +00:00
|
|
|
except sdk.exceptions.OpenStackCloudException as e:
|
2016-01-13 16:00:16 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2015-03-31 20:37:07 +00:00
|
|
|
|
2017-10-09 16:56:26 +00:00
|
|
|
|
2015-06-30 20:18:56 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|