Add support for replacing an existing openstack keypair (#62762)

Signed-off-by: Paul Belanger <pabelanger@redhat.com>

Co-authored-by: Monty Taylor <mordred@redhat.com>
This commit is contained in:
Paul Belanger 2020-01-07 18:35:06 -05:00 committed by ansibot
parent 452ade007e
commit 009225dfc6

View file

@ -38,8 +38,10 @@ options:
with public_key. with public_key.
state: state:
description: description:
- Should the resource be present or absent. - Should the resource be present or absent. If state is replace and
choices: [present, absent] the key exists but has different content, delete it and recreate it
with the new content.
choices: [present, absent, replace]
default: present default: present
availability_zone: availability_zone:
description: description:
@ -100,7 +102,7 @@ def main():
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', state=dict(default='present',
choices=['absent', 'present']), choices=['absent', 'present', 'replace']),
) )
module_kwargs = openstack_module_kwargs( module_kwargs = openstack_module_kwargs(
@ -125,13 +127,18 @@ def main():
if module.check_mode: if module.check_mode:
module.exit_json(changed=_system_state_change(module, keypair)) module.exit_json(changed=_system_state_change(module, keypair))
if state == 'present': if state in ('present', 'replace'):
if keypair and keypair['name'] == name: if keypair and keypair['name'] == name:
if public_key and (public_key != keypair['public_key']): if public_key and (public_key != keypair['public_key']):
module.fail_json( if state == 'present':
msg="Key name %s present but key hash not the same" module.fail_json(
" as offered. Delete key first." % name msg="Key name %s present but key hash not the same"
) " as offered. Delete key first." % name
)
else:
cloud.delete_keypair(name)
keypair = cloud.create_keypair(name, public_key)
changed = True
else: else:
changed = False changed = False
else: else: