Skip to content

Instantly share code, notes, and snippets.

@yanndegat
Created April 28, 2017 11:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanndegat/56454b80c9813af76714d068412cf3b2 to your computer and use it in GitHub Desktop.
Save yanndegat/56454b80c9813af76714d068412cf3b2 to your computer and use it in GitHub Desktop.
ec2 instances to ansible inventory using jq
#!/bin/bash
STACK=$1
aws ec2 describe-instances | jq -r \
'[
.Reservations[].Instances[]
| . as $node
| {
ip: ($node.NetworkInterfaces[0].PrivateIpAddress),
name: ($node.Tags[] | select(.Key == "Name").Value),
group: ($node.Tags[] | select(.Key == "Group").Value),
stack: ($node.Tags[] | select(.Key == "Stack").Value)
}
| select( .stack == "'"$STACK"'")
]
| group_by( .group)
| map ({group:.[0].group, nodes:([.[]|"\(.name) ansible_host=\(.ip)"]|join("\n"))})
| .[] | "[\(.group)]","\(.nodes)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment