-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_inventory.py
41 lines (35 loc) · 1.04 KB
/
dynamic_inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
import boto3
# Initialize the AWS client
ec2 = boto3.client('ec2')
# Fetch EC2 instances with specific tags
response = ec2.describe_instances(
Filters=[
{'Name': 'tag:Environment', 'Values': ['Production']},
# Add more filters as needed
]
)
# Initialize an empty inventory
inventory = {
'all': {
'children': {
'linux': {
'hosts': []
},
'windows': {
'hosts': []
}
}
}
}
# Loop through instances and categorize them by OS
for reservation in response['Reservations']:
for instance in reservation['Instances']:
os = instance.get('Tags', {}).get('OS', 'Unknown')
if os.lower() == 'linux':
inventory['all']['children']['linux']['hosts'].append(instance['PrivateIpAddress'])
elif os.lower() == 'windows':
inventory['all']['children']['windows']['hosts'].append(instance['PrivateIpAddress'])
# Print the inventory in JSON format
import json
print(json.dumps(inventory))