Skip to content

Instantly share code, notes, and snippets.

@naftulikay
Created March 26, 2016 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naftulikay/c5bd72bb709eac76aab1 to your computer and use it in GitHub Desktop.
Save naftulikay/c5bd72bb709eac76aab1 to your computer and use it in GitHub Desktop.
Generate an Ansible Inventory for EC2 instances with a given name.
#!/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