Added support for bridge network interfaces for libvirt.
parent
10d01ff839
commit
7de2ba1728
@ -0,0 +1,81 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script will grab the IPs for libvirt VMs. This script is only needed when
|
||||
# using a bridge as the network for the VMs. This should only be needed while
|
||||
# https://github.com/dmacvicar/terraform-provider-libvirt/issues/891 is
|
||||
# unresolved.
|
||||
|
||||
# These are the network interfaces that this script will attempt to get the IP
|
||||
# address for.
|
||||
# Ubuntu 20.04 ens3
|
||||
# Centos 7 & 8 eth0
|
||||
NET_INTERFACES="eth0 ens3"
|
||||
|
||||
LIBVIRT_CONNECTION_URL="libvirt-connection-url"
|
||||
VM_NAME_PREFIX="vm-name-prefix"
|
||||
|
||||
INV_GROUPS="$( \
|
||||
cat terraform.tfstate | \
|
||||
jq '.resources[] | select(.type=="libvirt_domain") | .module' | \
|
||||
sed 's/".*\[\\"\(.*\)\\.*$/\1/g' )"
|
||||
|
||||
# Grab the connection URL and the vm name prefix. We do this by greping all
|
||||
# *.tfvars files making sure to cat terraform.tfvars last. Then we just grab the
|
||||
# last grep result, this way we make sure any value in terraform.tfvars will
|
||||
# take priority.
|
||||
CONN_URLS="$( \
|
||||
find . -name "*.tfvars" -exec grep "$LIBVIRT_CONNECTION_URL" {} \; && \
|
||||
grep "$LIBVIRT_CONNECTION_URL" terraform.tfvars)"
|
||||
|
||||
CONN_URL="$(echo "$CONN_URLS" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
||||
|
||||
NAME_PREFIXES="$( \
|
||||
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX" {} \; && \
|
||||
grep "$VM_NAME_PREFIX" terraform.tfvars)"
|
||||
|
||||
NAME_PREFIX="$(echo "$NAME_PREFIXES" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
||||
|
||||
# These can be used for debugging.
|
||||
# echo "Using connection URL: $CONN_URL"
|
||||
# echo "Using prefix: $NAME_PREFIX"
|
||||
|
||||
# Get the names of our VMs from libvirt.
|
||||
VMS="$(virsh -c $CONN_URL list --all | grep $NAME_PREFIX | awk '{print $2}')"
|
||||
|
||||
# Convert the lines of VM names to an array.
|
||||
OLD_IFS=$IFS
|
||||
IFS=$'\n'
|
||||
VMS=($VMS)
|
||||
IFS=$OLD_IFS
|
||||
|
||||
# Loop over our VM array and grab the ipv4 IP address from libvirt. Then add the
|
||||
# result to VM_IP_PAIRS as <vm-name>:<ipv4-address>.
|
||||
VM_IP_PAIRS=""
|
||||
for VM in "${VMS[@]}"; do
|
||||
for INTERFACE in $NET_INTERFACES; do
|
||||
IP="$( \
|
||||
virsh -c $CONN_URL qemu-agent-command $VM '{"execute": "guest-network-get-interfaces"}' | \
|
||||
jq '.return[] | select(.name=="'"$INTERFACE"'") | ."ip-addresses"[] | select(."ip-address-type"=="ipv4") | ."ip-address"' | \
|
||||
sed 's/"//g')"
|
||||
# Add the VM:IP pair if IP is not empty.
|
||||
if [ ! -z "$IP" ]; then
|
||||
VM_IP_PAIRS="$VM_IP_PAIRS"$'\n'"$VM:$IP"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# Write inventory
|
||||
cat /dev/null > inventory
|
||||
for GROUP in $INV_GROUPS; do
|
||||
echo "[$GROUP]" >> inventory
|
||||
echo "$VM_IP_PAIRS" | \
|
||||
grep $GROUP | \
|
||||
sed 's/^\(.*\):\(.*\)$/\1 ansible_host=\2/g' >> inventory
|
||||
done
|
||||
|
||||
# Print vars
|
||||
echo "$VM_IP_PAIRS" | \
|
||||
sed 's/^\(.*\):\(.*\)$/\1=\2/g' | \
|
||||
sed s/$NAME_PREFIX-//g | \
|
||||
sed 's/-/_/g' | \
|
||||
awk '{print toupper($1)}'
|
||||
@ -1,60 +1,92 @@
|
||||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# This script will create environment variables for all of the output IPs. It
|
||||
# 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.
|
||||
# This script will create environment variables for all of the output IPs. An
|
||||
# anisble inventory file is created as well.
|
||||
#
|
||||
# Use eval $(./get-vm-ips.sh) to set env vars for ips.
|
||||
|
||||
terraform refresh > /dev/null
|
||||
|
||||
# All terraform outputs in json format.
|
||||
OUTPUTS_JSON="$(
|
||||
terraform show -json | \
|
||||
jq '.values.outputs' | \
|
||||
sed 's/-/_/g')"
|
||||
# Just the IP address outputs in json format. Also all '-' characters are
|
||||
# replaced by '_' becuase '-' causes jq some problems.
|
||||
IPS_JSON="$(
|
||||
echo $OUTPUTS_JSON | \
|
||||
jq 'to_entries | .[] | select(.key | contains("ips"))')"
|
||||
# An array of all node "types"
|
||||
NODE_TYPE_ARRAY="$(
|
||||
echo $IPS_JSON | \
|
||||
jq '.value.value | to_entries | .[] | .key' | \
|
||||
sed 's/"//g' | \
|
||||
sed -z 's/\n/ /g;s/ $/\n/g')"
|
||||
|
||||
# Loop over all the node types and create an export line for each IP.
|
||||
VM_IP_EXPORTS="$(
|
||||
for TYPE in $NODE_TYPE_ARRAY; do
|
||||
|
||||
# Convert type, converts "master-ips" to "MASTER"
|
||||
TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)"
|
||||
echo "$IPS_JSON" | \
|
||||
jq '.value.value.'"$TYPE"'[]' | \
|
||||
# Add line numbers starting with 0.
|
||||
nl -v 0 | \
|
||||
# Print an export string with a type placeholder "__TYPE__".
|
||||
awk '{print "export __TYPE___" $1 "=" $2}' | \
|
||||
sed s/__TYPE__/$TYPE_UPPER/g
|
||||
done)"
|
||||
|
||||
ANSIBLE_INV="$(
|
||||
echo "$VM_IP_EXPORTS" | \
|
||||
sed 's/"//g' | \
|
||||
sed 's/^.*=//g' | \
|
||||
sed -z 's/\n/,/g;s/,$/\n/g')"
|
||||
# The file to write the inventory to. This file will be completely overridden.
|
||||
INVENTORY_FILE="inventory"
|
||||
|
||||
# Grab the the vm name prefix. We do this by greping all *.tfvars files making
|
||||
# sure to cat terraform.tfvars last. Then we just grab the last grep result,
|
||||
# this way we make sure any value in terraform.tfvars will take priority.
|
||||
VM_NAME_PREFIX_VAR="vm-name-prefix"
|
||||
VM_NAME_PREFIXES="$( \
|
||||
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX_VAR" {} \; && \
|
||||
grep "$VM_NAME_PREFIX_VAR" terraform.tfvars)"
|
||||
VM_NAME_PREFIX="$(
|
||||
echo "$VM_NAME_PREFIXES" | \
|
||||
tail -n 1 | \
|
||||
sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
||||
|
||||
# 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})})')"
|
||||
|
||||
# Create an inventory file for ansible.
|
||||
echo "[k8s_nodes]" > inventory
|
||||
echo $VM_IP_EXPORTS | \
|
||||
# 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/export //g' | \
|
||||
sed 's/ /\n/g' | \
|
||||
sed 's/^\(.*\)\(=.*\)$/\1 ansible_host\2/g' \
|
||||
>> inventory
|
||||
tr '\n' ' '
|
||||
)"
|
||||
|
||||
echo $VM_IP_EXPORTS | sed 's/" /"\n/g'
|
||||
echo export ANSIBLE_INV=$ANSIBLE_INV
|
||||
# Clear the inventory file.
|
||||
cat /dev/null > $INVENTORY_FILE
|
||||
|
||||
# For each group, write the VM info to $INVENTORY_FILE and also print a variable
|
||||
# expression to stdout.
|
||||
for GROUP in $ANS_GROUPS; do
|
||||
|
||||
# Write the inventory file to $INVENTORY_FILE.
|
||||
echo "[$GROUP]" >> $INVENTORY_FILE
|
||||
echo $DATA | \
|
||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
||||
"\(.hostname) ansible_host=\(.ip)"' | \
|
||||
sed 's/"//g' \
|
||||
>> $INVENTORY_FILE
|
||||
|
||||
# For this group, collect expressions into VARS. The format is:
|
||||
# HOSTNAME1=0.0.0.0
|
||||
# HOSTNAME2=0.0.0.0
|
||||
VARS="$(
|
||||
echo $DATA | \
|
||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
||||
"\(.hostname)=\(.ip)"' | \
|
||||
sed 's/"//g' | \
|
||||
sed "s/$VM_NAME_PREFIX-//g" | \
|
||||
sed 's/-/_/g'
|
||||
)"
|
||||
# Print the contents of $VARS converted to uppercase.
|
||||
echo "${VARS^^}"
|
||||
done
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
output "ips" {
|
||||
value = aws_instance.nodes.*.public_ip
|
||||
}
|
||||
|
||||
output "names" {
|
||||
value = aws_instance.nodes.*.tags.Name
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
|
||||
output "ips" {
|
||||
value = libvirt_domain.nodes.*.network_interface.0.addresses.0
|
||||
}
|
||||
# This only works on the default network. They will not work using the bridged
|
||||
# network.
|
||||
# output "ips" {
|
||||
# value = libvirt_domain.nodes.*.network_interface.0.addresses.0
|
||||
# }
|
||||
|
||||
Loading…
Reference in New Issue