Created
July 13, 2020 13:59
-
-
Save 5sfayas/4558f4f6db78e1136b1e47c5642a7b8c to your computer and use it in GitHub Desktop.
Find IP address of aws Instances using tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!#/bin/python | |
import boto3 | |
import jason | |
def find-ip(client,tagkey,tagvalue): | |
response = client.describe_instances( | |
Filters=[{ | |
'Name': 'instance-state-name', | |
'Values': [ | |
'running', | |
] | |
}, | |
{ | |
'Name': 'tagkey', | |
'Values': [ | |
'tagvalue', | |
] | |
}],DryRun=TRUE | |
) | |
instance-ip-list = [] | |
for res in (respones["Reservations"]): | |
for instance in reservation["Instances"]: | |
instance-ip-list.append(instance["PublicIpAddress"]) | |
return instance-ip-list | |
def main(): | |
client = boto3.client('ec2') | |
db-group-ip=find-ip(client,"tag:Env","db") | |
app-group-ip=find-ip(client,"tag:Env","app") | |
jason-group={'db':db-group-ip,'app':app-group-ip} | |
#it create jason data | |
print(jason.dumps(jason-group)) | |
return None | |
if __name__=="__main__": | |
main() | |
#now we can have jason output and | |
#we can use those ip agans ansible to test my instances (ansible -i find-ip-by-tag.py db) | |
"""ec2=boto3.resource("ec2") | |
for instance in ec2.instances.filter(Filters=[{ | |
'Name': 'instance-state-name', | |
'Values': [ | |
'running', | |
] | |
}, | |
{ | |
'Name': 'tagkey', | |
'Values': [ | |
'tagvalue', | |
] | |
}],DryRun=TRUE): | |
print(instance.public_ip_address) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment