feat: deploy 2 machines
This commit is contained in:
1
opdracht-1/.gitignore
vendored
Normal file
1
opdracht-1/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
inventory.ini
|
41
opdracht-1/.terraform.lock.hcl
generated
Normal file
41
opdracht-1/.terraform.lock.hcl
generated
Normal 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/hashicorp/azurerm" {
|
||||||
|
version = "4.27.0"
|
||||||
|
constraints = "~> 4.27.0"
|
||||||
|
hashes = [
|
||||||
|
"h1:2fs47aLDaEm93ANXXVRdTjlbUBmFBZRsFjyshKoPE3o=",
|
||||||
|
"zh:0c69edea1995bd3bd9e61980757169c35bf22281b660b5c755b6cb13d08d29d2",
|
||||||
|
"zh:25b86bf7b9678371d8573983954c571696f3e64a3967133be3b835da36307106",
|
||||||
|
"zh:49921cff4f26a49bafada60cd07dabb52c5eb35231059ed928a4f4722e269c82",
|
||||||
|
"zh:4b986166531f9fd1289f01d8220519443e74888a21da512c1b841b006dad6215",
|
||||||
|
"zh:53fb65b2ca4df637f03e4748a100a7d7fc77249e307c03e294d6259cec0310f6",
|
||||||
|
"zh:5c0d021a387ca4e2a5a01da009746a08c45f08e971c10d9bda54539d7264d671",
|
||||||
|
"zh:600043f2b20dc5a45275e43f175c19fe8b6e8e9557a0c884aef018f1f63de90e",
|
||||||
|
"zh:a0284f6f38912f67bb4cb7829fda3fa75be81fea6a9b21119965c2a839430092",
|
||||||
|
"zh:a7ac0576e2069ef77557042c6b5157ded364fbd355b2f9bf7f5441622424086e",
|
||||||
|
"zh:c5db0bcafe986868e28cc6225b68b2d1cf4bf631939d260ca845f17a9aa1677d",
|
||||||
|
"zh:ce620c0eb71b1fdd925828b30cf232a869abccf1c459180f2f991c4166315251",
|
||||||
|
"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "registry.terraform.io/hashicorp/local" {
|
||||||
|
version = "2.5.2"
|
||||||
|
hashes = [
|
||||||
|
"h1:JlMZD6nYqJ8sSrFfEAH0Vk/SL8WLZRmFaMUF9PJK5wM=",
|
||||||
|
"zh:136299545178ce281c56f36965bf91c35407c11897f7082b3b983d86cb79b511",
|
||||||
|
"zh:3b4486858aa9cb8163378722b642c57c529b6c64bfbfc9461d940a84cd66ebea",
|
||||||
|
"zh:4855ee628ead847741aa4f4fc9bed50cfdbf197f2912775dd9fe7bc43fa077c0",
|
||||||
|
"zh:4b8cd2583d1edcac4011caafe8afb7a95e8110a607a1d5fb87d921178074a69b",
|
||||||
|
"zh:52084ddaff8c8cd3f9e7bcb7ce4dc1eab00602912c96da43c29b4762dc376038",
|
||||||
|
"zh:71562d330d3f92d79b2952ffdda0dad167e952e46200c767dd30c6af8d7c0ed3",
|
||||||
|
"zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3",
|
||||||
|
"zh:805f81ade06ff68fa8b908d31892eaed5c180ae031c77ad35f82cb7a74b97cf4",
|
||||||
|
"zh:8b6b3ebeaaa8e38dd04e56996abe80db9be6f4c1df75ac3cccc77642899bd464",
|
||||||
|
"zh:ad07750576b99248037b897de71113cc19b1a8d0bc235eb99173cc83d0de3b1b",
|
||||||
|
"zh:b9f1c3bfadb74068f5c205292badb0661e17ac05eb23bfe8bd809691e4583d0e",
|
||||||
|
"zh:cc4cbcd67414fefb111c1bf7ab0bc4beb8c0b553d01719ad17de9a047adff4d1",
|
||||||
|
]
|
||||||
|
}
|
9
opdracht-1/ansible-inventory.tmpl
Normal file
9
opdracht-1/ansible-inventory.tmpl
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[webservers]
|
||||||
|
%{ for vm in webservers ~}
|
||||||
|
${vm.name} ansible_host=${vm.ip} ansible_user=adminuser ansible_ssh_private_key_file=${vm.private_key_file}
|
||||||
|
%{ endfor ~}
|
||||||
|
|
||||||
|
[databaseservers]
|
||||||
|
%{ for vm in databaseservers ~}
|
||||||
|
${vm.name} ansible_host=${vm.ip} ansible_user=adminuser ansible_ssh_private_key_file=${vm.private_key_file}
|
||||||
|
%{ endfor ~}
|
183
opdracht-1/main.tf
Normal file
183
opdracht-1/main.tf
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
variable "azure_private_key_path" {
|
||||||
|
default = "/home/student/.ssh/azure"
|
||||||
|
}
|
||||||
|
|
||||||
|
data "local_file" "azure_private_key" {
|
||||||
|
filename = var.azure_private_key_path
|
||||||
|
}
|
||||||
|
|
||||||
|
# Azure
|
||||||
|
|
||||||
|
resource "azurerm_virtual_network" "main" {
|
||||||
|
name = "${var.prefix}-network"
|
||||||
|
address_space = ["10.0.0.0/16"]
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_subnet" "main" {
|
||||||
|
name = "internal"
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
virtual_network_name = azurerm_virtual_network.main.name
|
||||||
|
address_prefixes = ["10.0.2.0/24"]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_network_security_group" "main" {
|
||||||
|
name = "${var.prefix}-nsg"
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
security_rule {
|
||||||
|
access = "Allow"
|
||||||
|
direction = "Inbound"
|
||||||
|
name = "tls"
|
||||||
|
priority = 100
|
||||||
|
protocol = "Tcp"
|
||||||
|
source_port_range = "*"
|
||||||
|
source_address_prefix = "*"
|
||||||
|
destination_port_range = "22"
|
||||||
|
destination_address_prefix = "*"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_subnet_network_security_group_association" "nsg_link" {
|
||||||
|
subnet_id = azurerm_subnet.main.id
|
||||||
|
network_security_group_id = azurerm_network_security_group.main.id
|
||||||
|
}
|
||||||
|
|
||||||
|
#webserver 1
|
||||||
|
resource "azurerm_public_ip" "webserver" {
|
||||||
|
name = "${var.prefix}-webserver-pip"
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
location = var.azure_location
|
||||||
|
allocation_method = "Static"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_network_interface" "webserver" {
|
||||||
|
name = "${var.prefix}-webserver-nic"
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
|
||||||
|
ip_configuration {
|
||||||
|
name = "internal"
|
||||||
|
subnet_id = azurerm_subnet.main.id
|
||||||
|
private_ip_address_allocation = "Dynamic"
|
||||||
|
public_ip_address_id = azurerm_public_ip.webserver.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_linux_virtual_machine" "webserver" {
|
||||||
|
name = "${var.prefix}-webserver-vm"
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
network_interface_ids = [
|
||||||
|
azurerm_network_interface.webserver.id,
|
||||||
|
]
|
||||||
|
size = "Standard_B2ats_v2"
|
||||||
|
|
||||||
|
admin_username = "adminuser"
|
||||||
|
admin_ssh_key {
|
||||||
|
username = "adminuser"
|
||||||
|
public_key = data.azurerm_ssh_public_key.azure.public_key
|
||||||
|
}
|
||||||
|
|
||||||
|
os_disk {
|
||||||
|
caching = "ReadWrite"
|
||||||
|
storage_account_type = "Standard_LRS"
|
||||||
|
}
|
||||||
|
|
||||||
|
source_image_reference {
|
||||||
|
publisher = "Canonical"
|
||||||
|
offer = "ubuntu-24_04-lts"
|
||||||
|
sku = "server"
|
||||||
|
version = "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#databaseserver
|
||||||
|
resource "azurerm_public_ip" "databaseserver" {
|
||||||
|
name = "${var.prefix}-databaseserver-pip"
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
location = var.azure_location
|
||||||
|
allocation_method = "Static"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_network_interface" "databaseserver" {
|
||||||
|
name = "${var.prefix}-databaseserver-nic"
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
|
||||||
|
ip_configuration {
|
||||||
|
name = "internal"
|
||||||
|
subnet_id = azurerm_subnet.main.id
|
||||||
|
private_ip_address_allocation = "Dynamic"
|
||||||
|
public_ip_address_id = azurerm_public_ip.databaseserver.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "azurerm_linux_virtual_machine" "databaseserver" {
|
||||||
|
name = "${var.prefix}-databaseserver-vm"
|
||||||
|
location = var.azure_location
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
network_interface_ids = [
|
||||||
|
azurerm_network_interface.databaseserver.id,
|
||||||
|
]
|
||||||
|
size = "Standard_B2ats_v2"
|
||||||
|
|
||||||
|
admin_username = "adminuser"
|
||||||
|
admin_ssh_key {
|
||||||
|
username = "adminuser"
|
||||||
|
public_key = data.azurerm_ssh_public_key.azure.public_key
|
||||||
|
}
|
||||||
|
|
||||||
|
os_disk {
|
||||||
|
caching = "ReadWrite"
|
||||||
|
storage_account_type = "Standard_LRS"
|
||||||
|
}
|
||||||
|
|
||||||
|
source_image_reference {
|
||||||
|
publisher = "Canonical"
|
||||||
|
offer = "ubuntu-24_04-lts"
|
||||||
|
sku = "server"
|
||||||
|
version = "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
locals {
|
||||||
|
webservers = [
|
||||||
|
{
|
||||||
|
name = azurerm_linux_virtual_machine.webserver.name
|
||||||
|
ip = azurerm_linux_virtual_machine.webserver.public_ip_address
|
||||||
|
private_key_file = var.azure_private_key_path
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
databaseservers = [
|
||||||
|
{
|
||||||
|
name = azurerm_linux_virtual_machine.databaseserver.name
|
||||||
|
ip = azurerm_linux_virtual_machine.databaseserver.public_ip_address
|
||||||
|
private_key_file = var.azure_private_key_path
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
locals {
|
||||||
|
inventory = templatefile("${path.module}/ansible-inventory.tmpl", {
|
||||||
|
webservers = local.webservers
|
||||||
|
databaseservers = local.databaseservers
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
resource "local_file" "ansible_inventory" {
|
||||||
|
content = local.inventory
|
||||||
|
filename = "${path.module}/inventory.ini"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
output "ip_addresses" {
|
||||||
|
value = local_file.ansible_inventory.content
|
||||||
|
}
|
15
opdracht-1/providers.tf
Normal file
15
opdracht-1/providers.tf
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
azurerm = {
|
||||||
|
source = "hashicorp/azurerm"
|
||||||
|
version = "~> 4.27.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
provider "azurerm" {
|
||||||
|
resource_provider_registrations = "none"
|
||||||
|
subscription_id = var.azure_subscriptionid
|
||||||
|
features {}
|
||||||
|
}
|
29
opdracht-1/variables.tf
Normal file
29
opdracht-1/variables.tf
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
variable "azure_subscriptionid" {
|
||||||
|
description = "ID of the Azure Subscription."
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "azure_resourcegroup" {
|
||||||
|
description = "Name of the Azure Resource Group."
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "azure_location" {
|
||||||
|
description = "Location of the Azure resources"
|
||||||
|
default = "westeurope"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "azure_user" {
|
||||||
|
description = "User that will gain access to the Azure vm's"
|
||||||
|
default = "iac"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "prefix" {
|
||||||
|
description = "The Prefix used for all resources"
|
||||||
|
default = "week-4-opdracht-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Pull the SSH public key from Azure Key Vault
|
||||||
|
data "azurerm_ssh_public_key" "azure" {
|
||||||
|
name = "azure"
|
||||||
|
resource_group_name = var.azure_resourcegroup
|
||||||
|
}
|
Reference in New Issue
Block a user