Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/ec2_key_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: ec2_key_info
10 version_added: 6.4.0
11 short_description: Gather information about EC2 key pairs in AWS
12 description:
13 - Gather information about EC2 key pairs in AWS.
14 author:
15 - Aubin Bikouo (@abikouo)
16 options:
17 filters:
18 description:
19 - A dict of filters to apply. Each dict item consists of a filter key and a filter value. See
20 U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeKeyPairs.html) for possible filters. Filter
21 names and values are case sensitive.
22 required: false
23 default: {}
24 type: dict
25 names:
26 description:
27 - The key pair names.
28 required: false
29 type: list
30 elements: str
31 default: []
32 ids:
33 description:
34 - The IDs of the key pairs.
35 required: false
36 type: list
37 elements: str
38 default: []
39 include_public_key:
40 description:
41 - Whether or not to include the public key material in the response.
42 type: bool
43 default: false
44 extends_documentation_fragment:
45 - amazon.aws.common.modules
46 - amazon.aws.region.modules
47 - amazon.aws.boto3
48 """
49
50 EXAMPLES = r"""
51 # Note: These examples do not set authentication details, see the AWS Guide for details.
52
53 - name: Gather information about all key pairs
54 amazon.aws.ec2_key_info:
55
56 - name: Gather information about a specific key pair
57 amazon.aws.ec2_key_info:
58 names:
59 - my-sample-key
60
61 - name: Retrieve EC2 key pair by fingerprint
62 amazon.aws.ec2_key_info:
63 filters:
64 fingerprint: "1bSd8jVye3In5oF4zZI4o8BcXfdbYN+daCt9O1fh3Qk="
65 """
66
67 RETURN = r"""
68 keypairs:
69 description: A list of ec2 key pairs.
70 returned: always
71 type: complex
72 contains:
73 key_pair_id:
74 description: The ID of the key pair.
75 returned: always
76 type: str
77 sample: key-01238eb03f07d7268
78 key_fingerprint:
79 description: Fingerprint of the key.
80 returned: always
81 type: str
82 sample: '05:97:1a:2a:df:f6:06:a9:98:4b:ca:05:71:a1:81:e8:ff:6d:d2:a3'
83 key_name:
84 description: The name of the key pair.
85 returned: always
86 type: str
87 sample: my-sample-keypair
88 key_type:
89 description: The type of key pair.
90 returned: always
91 type: str
92 sample: rsa
93 public_key:
94 description: The public key material.
95 returned: always
96 type: str
97 create_time:
98 description: The time the key pair was created.
99 returned: always
100 type: str
101 sample: "2023-08-16T10:13:33.025000+00:00"
102 tags:
103 description: A dictionary representing the tags attached to the key pair.
104 returned: always
105 type: dict
106 sample: '{"my_key": "my value"}'
107 """
108
109
110 try:
111 import botocore
112 except ImportError:
113 pass # caught by AnsibleAWSModule
114
115 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
116
117 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
118 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_key_pairs
119 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
120 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
121 from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list
122
123
124 def list_ec2_key_pairs(connection, module):
125 ids = module.params.get("ids")
126 names = module.params.get("names")
127 include_public_key = module.params.get("include_public_key")
128 filters = module.params.get("filters")
129 if filters:
130 filters = ansible_dict_to_boto3_filter_list(filters)
131
132 params = {}
133 if filters:
134 params["Filters"] = filters
135 if ids:
136 params["KeyPairIds"] = ids
137 if names:
138 params["KeyNames"] = names
139 if include_public_key:
140 params["IncludePublicKey"] = True
141
142 try:
143 result = describe_key_pairs(connection, **params)
144 except AnsibleEC2Error as e:
145 module.fail_json_aws(e, msg="Failed to list EC2 key pairs")
146
147 # Turn the boto3 result in to ansible_friendly_snaked_names
148 snaked_keys = [camel_dict_to_snake_dict(key) for key in result]
149
150 # Turn the boto3 result in to ansible friendly tag dictionary
151 for key in snaked_keys:
152 key["tags"] = boto3_tag_list_to_ansible_dict(key.get("tags", []), "key", "value")
153
154 module.exit_json(keypairs=snaked_keys)
155
156
157 def main():
158 argument_spec = dict(
159 filters=dict(type="dict", default={}),
160 names=dict(type="list", elements="str", default=[]),
161 ids=dict(type="list", elements="str", default=[]),
162 include_public_key=dict(type="bool", default=False),
163 )
164
165 module = AnsibleAWSModule(
166 argument_spec=argument_spec,
167 supports_check_mode=True,
168 )
169
170 try:
171 connection = module.client("ec2")
172 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
173 module.fail_json_aws(e, msg="Failed to connect to AWS")
174
175 list_ec2_key_pairs(connection, module)
176
177
178 if __name__ == "__main__":
179 main()