Debian Codegraph @

ansible_collections/community/aws/plugins/modules/ssm_inventory_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Contributors to the Ansible project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = """
8 module: ssm_inventory_info
9 version_added: 6.0.0
10 short_description: Get SSM inventory information for EC2 instance
11
12 description:
13 - Gather SSM inventory for EC2 instance configured with SSM.
14
15 author: 'Aubin Bikouo (@abikouo)'
16
17 options:
18 instance_id:
19 description:
20 - EC2 instance id.
21 required: true
22 type: str
23
24 extends_documentation_fragment:
25 - amazon.aws.common.modules
26 - amazon.aws.region.modules
27 - amazon.aws.boto3
28 """
29
30 EXAMPLES = """
31 - name: Retrieve SSM inventory info for instance id 'i-012345678902'
32 community.aws.ssm_inventory_info:
33 instance_id: 'i-012345678902'
34 """
35
36
37 RETURN = """
38 ssm_inventory:
39 returned: on success
40 description: >
41 SSM inventory information.
42 type: dict
43 sample: {
44 'agent_type': 'amazon-ssm-agent',
45 'agent_version': '3.2.582.0',
46 'computer_name': 'ip-172-31-44-166.ec2.internal',
47 'instance_id': 'i-039eb9b1f55934ab6',
48 'instance_status': 'Active',
49 'ip_address': '172.31.44.166',
50 'platform_name': 'Fedora Linux',
51 'platform_type': 'Linux',
52 'platform_version': '37',
53 'resource_type': 'EC2Instance'
54 }
55 """
56
57
58 try:
59 import botocore
60 except ImportError:
61 pass # Handled by AnsibleAWSModule
62
63 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
64
65 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
66
67
68 class SsmInventoryInfoFailure(Exception):
69 def __init__(self, exc, msg):
70 self.exc = exc
71 self.msg = msg
72 super().__init__(self)
73
74
75 def get_ssm_inventory(connection, filters):
76 try:
77 return connection.get_inventory(Filters=filters)
78 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
79 raise SsmInventoryInfoFailure(exc=e, msg="get_ssm_inventory() failed.")
80
81
82 def execute_module(module, connection):
83 instance_id = module.params.get("instance_id")
84 try:
85 filters = [{"Key": "AWS:InstanceInformation.InstanceId", "Values": [instance_id]}]
86
87 response = get_ssm_inventory(connection, filters)
88 entities = response.get("Entities", [])
89 ssm_inventory = {}
90 if entities:
91 content = entities[0].get("Data", {}).get("AWS:InstanceInformation", {}).get("Content", [])
92 if content:
93 ssm_inventory = camel_dict_to_snake_dict(content[0])
94 module.exit_json(changed=False, ssm_inventory=ssm_inventory)
95 except SsmInventoryInfoFailure as e:
96 module.fail_json_aws(exception=e.exc, msg=e.msg)
97
98
99 def main():
100 argument_spec = dict(
101 instance_id=dict(required=True, type="str"),
102 )
103
104 module = AnsibleAWSModule(
105 argument_spec=argument_spec,
106 supports_check_mode=True,
107 )
108
109 connection = module.client("ssm")
110 execute_module(module, connection)
111
112
113 if __name__ == "__main__":
114 main()