diff --git a/ci/roles/keypair/tasks/main.yml b/ci/roles/keypair/tasks/main.yml index c58fdbba38802a7394884e6c9463ebdce6177f72..b315415373bb5d468532de4192c2c61ef2392df0 100644 --- a/ci/roles/keypair/tasks/main.yml +++ b/ci/roles/keypair/tasks/main.yml @@ -6,6 +6,12 @@ state: present register: keypair +- name: Assert fields + assert: + that: + - item in keypair.keypair + loop: "{{ expected_fields }}" + - name: Get list of all keypairs openstack.cloud.keypair_info: cloud: "{{ cloud }}" @@ -32,7 +38,7 @@ - name: Ensure public key is returned assert: that: - - keypair.key.public_key is defined and keypair.key.public_key + - keypair.keypair.public_key is defined and keypair.keypair.public_key - name: Create another keypair openstack.cloud.keypair: diff --git a/plugins/modules/keypair.py b/plugins/modules/keypair.py index 319686d22bbfcc281cdf51083198a75514aeb5f6..9e931101925fb156d12ec1441864d1aa758a0587 100644 --- a/plugins/modules/keypair.py +++ b/plugins/modules/keypair.py @@ -61,23 +61,49 @@ EXAMPLES = ''' ''' RETURN = ''' -id: - description: Unique UUID. - returned: success - type: str -name: - description: Name given to the keypair. - returned: success - type: str -public_key: - description: The public key value for the keypair. - returned: success - type: str -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: str +keypair: + description: Dictionary describing the keypair. + returned: On success when I(state) is 'present' + type: dict + contains: + created_at: + description: Date the keypair was created + returned: success + type: str + fingerprint: + description: The short fingerprint associated with the public_key + for this keypair. + returned: success + type: str + id: + description: Unique UUID. + returned: success + type: str + is_deleted: + description: Whether the keypair is deleted or not + returned: success + type: bool + name: + description: Name given to the keypair. + returned: success + type: str + 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: str + public_key: + description: The public key value for the keypair. + returned: success + type: str + type: + description: The type of keypair + returned: success + type: str + user_id: + description: The user id for a keypair + returned: success + type: str ''' from ansible_collections.openstack.cloud.plugins.module_utils.openstack import ( @@ -115,11 +141,12 @@ class KeyPairModule(OpenStackModule): with open(self.params['public_key_file']) as public_key_fh: public_key = public_key_fh.read().rstrip() - keypair = self.conn.get_keypair(name) + keypair = self.conn.compute.find_keypair(name) if self.ansible.check_mode: self.exit_json(changed=self._system_state_change(keypair)) + changed = False if state in ('present', 'replace'): if keypair and keypair['name'] == name: if public_key and (public_key != keypair['public_key']): @@ -129,20 +156,19 @@ class KeyPairModule(OpenStackModule): " as offered. Delete key first." % name ) else: - self.conn.delete_keypair(name) + self.conn.compute.delete_keypair(keypair) keypair = self.conn.create_keypair(name, public_key) changed = True - else: - changed = False else: keypair = self.conn.create_keypair(name, public_key) changed = True - self.exit_json(changed=changed, key=keypair, id=keypair['id']) + self.exit_json( + changed=changed, keypair=keypair.to_dict(computed=False)) elif state == 'absent': if keypair: - self.conn.delete_keypair(name) + self.conn.compute.delete_keypair(keypair) self.exit_json(changed=True) self.exit_json(changed=False)