Created
April 28, 2017 11:26
-
-
Save yanndegat/56454b80c9813af76714d068412cf3b2 to your computer and use it in GitHub Desktop.
ec2 instances to ansible inventory using jq
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/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