Skip to content

Ansible KVM Router Lab Part 2

date: 2021-10-16

Introduction

This is Part 2 of a multi-part series of blog posts for building a router lab automatically using a series of bash scripts and ansible.

Ansible KVM Router Lab Part 1 is an overview.

In this post I begin breaking down the bash scripts which build the router lab, beginning with build_vms.bash.

In Ansible KVM Router Lab Part 3, I explain define_bridge_networks.bash and shutdown_vms.bash scripts which are used to construct the lab.

In Ansible KVM Router Lab Part 4, I explain connect_vms_to_bridges.bash, start_vms.bash, and rebuild_known_hosts.bash scripts which are used to construct the lab.

In Ansible KVM Router Lab Part 5, I explain the ansible playbook tasks used to finish building the lab.

In Ansible KVM Router Lab Part 6, I explain disconnect_vms_from_bridges.bash, undefine_and_remove_vms.bash, and remove_bridge_networks which are used to destroy the lab.

build_vms.bash

check_uid "0"

build_vms.bash begins by making sure that it is run as the root user. This is because root is required to ssh into the clones to change their hostnames, machine-ids, and host-ssh-keys. You can call with sudo bash build_vms.bash.

For this same reason, ~/.ssh/known_hosts is useless so it is deleted (and then rebuilt).

function build_vms()

Next, build_vms.bash calls build_vms, which loops over the array of MACHINES, which is an array that holds that names of the lab clients, passing each name in turn to create_vm.

create_vm creates the virtual machine if it does not already exist, using virt-clone, and then calls start_vm to start it.

start_vm is exported from env.bash, and per parsing the output of virsh list --inactive, starts the virtual machine if it is not running.

function set_hostnames()

Next, build_vms.bash calls set_hostnames, which simultaneously calls set_hostname on the entire MACHINES array. set_hostname in turn waits for the virtual machine to be fully booted, then updates the files /etc/hostname and /etc/hosts, and then reboots the virtual machine to apply the new hostname.

function confirm_hostnames()

confirm_hostnames simultaneously calls confirm_hostname against the entire MACHINES array. confirm_hostname waits for the virtual machine to be fully booted, then confirms the correct hostname in /etc/hostname.

function confirm_hostnames_in_hosts()

confirm_hostnames_in_hosts works almost exactly the same as confirm_hostnames, but this time the file /etc/hosts on the virtual machine is grepped for the proper hostname, and corrected if necessary.

function reset_hosts_ssh_keys()

reset_hosts_ssh_keys simultaneously calls reset_host_ssh_keys against the MACHINES array, which in turn compares the host_ssh_key of the virtual machine against the bas3 virtual machine, and if necessary deletes /etc/ssh/ssh_host_*, generates new host_ssh_keys, restarts sshd on the virtual machine, removes ~/.ssh/known_hosts, and then reruns itself in order to confirm the new host_ssh_keys.

function reset_machine_ids()

reset_machine_ids simultaneously calls reset_machine_id against the entire MACHINES array, which in turn checks the machine-id of the virtual machine to make sure that it is different than the machine-id of the base virtual machine, and if necessary deletes /etc/machine-id and /var/lib/dbus/machine-id and recreates them.

To Be Continued

In Ansible KVM Router Lab Part 3, I explain define_bridge_networks.bash and shutdown_vms.bash scripts which are used to construct the lab.