add script to automatically add SSH key pair to main.tf files, for deployments that do require it
122 lines
2.7 KiB
Markdown
122 lines
2.7 KiB
Markdown
# Modern libvirt-driven Terraform examples
|
|
|
|
This repository contains Terraform recipes to deploy various modern virtual machines using QEMU and libvirt.
|
|
|
|
By modern, it is meant virtual machines that leverage the use of modern desktop-oriented technologies, like UEFI firmware and recent virtual motherboard chipset (i.e. Phyllome OS itself), by staying as close as possible as domain definitions maintained [here](https://git.phyllo.me/roots/xml-definition-for-domains).
|
|
|
|
## Organization
|
|
|
|
The folder *multiple* contains two subfolders, one with shared modules and the other with the various target deployment environments.
|
|
|
|
The idea is to reuse modules across multiple virtual machines and operating systems.
|
|
|
|
```
|
|
.:
|
|
environments shared_modules
|
|
|
|
./environments:
|
|
cloud_init.yaml ubuntu-cloud-server-2404-bios
|
|
|
|
./environments/ubuntu-cloud-server-2404-bios:
|
|
ubuntu-cloud-server-2404-bios.tf
|
|
|
|
./shared_modules:
|
|
cloud-init.tf domain.tf network.tf outputs.tf pool.tf provider.tf variables.tf volume.tf
|
|
```
|
|
|
|
## Requirements
|
|
|
|
- [QEMU](https://www.qemu.org/)
|
|
- [libvirt](https://libvirt.org/)
|
|
- [Terraform provider for Libvirt](https://github.com/dmacvicar/terraform-provider-libvirt)
|
|
- An SSH key pair to connect to machines that are deployed using cloud-init
|
|
|
|
## Assumptions
|
|
|
|
- Your Linux x86_64-based machine has at least 4 GB of available memory and 2 CPUs
|
|
|
|
## How to use it
|
|
|
|
- Clone this repository
|
|
- Run the following to generate a public key pair
|
|
|
|
```
|
|
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/terraform_key -C "terraform-deployment"
|
|
```
|
|
|
|
- Make the script executable
|
|
```
|
|
$ chmod +x update_ssh_keys.sh
|
|
```
|
|
|
|
- Run the script (it will use terraform_key by default), and will update all `main.tf` file so that they use the previously generated key:
|
|
|
|
```
|
|
$ ./update_ssh_keys.sh
|
|
```
|
|
|
|
> Alternatively, you can use your own public key and update it manually in the `main.tf` deployment file
|
|
|
|
- Navigate to one of the available deployment
|
|
|
|
```
|
|
$ cd environments/ubuntu-cloud-server-2404-bios/
|
|
```
|
|
|
|
- Initialize your terraform environment
|
|
|
|
```
|
|
$ terraform init
|
|
```
|
|
- Plan the deployment
|
|
|
|
```
|
|
$ terraform plan
|
|
```
|
|
|
|
- Deploy
|
|
|
|
```
|
|
$ terraform deploy
|
|
```
|
|
|
|
- Identify the name of the machine, which requires elevated privileges
|
|
|
|
```
|
|
# virsh list --all
|
|
Id Name State
|
|
--------------------------------------------
|
|
2 u24-bios-0 running
|
|
```
|
|
|
|
- Fetch IP address
|
|
|
|
```
|
|
# virsh domifaddr u24-bios-0
|
|
```
|
|
|
|
- Connect to the machine with the user `groot`
|
|
|
|
```
|
|
$ ssh groot@10.17.3.107
|
|
```
|
|
|
|
```
|
|
groot@ubuntu:~$
|
|
```
|
|
|
|
- Logout
|
|
|
|
```
|
|
$ exit
|
|
```
|
|
|
|
- Destroy the machine
|
|
|
|
```
|
|
$ terraform destroy
|
|
```
|
|
|
|
## Resources
|
|
|
|
- [Terraform provider domain documentation](https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/domain.html.markdown) |