Created
March 26, 2016 00:18
-
-
Save naftulikay/c5bd72bb709eac76aab1 to your computer and use it in GitHub Desktop.
Generate an Ansible Inventory for EC2 instances with a given name.
This file contains hidden or 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/bash | |
if [ -z "$1" ]; then | |
echo "$0 takes one argument; the value of the name tag of EC2 instances to search for." >&2 | |
exit 1 | |
fi | |
name="$1" | |
# create a header for the inventory with a group name of "all" | |
echo "[all]" > inventory.txt | |
# find all instances whose "Name" tag matches $name, output their private IP addresses, translate tabs into newlines, | |
# and finally collect a unique list of the output and append it to the inventory, simple as that. | |
aws ec2 describe-instances --output text --filter "Name=tag:Name,Values=$name" \ | |
--query 'Reservations[].Instances[].PrivateIpAddress' | tr '\t' '\n' | uniq >> inventory.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment