Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public key return to openssh_keypair #53214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/ansible/modules/crypto/openssh_keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
returned: changed or success
type: str
sample: 4096 SHA256:r4YCZxihVjedH2OlfjVGI6Y5xAYtdCwk8VxKyzVyYfM example@example.com (RSA)
public_key:
description: The public key of the generated SSH private key
returned: changed or success
type: str
sample: ssh-rsa AAAAB3Nza(...omitted...)veL4E3Xcw== test_key
'''

import os
Expand All @@ -134,6 +139,7 @@ def __init__(self, module):
self.check_mode = module.check_mode
self.privatekey = None
self.fingerprint = {}
self.public_key = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this and the one above it start out as an empty dict, but you are matching the surrounding code...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the idea.
Thanks.


if self.type in ('rsa', 'rsa1'):
self.size = 4096 if self.size is None else self.size
Expand Down Expand Up @@ -178,6 +184,8 @@ def generate(self, module):
module.run_command(args)
proc = module.run_command([module.get_bin_path('ssh-keygen', True), '-lf', self.path])
self.fingerprint = proc[1].split()
pubkey = module.run_command([module.get_bin_path('ssh-keygen', True), '-yf', self.path])
self.public_key = pubkey[1].strip('\n')
except Exception as e:
self.remove()
module.fail_json(msg="%s" % to_native(e))
Expand All @@ -195,6 +203,8 @@ def _check_state():
if _check_state():
proc = module.run_command([module.get_bin_path('ssh-keygen', True), '-lf', self.path])
fingerprint = proc[1].split()
pubkey = module.run_command([module.get_bin_path('ssh-keygen', True), '-yf', self.path])
pubkey = pubkey[1].strip('\n')
keysize = int(fingerprint[0])
keytype = fingerprint[-1][1:-1].lower()
else:
Expand All @@ -211,6 +221,7 @@ def _check_size():
return self.size == keysize

self.fingerprint = fingerprint
self.public_key = pubkey

if not perms_required:
return _check_state() and _check_type() and _check_size()
Expand All @@ -228,6 +239,7 @@ def dump(self):
'type': self.type,
'filename': self.path,
'fingerprint': self.fingerprint,
'public_key': self.public_key,
}

return result
Expand Down
5 changes: 5 additions & 0 deletions test/integration/targets/openssh_keypair/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
state: absent
path: '{{ output_dir }}/privatekey4'

- name: Generate privatekey5 - standard
openssh_keypair:
path: '{{ output_dir }}/privatekey5'
register: publickey_gen

- import_tasks: ../tests/validate.yml
6 changes: 6 additions & 0 deletions test/integration/targets/openssh_keypair/tests/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@
assert:
that:
- privatekey4.stat.exists == False


- name: Validate privatekey5 (assert - Public key module output equal to the public key on host)
assert:
that:
- "publickey_gen.public_key == lookup('file', output_dir ~ '/privatekey5.pub').strip('\n')"