Update os_keypair for latest shade

Uses the latest version of shade for cleaner code. Also, always
return the key dict whether we create the key, or it already exists.
The example using public_key_file is corrected to use a full path
since ~ is not converted for us.
reviewable/pr18780/r1
David Shrewsbury 10 years ago
parent 02d0a73906
commit bed420cd53

@ -41,12 +41,14 @@ options:
default: None default: None
public_key: public_key:
description: description:
- The public key that would be uploaded to nova and injected to vm's upon creation - The public key that would be uploaded to nova and injected into VMs
upon creation.
required: false required: false
default: None default: None
public_key_file: public_key_file:
description: description:
- Path to local file containing ssh public key. Mutually exclusive with public_key - Path to local file containing ssh public key. Mutually exclusive
with public_key.
required: false required: false
default: None default: None
state: state:
@ -63,7 +65,7 @@ EXAMPLES = '''
cloud: mordred cloud: mordred
state: present state: present
name: ansible_key name: ansible_key
public_key_file: ~/.ssh/id_rsa.pub public_key_file: /home/me/.ssh/id_rsa.pub
# Creates a new key pair and the private key returned after the run. # Creates a new key pair and the private key returned after the run.
- os_keypair: - os_keypair:
@ -73,16 +75,33 @@ EXAMPLES = '''
''' '''
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
def main(): def main():
argument_spec = openstack_full_argument_spec( argument_spec = openstack_full_argument_spec(
name = dict(required=True), name = dict(required=True),
public_key = dict(default=None), public_key = dict(default=None),
public_key_file = dict(default=None), public_key_file = dict(default=None),
state = dict(default='present', choices=['absent', 'present']), state = dict(default='present',
choices=['absent', 'present']),
) )
module_kwargs = openstack_module_kwargs( module_kwargs = openstack_module_kwargs(
mutually_exclusive=[['public_key', 'public_key_file']]) mutually_exclusive=[['public_key', 'public_key_file']])
module = AnsibleModule(argument_spec, **module_kwargs)
module = AnsibleModule(argument_spec,
supports_check_mode=True,
**module_kwargs)
if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')
state = module.params['state'] state = module.params['state']
name = module.params['name'] name = module.params['name']
@ -90,44 +109,33 @@ def main():
if module.params['public_key_file']: if module.params['public_key_file']:
public_key = open(module.params['public_key_file']).read() public_key = open(module.params['public_key_file']).read()
public_key = public_key.rstrip()
if not HAS_SHADE:
module.fail_json(msg='shade is required for this module')
try: try:
cloud = shade.openstack_cloud(**module.params) cloud = shade.openstack_cloud(**module.params)
keypair = cloud.get_keypair(name)
if module.check_mode:
module.exit_json(changed=_system_state_change(module, keypair))
if state == 'present': if state == 'present':
for key in cloud.list_keypairs(): if keypair and keypair['name'] == name:
if key.name == name: if public_key and (public_key != keypair['public_key']):
if public_key and (public_key != key.public_key):
module.fail_json( module.fail_json(
msg="Key name %s present but key hash not the same" msg="Key name %s present but key hash not the same"
" as offered. Delete key first." % key.name " as offered. Delete key first." % name
) )
else: else:
module.exit_json(changed=False, result="Key present") module.exit_json(changed=False, key=keypair)
try:
key = cloud.create_keypair(name, public_key) new_key = cloud.create_keypair(name, public_key)
except Exception, e: module.exit_json(changed=True, key=new_key)
module.exit_json(
msg="Error in creating the keypair: %s" % e.message
)
if not public_key:
module.exit_json(changed=True, key=key.private_key)
module.exit_json(changed=True, key=None)
elif state == 'absent': elif state == 'absent':
for key in cloud.list_keypairs(): if keypair:
if key.name == name:
try:
cloud.delete_keypair(name) cloud.delete_keypair(name)
except Exception, e: module.exit_json(changed=True)
module.fail_json( module.exit_json(changed=False)
msg="Keypair deletion has failed: %s" % e.message
)
module.exit_json(changed=True, result="deleted")
module.exit_json(changed=False, result="not present")
except shade.OpenStackCloudException as e: except shade.OpenStackCloudException as e:
module.fail_json(msg=e.message) module.fail_json(msg=e.message)

Loading…
Cancel
Save