Debian Codegraph @

ansible_collections/community/hrobot/plugins/modules/ssh_key_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019 Felix Fontein <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r"""
13 module: ssh_key_info
14 short_description: Query information on SSH keys
15 version_added: 1.2.0
16 author:
17 - Felix Fontein (@felixfontein)
18 description:
19 - List information on all your SSH keys stored in Hetzner's Robot.
20 seealso:
21 - module: community.hrobot.ssh_key
22 description: Add, remove or update SSH key.
23 extends_documentation_fragment:
24 - community.hrobot.robot
25 - community.hrobot.attributes
26 - community.hrobot.attributes.actiongroup_robot
27 - community.hrobot.attributes.idempotent_not_modify_state
28 - community.hrobot.attributes.info_module
29 attributes:
30 action_group:
31 version_added: 1.6.0
32 """
33
34 EXAMPLES = r"""
35 ---
36 - name: List all SSH keys
37 community.hrobot.ssh_key_info:
38 hetzner_user: foo
39 hetzner_password: bar
40 register: ssh_keys
41
42 - name: Show how many keys were found
43 ansible.builtin.debug:
44 msg: "Found {{ ssh_keys.ssh_keys | length }} keys"
45 """
46
47 RETURN = r"""
48 ssh_keys:
49 description:
50 - The list of all SSH keys stored in Hetzner's Robot for your user.
51 returned: success
52 type: list
53 elements: dict
54 contains:
55 name:
56 description:
57 - The key's name shown in the UI.
58 type: str
59 sample: key1
60 fingerprint:
61 description:
62 - The key's MD5 fingerprint.
63 type: str
64 sample: 56:29:99:a4:5d:ed:ac:95:c1:f5:88:82:90:5d:dd:10
65 type:
66 description:
67 - The key's algorithm type.
68 type: str
69 sample: ECDSA
70 size:
71 description:
72 - The key's size in bits.
73 type: int
74 sample: 521
75 data:
76 description:
77 - The key data in OpenSSH's format.
78 type: str
79 sample: ecdsa-sha2-nistp521 AAAAE2VjZHNh ...
80 """
81
82 from ansible.module_utils.basic import AnsibleModule
83
84 from ansible_collections.community.hrobot.plugins.module_utils.robot import (
85 BASE_URL,
86 ROBOT_DEFAULT_ARGUMENT_SPEC,
87 fetch_url_json,
88 )
89
90
91 def main():
92 argument_spec = dict()
93 argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
94 module = AnsibleModule(
95 argument_spec=argument_spec,
96 supports_check_mode=True,
97 )
98
99 url = "{0}/key".format(BASE_URL)
100 result, error = fetch_url_json(module, url, accept_errors=['NOT_FOUND'])
101 if error == 'NOT_FOUND':
102 result = []
103 elif error is not None:
104 raise AssertionError('Unexpected error {0}'.format(error)) # pragma: no cover
105
106 keys = []
107 for key in result:
108 keys.append(key['key'])
109
110 module.exit_json(changed=False, ssh_keys=keys)
111
112
113 if __name__ == '__main__': # pragma: no cover
114 main() # pragma: no cover