feat: ubuntu terraform libvirt example

This commit is contained in:
Aravinth Manivannan 2022-10-24 10:54:04 +05:30
parent eb17b35d20
commit d9cf6f08f1
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
7 changed files with 385 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.terraform/

View File

@ -1,5 +1,8 @@
## Infrastructure
### libvirt
1. [Terraform libvirt](https://github.com/bodsch/terraform-libvirt)
### OpenStack
1. [Upload Debian image to Glance](https://cloud.debian.org/images/cloud/)
@ -8,6 +11,7 @@
### Testing
- [serverspec.org](https://serverspec.org/tutorial.html): Ruby based

View File

@ -0,0 +1,41 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/dmacvicar/libvirt" {
version = "0.7.0"
constraints = "~> 0.7.0"
hashes = [
"h1:1RiomFBEdgi6GvqaAf16mW6bRqrxAV0P47HKKwETH3E=",
"zh:1161bfcac075d5790c9b36145811d95241622636b859222f995888471ba04efa",
"zh:317bca5edd36e2497e0ac2ed07dd5e7f09cfd5561a96607cb53fe1af6b0632c0",
"zh:4fa67e3baf6845148f2b4e617fb01c47f0971ce2d945efa805ac5c3820bb0ca6",
"zh:6e17f5f24373e21c0ff463d36d9caa4f08528e13764c5d1d7eceb719dcef6a14",
"zh:84622e2aca8bc91d71d3596fcd1b298c5dfe572c8722ab98084495d26b5c5e7d",
"zh:8ce125d872b26ce9b71a729437eb8ab36944a86da3784edaab7368af43ca3858",
"zh:8fc7eee76776d515c023d013c018a7b9816f0e840578af01bfaf58e49f020c03",
"zh:a4d6fccc0188746be35488396c431e4b313cd1221df408871c710d3a7382b02e",
"zh:b575bb2d2f8987043aecbb22ac3bbf1e9c8b9da49b201b6b225baf2b4595dae4",
"zh:b65b1733c29a09491912a98a829b19c9842af5971fbb358bc0e979b95bf33248",
"zh:b8266ed7b4bce4791fee5433d102d89187974a273574d69f637cfdeb913462c2",
"zh:bd0b842d6f694c6d558d3329a2c157dd9d84074d618d5ced891ef36798b1c97b",
"zh:dacf0299c2c11d84bdaa2f614ca14aeac36ffba0f20dff5a63437a81a61f6867",
"zh:e8c92794a06df42c15ff071859e99c6e95e93dcb40797c4128d31d3a47a27923",
]
}
provider "registry.terraform.io/hashicorp/template" {
version = "2.2.0"
hashes = [
"h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=",
"zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386",
"zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53",
"zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603",
"zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16",
"zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776",
"zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451",
"zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae",
"zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde",
"zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d",
"zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2",
]
}

View File

@ -0,0 +1,19 @@
#cloud-config
# vim: syntax=yaml
#
# ***********************
# ---- for more examples look at: ------
# ---> https://cloudinit.readthedocs.io/en/latest/topics/examples.html
# ******************************
#
# This is the configuration syntax that the write_files module
# will know how to understand. encoding can be given b64 or gzip or (gz+b64).
# The content will be decoded accordingly and then written to the path that is
# provided.
#
# Note: Content strings here are truncated for example purposes.
ssh_pwauth: True
chpasswd:
list: |
root:terraform-libvirt-linux
expire: False

View File

@ -0,0 +1,4 @@
version: 2
ethernets:
ens3:
dhcp4: true

View File

@ -0,0 +1,229 @@
{
"version": 4,
"terraform_version": "1.3.2",
"serial": 5,
"lineage": "ad020180-a9b1-2642-e7c1-0de5d2b318c5",
"outputs": {},
"resources": [
{
"mode": "data",
"type": "template_file",
"name": "network_config",
"provider": "provider[\"registry.terraform.io/hashicorp/template\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"filename": null,
"id": "b36a1372ce4ea68b514354202c26c0365df9a17f25cd5acdeeaea525cd913edc",
"rendered": "version: 2\nethernets:\n ens3:\n dhcp4: true\n",
"template": "version: 2\nethernets:\n ens3:\n dhcp4: true\n",
"vars": null
},
"sensitive_attributes": []
}
]
},
{
"mode": "data",
"type": "template_file",
"name": "user_data",
"provider": "provider[\"registry.terraform.io/hashicorp/template\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"filename": null,
"id": "74c90b4966a0af6c145fc5551db49d4e5e463b0e5cdf771229aee494649157e3",
"rendered": "#cloud-config\n# vim: syntax=yaml\n#\n# ***********************\n# \t---- for more examples look at: ------\n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n#\n# This is the configuration syntax that the write_files module\n# will know how to understand. encoding can be given b64 or gzip or (gz+b64).\n# The content will be decoded accordingly and then written to the path that is\n# provided.\n#\n# Note: Content strings here are truncated for example purposes.\nssh_pwauth: True\nchpasswd:\n list: |\n root:terraform-libvirt-linux\n expire: False\n",
"template": "#cloud-config\n# vim: syntax=yaml\n#\n# ***********************\n# \t---- for more examples look at: ------\n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n#\n# This is the configuration syntax that the write_files module\n# will know how to understand. encoding can be given b64 or gzip or (gz+b64).\n# The content will be decoded accordingly and then written to the path that is\n# provided.\n#\n# Note: Content strings here are truncated for example purposes.\nssh_pwauth: True\nchpasswd:\n list: |\n root:terraform-libvirt-linux\n expire: False\n",
"vars": null
},
"sensitive_attributes": []
}
]
},
{
"mode": "managed",
"type": "libvirt_cloudinit_disk",
"name": "commoninit",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "/tmp/terraform-provider-libvirt-pool-ubuntu/commoninit.iso;f998a504-a990-4b4a-bb52-4df8b70f3934",
"meta_data": "",
"name": "commoninit.iso",
"network_config": "version: 2\nethernets:\n ens3:\n dhcp4: true\n",
"pool": "ubuntu",
"user_data": "#cloud-config\n# vim: syntax=yaml\n#\n# ***********************\n# \t---- for more examples look at: ------\n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n#\n# This is the configuration syntax that the write_files module\n# will know how to understand. encoding can be given b64 or gzip or (gz+b64).\n# The content will be decoded accordingly and then written to the path that is\n# provided.\n#\n# Note: Content strings here are truncated for example purposes.\nssh_pwauth: True\nchpasswd:\n list: |\n root:terraform-libvirt-linux\n expire: False\n"
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"data.template_file.network_config",
"data.template_file.user_data",
"libvirt_pool.ubuntu"
]
}
]
},
{
"mode": "managed",
"type": "libvirt_domain",
"name": "domain-ubuntu",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arch": "x86_64",
"autostart": false,
"boot_device": [],
"cloudinit": "/tmp/terraform-provider-libvirt-pool-ubuntu/commoninit.iso;f998a504-a990-4b4a-bb52-4df8b70f3934",
"cmdline": null,
"console": [
{
"source_host": "127.0.0.1",
"source_path": "",
"source_service": "0",
"target_port": "0",
"target_type": "serial",
"type": "pty"
},
{
"source_host": "127.0.0.1",
"source_path": "",
"source_service": "0",
"target_port": "1",
"target_type": "virtio",
"type": "pty"
}
],
"coreos_ignition": null,
"cpu": [
{
"mode": "custom"
}
],
"description": "",
"disk": [
{
"block_device": "",
"file": "",
"scsi": false,
"url": "",
"volume_id": "/tmp/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2",
"wwn": ""
}
],
"emulator": "/usr/bin/qemu-system-x86_64",
"filesystem": [],
"firmware": null,
"fw_cfg_name": "opt/com.coreos/config",
"graphics": [
{
"autoport": true,
"listen_address": "127.0.0.1",
"listen_type": "address",
"type": "spice",
"websocket": 0
}
],
"id": "c932a058-d7f3-4b2c-a21f-b54a3c3e8c10",
"initrd": "",
"kernel": "",
"machine": "pc",
"memory": 512,
"metadata": null,
"name": "ubuntu-terraform",
"network_interface": [
{
"addresses": [],
"bridge": "",
"hostname": "",
"mac": "52:54:00:AA:5B:84",
"macvtap": "",
"network_id": "f50e127a-7413-4d45-80de-587b22da0aa6",
"network_name": "default",
"passthrough": "",
"vepa": "",
"wait_for_lease": false
}
],
"nvram": [],
"qemu_agent": false,
"running": true,
"timeouts": null,
"tpm": [],
"vcpu": 1,
"video": [],
"xml": []
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==",
"dependencies": [
"data.template_file.network_config",
"data.template_file.user_data",
"libvirt_cloudinit_disk.commoninit",
"libvirt_pool.ubuntu",
"libvirt_volume.ubuntu-qcow2"
]
}
]
},
{
"mode": "managed",
"type": "libvirt_pool",
"name": "ubuntu",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"allocation": 36864,
"available": 11981750272,
"capacity": 11981787136,
"id": "b8d85970-bfed-4907-9b73-c26c3578b7c6",
"name": "ubuntu",
"path": "/tmp/terraform-provider-libvirt-pool-ubuntu",
"type": "dir",
"xml": []
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
},
{
"mode": "managed",
"type": "libvirt_volume",
"name": "ubuntu-qcow2",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"base_volume_id": null,
"base_volume_name": null,
"base_volume_pool": null,
"format": "qcow2",
"id": "/tmp/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2",
"name": "ubuntu-qcow2",
"pool": "ubuntu",
"size": 2361393152,
"source": "https://cloud-images.ubuntu.com/releases/xenial/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
"xml": []
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"libvirt_pool.ubuntu"
]
}
]
}
],
"check_results": []
}

View File

@ -0,0 +1,87 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.7.0"
}
}
}
# instance the provider
provider "libvirt" {
uri = "qemu:///system"
}
resource "libvirt_pool" "ubuntu" {
name = "ubuntu"
type = "dir"
path = "/tmp/terraform-provider-libvirt-pool-ubuntu"
}
# We fetch the latest ubuntu release image from their mirrors
resource "libvirt_volume" "ubuntu-qcow2" {
name = "ubuntu-qcow2"
pool = libvirt_pool.ubuntu.name
source = "https://cloud-images.ubuntu.com/releases/xenial/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img"
format = "qcow2"
}
data "template_file" "user_data" {
template = file("${path.module}/cloud_init.cfg")
}
data "template_file" "network_config" {
template = file("${path.module}/network_config.cfg")
}
# for more info about paramater check this out
# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/cloudinit.html.markdown
# Use CloudInit to add our ssh-key to the instance
# you can add also meta_data field
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
pool = libvirt_pool.ubuntu.name
}
# Create the machine
resource "libvirt_domain" "domain-ubuntu" {
name = "ubuntu-terraform"
memory = "512"
vcpu = 1
cloudinit = libvirt_cloudinit_disk.commoninit.id
network_interface {
network_name = "default"
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = libvirt_volume.ubuntu-qcow2.id
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
# IPs: use wait_for_lease true or after creation use terraform refresh and terraform show for the ips of domain