Compare commits

..

No commits in common. '6c2d3e3837d7237bf2d56baae649371c343ae4ce' and '482e8d10276c05e54acb84ed7802efe8204f2c53' have entirely different histories.

@ -26,17 +26,15 @@ chpasswd:
hostname: ${hostname} hostname: ${hostname}
fqdn: ${hostname} fqdn: ${hostname}
%{ if install-qemu-agent }
packages: packages:
# This are only necessary for libvirt. # TODO These 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
# These are only necessary for libvirt. # TODO 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 }

@ -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

@ -11,15 +11,14 @@ terraform {
locals { locals {
nodes-config = { nodes-config = {
"master" = { "master" = {
base-image = var.centos8-ami base-image = var.amzn2-ami
num = 1 num = 1
}, },
"worker" = { "worker" = {
base-image = var.centos8-ami base-image = var.amzn2-ami
num = 2 num = 2
} }
} }
install-qemu-agent = false
} }
################################################################################ ################################################################################
@ -34,7 +33,6 @@ 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
} }
################################################################################ ################################################################################
@ -127,8 +125,6 @@ module "nodes" {
# end libvirt # end libvirt
################################################################################ ################################################################################
# This will outpus a map of group => [{hostname, ip}]. output "ips" {
# TODO A 'names' output needs to be added to libvirt. value = { for type, node in module.nodes : type => node.ips }
output "groups_hostnames_ips" {
value = { for type, node in module.nodes : type => zipmap(node.names, node.ips) }
} }

@ -33,7 +33,7 @@ resource "aws_default_security_group" "sg" {
} }
tags = { tags = {
Name = "${var.name-prefix}-ssh-from-admins-sg" Name = "${var.name-prefix}-ssh-from-admins--sg"
} }
} }

@ -1,7 +1,3 @@
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,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
} }

@ -8,12 +8,6 @@ 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."
} }

Loading…
Cancel
Save