105 lines
5.6 KiB
Markdown
105 lines
5.6 KiB
Markdown
|
---
|
||
|
title: "Ansible KVM Router Lab Part 3"
|
||
|
date: 2021-10-16
|
||
|
draft: false
|
||
|
tags: ["linux", "kvm", "libvirt", "virsh", "ansible", "bash"]
|
||
|
authors: ["trent"]
|
||
|
post: 28
|
||
|
---
|
||
|
date: 2021-10-16
|
||
|
|
||
|
## Introduction
|
||
|
This is Part 3 of a multi-part series of blog posts for building a
|
||
|
[router lab](https://github.com/TrentSPalmer/router-lab){target="_blank"}
|
||
|
automatically using a series of bash scripts and ansible.
|
||
|
|
||
|
[Ansible KVM Router Lab Part 1](/posts/ansible-kvm-router-lab-part-1/){target="_blank"}
|
||
|
is an overview.
|
||
|
|
||
|
In [Ansible KVM Router Lab Part 2](/posts/ansible-kvm-router-lab-part-2/){target="_blank"},
|
||
|
I break down the script
|
||
|
[build_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/build_vms.bash){target="_blank"}.
|
||
|
|
||
|
In this post I explain
|
||
|
[define_bridge_networks.bash](https://github.com/TrentSPalmer/router-lab/blob/master/define_bridge_networks.bash){target="_blank"}
|
||
|
and
|
||
|
[shutdown_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/shutdown_vms.bash){target="_blank"}
|
||
|
scripts which are used to construct the lab.
|
||
|
|
||
|
In [Ansible KVM Router Lab Part 4](/posts/ansible-kvm-router-lab-part-4/){target="_blank"},
|
||
|
I explain
|
||
|
[connect_vms_to_bridges.bash](https://github.com/TrentSPalmer/router-lab/blob/master/connect_vms_to_bridges.bash){target="_blank"},
|
||
|
[start_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/start_vms.bash){target="_blank"},
|
||
|
and [rebuild_known_hosts.bash](https://github.com/TrentSPalmer/router-lab/blob/master/rebuild_known_hosts.bash){target="_blank"}
|
||
|
scripts which are used to construct the lab.
|
||
|
|
||
|
In [Ansible KVM Router Lab Part 5](/posts/ansible-kvm-router-lab-part-5/){target="_blank"},
|
||
|
I explain the ansible playbook tasks used to finish building the lab.
|
||
|
|
||
|
In [Ansible KVM Router Lab Part 6](/posts/ansible-kvm-router-lab-part-6/){target="_blank"},
|
||
|
I explain
|
||
|
[disconnect_vms_from_bridges.bash](https://github.com/TrentSPalmer/router-lab/blob/master/disconnect_vms_from_bridges.bash){target="_blank"},
|
||
|
[undefine_and_remove_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/undefine_and_remove_vms.bash){target="_blank"},
|
||
|
and [remove_bridge_networks](https://github.com/TrentSPalmer/router-lab/blob/master/remove_bridge_networks.bash){target="_blank"}
|
||
|
which are used to destroy the lab.
|
||
|
|
||
|
## `define_bridge_networks.bash`
|
||
|
The router-lab has two bridge networks, in addition to the initial out-of-band
|
||
|
network which is used to contact the virtual machines directly.
|
||
|
|
||
|
### `check_uid "${USER_UID}"`
|
||
|
[define_bridge_networks.bash](https://github.com/TrentSPalmer/router-lab/blob/master/define_bridge_networks.bash)
|
||
|
begins by making sure that it is run as a _non-privileged_ user. You can call
|
||
|
the script with `bash define_bridge_networks.bash`.
|
||
|
|
||
|
### `function define_bridge_networks()`
|
||
|
`define_bridge_networks` calls `define_bridge_network` twice, once for each of the
|
||
|
upper bridge and the lower bridge. `define_bridge_network` parses the output of
|
||
|
`virsh net-list --all` to determine if the network is defined yet. If not,
|
||
|
`virsh net-define vm_router_lab_lower_bridge.xml` or
|
||
|
`virsh net-define vm_router_lab_upper_bridge.xml` are invoked as necessary.
|
||
|
`define_bridge_network` then recursively calls itself for confirmation.
|
||
|
|
||
|
Links for
|
||
|
[vm_router_lab_upper_bridge.xml](https://github.com/TrentSPalmer/router-lab/blob/master/vm_router_lab_upper_bridge.xml){target="_blank"}
|
||
|
and
|
||
|
[vm_router_lab_lower_bridge.xml](https://github.com/TrentSPalmer/router-lab/blob/master/vm_router_lab_lower_bridge.xml){target="_blank"}.
|
||
|
|
||
|
### `function start_bridge_networks()`
|
||
|
`start_bridge_networks` calls `start_bridge_network` twice, once for each of the
|
||
|
upper and the lower bridge. `start_bridge_network` in turn parses the output of
|
||
|
`virsh net-info vm_router_lab_upper_bridge` and/or `virsh net-info vm_router_lab_lower_bridge`
|
||
|
to determine if the cooresponding network is running, and if not invokes
|
||
|
`virsh net-start vm_router_lab_upper_bridge` or `virsh net-start vm_router_lab_lower_bridge`,
|
||
|
and then recursively calls itself again for confirmation.
|
||
|
|
||
|
### `function autostart_bridge_networks()`
|
||
|
`autostart_bridge_networks` is nearly identical to `start_bridge_networks`, but
|
||
|
`virsh net-autostart vm_router_lab_upper_bridge` or `virsh net-autostart vm_router_lab_lower_bridge`,
|
||
|
are invoked in order to mark the cooresponding network to autostart.
|
||
|
|
||
|
## `shutdown_vms.bash`
|
||
|
After creating the upper and lower bridge networks, it is necessary to shut down
|
||
|
the lab clients before connecting the lab clients to the bridge networks. This is
|
||
|
because network interfaces must be _permanently_ added to the lab client definitions.
|
||
|
|
||
|
### `check_uid "${USER_UID}"`
|
||
|
[shutdown_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/shutdown_vms.bash)
|
||
|
begins by making sure that it is run as a _non-privileged_ user. You can call
|
||
|
the script with `bash shutdown_vms.bash`.
|
||
|
|
||
|
### `function shutdown_vms()`
|
||
|
`shutdown_vms` simultaneously calls `shutdown_vm` on the entire MACHINES array.
|
||
|
`shutdown_vm` in turn parses the output of `virsh list --state-running` to determine if
|
||
|
the virtual machine is running, and if so invokes `virsh shutdown <vm>`. `shutdown_vm`
|
||
|
then recursively calls itself to confirm that the virtual machine is indeed shut down.
|
||
|
|
||
|
## To Be Continued
|
||
|
In [Ansible KVM Router Lab Part 4](/posts/ansible-kvm-router-lab-part-4/){target="_blank"},
|
||
|
I explain
|
||
|
[connect_vms_to_bridges.bash](https://github.com/TrentSPalmer/router-lab/blob/master/connect_vms_to_bridges.bash){target="_blank"},
|
||
|
[start_vms.bash](https://github.com/TrentSPalmer/router-lab/blob/master/start_vms.bash){target="_blank"},
|
||
|
and
|
||
|
[rebuild_known_hosts.bash](https://github.com/TrentSPalmer/router-lab/blob/master/rebuild_known_hosts.bash){target="_blank"}
|
||
|
scripts which are used to construct the lab.
|