Updated get-vim-ips script to use hostnames.
- Made the qemu-agent content in the cloud-init config to be controlled by a 'install-qemu-agent' flag.
This commit is contained in:
+4
-2
@@ -26,15 +26,17 @@ chpasswd:
|
|||||||
hostname: ${hostname}
|
hostname: ${hostname}
|
||||||
fqdn: ${hostname}
|
fqdn: ${hostname}
|
||||||
|
|
||||||
|
%{ if install-qemu-agent }
|
||||||
packages:
|
packages:
|
||||||
# TODO These are only necessary for libvirt.
|
# This are only necessary for libvirt.
|
||||||
- qemu-guest-agent
|
- qemu-guest-agent
|
||||||
runcmd:
|
runcmd:
|
||||||
# TODO At some point revisit this, this was added because it seemed like
|
# TODO At some point revisit this, this was added because it seemed like
|
||||||
# apparmor was causing dhclient to not get an IP address for ubuntu. This
|
# apparmor was causing dhclient to not get an IP address for ubuntu. This
|
||||||
# should be double checked.
|
# should be double checked.
|
||||||
- echo "/proc/*/task/*/comm wr," | tee -a /etc/apparmor.d/local/sbin.dhclient
|
- echo "/proc/*/task/*/comm wr," | tee -a /etc/apparmor.d/local/sbin.dhclient
|
||||||
# TODO These are only necessary for libvirt.
|
# These are only necessary for libvirt.
|
||||||
- systemctl enable qemu-guest-agent
|
- systemctl enable qemu-guest-agent
|
||||||
- systemctl start qemu-guest-agent
|
- systemctl start qemu-guest-agent
|
||||||
- systemctl status qemu-guest-agent
|
- systemctl status qemu-guest-agent
|
||||||
|
%{ endif }
|
||||||
|
|||||||
+81
-49
@@ -1,60 +1,92 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
|
|
||||||
# This script will create environment variables for all of the output IPs. It
|
# This script will create environment variables for all of the output IPs. An
|
||||||
# will also create a `ANSIBLE_INV` variable that will be a comma separated
|
# anisble inventory file is created as well.
|
||||||
# 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
|
||||||
|
|
||||||
# All terraform outputs in json format.
|
# The file to write the inventory to. This file will be completely overridden.
|
||||||
OUTPUTS_JSON="$(
|
INVENTORY_FILE="inventory"
|
||||||
terraform show -json | \
|
|
||||||
jq '.values.outputs' | \
|
# Grab the the vm name prefix. We do this by greping all *.tfvars files making
|
||||||
sed 's/-/_/g')"
|
# sure to cat terraform.tfvars last. Then we just grab the last grep result,
|
||||||
# Just the IP address outputs in json format. Also all '-' characters are
|
# this way we make sure any value in terraform.tfvars will take priority.
|
||||||
# replaced by '_' becuase '-' causes jq some problems.
|
VM_NAME_PREFIX_VAR="vm-name-prefix"
|
||||||
IPS_JSON="$(
|
VM_NAME_PREFIXES="$( \
|
||||||
echo $OUTPUTS_JSON | \
|
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX_VAR" {} \; && \
|
||||||
jq 'to_entries | .[] | select(.key | contains("ips"))')"
|
grep "$VM_NAME_PREFIX_VAR" terraform.tfvars)"
|
||||||
# An array of all node "types"
|
VM_NAME_PREFIX="$(
|
||||||
NODE_TYPE_ARRAY="$(
|
echo "$VM_NAME_PREFIXES" | \
|
||||||
echo $IPS_JSON | \
|
tail -n 1 | \
|
||||||
jq '.value.value | to_entries | .[] | .key' | \
|
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})})')"
|
||||||
|
|
||||||
|
# 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' | \
|
||||||
sed -z 's/\n/ /g;s/ $/\n/g')"
|
tr '\n' ' '
|
||||||
|
)"
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# Convert type, converts "master-ips" to "MASTER"
|
# For each group, write the VM info to $INVENTORY_FILE and also print a variable
|
||||||
TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)"
|
# expression to stdout.
|
||||||
echo "$IPS_JSON" | \
|
for GROUP in $ANS_GROUPS; do
|
||||||
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="$(
|
# Write the inventory file to $INVENTORY_FILE.
|
||||||
echo "$VM_IP_EXPORTS" | \
|
echo "[$GROUP]" >> $INVENTORY_FILE
|
||||||
sed 's/"//g' | \
|
echo $DATA | \
|
||||||
sed 's/^.*=//g' | \
|
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
||||||
sed -z 's/\n/,/g;s/,$/\n/g')"
|
"\(.hostname) ansible_host=\(.ip)"' | \
|
||||||
|
sed 's/"//g' \
|
||||||
|
>> $INVENTORY_FILE
|
||||||
|
|
||||||
# Create an inventory file for ansible.
|
# For this group, collect expressions into VARS. The format is:
|
||||||
echo "[k8s_nodes]" > inventory
|
# HOSTNAME1=0.0.0.0
|
||||||
echo $VM_IP_EXPORTS | \
|
# HOSTNAME2=0.0.0.0
|
||||||
sed 's/"//g' | \
|
VARS="$(
|
||||||
sed 's/export //g' | \
|
echo $DATA | \
|
||||||
sed 's/ /\n/g' | \
|
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
||||||
sed 's/^\(.*\)\(=.*\)$/\1 ansible_host\2/g' \
|
"\(.hostname)=\(.ip)"' | \
|
||||||
>> inventory
|
sed 's/"//g' | \
|
||||||
|
sed "s/$VM_NAME_PREFIX-//g" | \
|
||||||
echo $VM_IP_EXPORTS | sed 's/" /"\n/g'
|
sed 's/-/_/g'
|
||||||
echo export ANSIBLE_INV=$ANSIBLE_INV
|
)"
|
||||||
|
# Print the contents of $VARS converted to uppercase.
|
||||||
|
echo "${VARS^^}"
|
||||||
|
done
|
||||||
|
|||||||
@@ -11,14 +11,15 @@ terraform {
|
|||||||
locals {
|
locals {
|
||||||
nodes-config = {
|
nodes-config = {
|
||||||
"master" = {
|
"master" = {
|
||||||
base-image = var.amzn2-ami
|
base-image = var.centos8-ami
|
||||||
num = 1
|
num = 1
|
||||||
},
|
},
|
||||||
"worker" = {
|
"worker" = {
|
||||||
base-image = var.amzn2-ami
|
base-image = var.centos8-ami
|
||||||
num = 2
|
num = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
install-qemu-agent = false
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -33,6 +34,7 @@ module "cloud-init-config" {
|
|||||||
num = each.value.num
|
num = each.value.num
|
||||||
root-admin-passwd = var.root-admin-passwd
|
root-admin-passwd = var.root-admin-passwd
|
||||||
root-admin-pub-key = var.root-admin-pub-key
|
root-admin-pub-key = var.root-admin-pub-key
|
||||||
|
install-qemu-agent = local.install-qemu-agent
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
@@ -125,6 +127,8 @@ module "nodes" {
|
|||||||
# end libvirt
|
# end libvirt
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
output "ips" {
|
# This will outpus a map of group => [{hostname, ip}].
|
||||||
value = { for type, node in module.nodes : type => node.ips }
|
# TODO A 'names' output needs to be added to libvirt.
|
||||||
|
output "groups_hostnames_ips" {
|
||||||
|
value = { for type, node in module.nodes : type => zipmap(node.names, node.ips) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
output "ips" {
|
output "ips" {
|
||||||
value = aws_instance.nodes.*.public_ip
|
value = aws_instance.nodes.*.public_ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output "names" {
|
||||||
|
value = aws_instance.nodes.*.tags.Name
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ variable "hostname-prefix" {
|
|||||||
description = "This prefix wil be applied as a prefix for the hostnames."
|
description = "This prefix wil be applied as a prefix for the hostnames."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "install-qemu-agent" {
|
||||||
|
default = false
|
||||||
|
description = "This flag determines whether or not qemu-agent is installed."
|
||||||
|
type = bool
|
||||||
|
}
|
||||||
|
|
||||||
variable "num" {
|
variable "num" {
|
||||||
description = "The number of user-datas to create with these parameters."
|
description = "The number of user-datas to create with these parameters."
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user