Compare commits
No commits in common. '786bbc4b4be89cfe3a6b6b3ffa3dc10192bbfaaf' and '5ea36d86268737b27b96d474809ecbada48d6b59' have entirely different histories.
786bbc4b4b
...
5ea36d8626
@ -1,92 +1,60 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
# This script will create environment variables for all of the output IPs. An
|
# This script will create environment variables for all of the output IPs. It
|
||||||
# anisble inventory file is created as well.
|
# will also create a `ANSIBLE_INV` variable that will be a comma separated
|
||||||
|
# string of all the IPs. A anisble inventory file called "inventory is created
|
||||||
|
# as well.
|
||||||
#
|
#
|
||||||
# Use eval $(./get-vm-ips.sh) to set env vars for ips.
|
# Use eval $(./get-vm-ips.sh) to set env vars for ips.
|
||||||
|
|
||||||
terraform refresh > /dev/null
|
terraform refresh > /dev/null
|
||||||
|
|
||||||
# The file to write the inventory to. This file will be completely overridden.
|
# All terraform outputs in json format.
|
||||||
INVENTORY_FILE="inventory"
|
OUTPUTS_JSON="$(
|
||||||
|
terraform show -json | \
|
||||||
# Grab the the vm name prefix. We do this by greping all *.tfvars files making
|
jq '.values.outputs' | \
|
||||||
# sure to cat terraform.tfvars last. Then we just grab the last grep result,
|
sed 's/-/_/g')"
|
||||||
# this way we make sure any value in terraform.tfvars will take priority.
|
# Just the IP address outputs in json format. Also all '-' characters are
|
||||||
VM_NAME_PREFIX_VAR="vm-name-prefix"
|
# replaced by '_' becuase '-' causes jq some problems.
|
||||||
VM_NAME_PREFIXES="$( \
|
IPS_JSON="$(
|
||||||
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX_VAR" {} \; && \
|
echo $OUTPUTS_JSON | \
|
||||||
grep "$VM_NAME_PREFIX_VAR" terraform.tfvars)"
|
jq 'to_entries | .[] | select(.key | contains("ips"))')"
|
||||||
VM_NAME_PREFIX="$(
|
# An array of all node "types"
|
||||||
echo "$VM_NAME_PREFIXES" | \
|
NODE_TYPE_ARRAY="$(
|
||||||
tail -n 1 | \
|
echo $IPS_JSON | \
|
||||||
sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
jq '.value.value | to_entries | .[] | .key' | \
|
||||||
|
|
||||||
# This command stores the output data in the format below.
|
|
||||||
# [
|
|
||||||
# {
|
|
||||||
# "group": "master",
|
|
||||||
# "vms": [
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-master-0",
|
|
||||||
# "ip": "52.14.114.48"
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "group": "worker",
|
|
||||||
# "vms": [
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-worker-0",
|
|
||||||
# "ip": "3.145.121.159"
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-worker-1",
|
|
||||||
# "ip": "18.217.112.176"
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
DATA="$(terraform show -json | \
|
|
||||||
jq '.values.outputs.groups_hostnames_ips.value | to_entries |
|
|
||||||
map({group: .key, vms:.value | to_entries |
|
|
||||||
map({hostname:.key,ip:.value})})')"
|
|
||||||
|
|
||||||
# Pull out the groups from $DATA. The format is a single string with the groups
|
|
||||||
# separated by spaces, ie. "group1 group2 group3".
|
|
||||||
ANS_GROUPS="$(
|
|
||||||
echo $DATA | \
|
|
||||||
jq '.[] | .group' | \
|
|
||||||
sed 's/"//g' | \
|
sed 's/"//g' | \
|
||||||
tr '\n' ' '
|
sed -z 's/\n/ /g;s/ $/\n/g')"
|
||||||
)"
|
|
||||||
|
# Loop over all the node types and create an export line for each IP.
|
||||||
# Clear the inventory file.
|
VM_IP_EXPORTS="$(
|
||||||
cat /dev/null > $INVENTORY_FILE
|
for TYPE in $NODE_TYPE_ARRAY; do
|
||||||
|
|
||||||
# For each group, write the VM info to $INVENTORY_FILE and also print a variable
|
# Convert type, converts "master-ips" to "MASTER"
|
||||||
# expression to stdout.
|
TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)"
|
||||||
for GROUP in $ANS_GROUPS; do
|
echo "$IPS_JSON" | \
|
||||||
|
jq '.value.value.'"$TYPE"'[]' | \
|
||||||
# Write the inventory file to $INVENTORY_FILE.
|
# Add line numbers starting with 0.
|
||||||
echo "[$GROUP]" >> $INVENTORY_FILE
|
nl -v 0 | \
|
||||||
echo $DATA | \
|
# Print an export string with a type placeholder "__TYPE__".
|
||||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
awk '{print "export __TYPE___" $1 "=" $2}' | \
|
||||||
"\(.hostname) ansible_host=\(.ip)"' | \
|
sed s/__TYPE__/$TYPE_UPPER/g
|
||||||
sed 's/"//g' \
|
done)"
|
||||||
>> $INVENTORY_FILE
|
|
||||||
|
ANSIBLE_INV="$(
|
||||||
# For this group, collect expressions into VARS. The format is:
|
echo "$VM_IP_EXPORTS" | \
|
||||||
# HOSTNAME1=0.0.0.0
|
sed 's/"//g' | \
|
||||||
# HOSTNAME2=0.0.0.0
|
sed 's/^.*=//g' | \
|
||||||
VARS="$(
|
sed -z 's/\n/,/g;s/,$/\n/g')"
|
||||||
echo $DATA | \
|
|
||||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
# Create an inventory file for ansible.
|
||||||
"\(.hostname)=\(.ip)"' | \
|
echo "[k8s_nodes]" > inventory
|
||||||
sed 's/"//g' | \
|
echo $VM_IP_EXPORTS | \
|
||||||
sed "s/$VM_NAME_PREFIX-//g" | \
|
sed 's/"//g' | \
|
||||||
sed 's/-/_/g'
|
sed 's/export //g' | \
|
||||||
)"
|
sed 's/ /\n/g' | \
|
||||||
# Print the contents of $VARS converted to uppercase.
|
sed 's/^\(.*\)\(=.*\)$/\1 ansible_host\2/g' \
|
||||||
echo "${VARS^^}"
|
>> inventory
|
||||||
done
|
|
||||||
|
echo $VM_IP_EXPORTS | sed 's/" /"\n/g'
|
||||||
|
echo export ANSIBLE_INV=$ANSIBLE_INV
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
data "template_file" "user-datas" {
|
data "template_file" "user-datas" {
|
||||||
template = file("${var.cloud-init-template}")
|
template = file("${var.cloud-init-template}")
|
||||||
vars = {
|
vars = {
|
||||||
admin-passwd = "${var.root-admin-passwd}"
|
admin-passwd = "${var.root-admin-passwd}"
|
||||||
admin-pub-key = "${var.root-admin-pub-key}"
|
admin-pub-key = "${var.root-admin-pub-key}"
|
||||||
hostname = "${var.hostname-prefix}-${count.index}"
|
hostname = "${var.hostname-prefix}-${count.index}"
|
||||||
install-qemu-agent = var.install-qemu-agent
|
|
||||||
}
|
}
|
||||||
count = var.num
|
count = var.num
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue