diff --git a/inventory.tf b/inventory.tf index 5d40761..5facec8 100644 --- a/inventory.tf +++ b/inventory.tf @@ -1,3 +1,8 @@ +resource "random_password" "demo_pass" { + length = 16 + special = true +} + resource "local_file" "ansible_inventory" { filename = "${path.module}/inventory.ini" file_permission = "0644" @@ -6,12 +11,18 @@ resource "local_file" "ansible_inventory" { ansible_ssh_extra_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' private_ip_caddy='${gridscale_server.server_caddy.network[1].auto_assigned_ip}' private_ip_monitoring='${gridscale_server.server_monitoring.network[1].auto_assigned_ip}' + private_ip_nginx='${gridscale_server.server_nginx.network[1].auto_assigned_ip}' + demo_pass="${random_password.demo_pass.result}" + demo_user="demo" [caddy] - ${var.caddy_hostname} ansible_host=${gridscale_ipv4.public_ipv4.ip} ansible_user=root + ${var.caddy_hostname} ansible_host=${gridscale_ipv4.public_ipv4_caddy.ip} ansible_user=root [monitoring] ${var.monitoring_hostname} ansible_host=${gridscale_ipv4.public_ipv4_monitoring.ip} ansible_user=root + + [nginx] + ${var.nginx_hostname} ansible_host=${gridscale_ipv4.public_ipv4_nginx.ip} ansible_user=root EOT } @@ -19,7 +30,8 @@ resource "null_resource" "ansible_provisioner" { depends_on = [ local_file.ansible_inventory, gridscale_server.server_caddy, - gridscale_server.server_monitoring + gridscale_server.server_monitoring, + gridscale_server.server_nginx ] provisioner "local-exec" { diff --git a/outputs.tf b/outputs.tf index 77926cf..bc3cdb8 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,6 +1,6 @@ output "caddy_public_ip" { description = "Public IPv4 address of the Caddy server" - value = gridscale_ipv4.public_ipv4.ip + value = gridscale_ipv4.public_ipv4_caddy.ip } output "monitoring_public_ip" { @@ -8,6 +8,11 @@ output "monitoring_public_ip" { value = gridscale_ipv4.public_ipv4_monitoring.ip } +output "nginx_public_ip" { + description = "Public IPv4 address of the Nginx server" + value = gridscale_ipv4.public_ipv4_nginx.ip +} + output "caddy_private_ip" { description = "Private IPv4 address of the Caddy server" value = gridscale_server.server_caddy.network[1].auto_assigned_ip @@ -18,7 +23,18 @@ output "monitoring_private_ip" { value = gridscale_server.server_monitoring.network[1].auto_assigned_ip } -output "ansible_inventory" { - description = "Generated Ansible inventory content" - value = local_file.ansible_inventory.content +output "nginx_private_ip" { + description = "Private IPv4 address of the Nginx server" + value = gridscale_server.server_nginx.network[1].auto_assigned_ip } + + +output "website_urls" { + description = "Website URLs" + value = [ + "Caddy: https://caddy.${replace(gridscale_ipv4.public_ipv4_caddy.ip, ".", "-")}.sslip.io", + "Nginx behind Caddy: https://nginx.${replace(gridscale_ipv4.public_ipv4_caddy.ip, ".", "-")}.sslip.io", + "Prometheus behind Caddy: https://monitoring.${replace(gridscale_ipv4.public_ipv4_caddy.ip, ".", "-")}.sslip.io", + "Login for Monitoring: demo ${nonsensitive(random_password.demo_pass.result)}" + ] +} \ No newline at end of file diff --git a/playbook.yml b/playbook.yml index 8f1f7da..890d2f3 100644 --- a/playbook.yml +++ b/playbook.yml @@ -114,4 +114,23 @@ ansible.builtin.template: src: prometheus.yml.j2 dest: /etc/prometheus/prometheus.yml - mode: "0644" \ No newline at end of file + mode: "0644" + +- name: Configure Nginx Server + hosts: nginx + become: true + tags: + - nginx + + tasks: + - name: Install Nginx + ansible.builtin.apt: + pkg: + - nginx + state: present + + - name: Create Hello World page + ansible.builtin.template: + src: index.html.j2 + dest: /var/www/html/index.html + mode: "0644" diff --git a/resources.tf b/resources.tf index 7e793f0..7055eac 100644 --- a/resources.tf +++ b/resources.tf @@ -7,12 +7,12 @@ resource "gridscale_network" "network_internal"{ dhcp_reserved_subnet = ["192.168.121.0/31"] } -resource "gridscale_ipv4" "public_ipv4" { - name = "public_ipv4" +resource "gridscale_ipv4" "public_ipv4_caddy" { + name = "public_ipv4_caddy" } -resource "gridscale_ipv6" "public_ipv6" { - name = "public_ipv6" +resource "gridscale_ipv6" "public_ipv6_caddy" { + name = "public_ipv6_caddy" } resource "gridscale_ipv4" "public_ipv4_monitoring" { @@ -23,6 +23,14 @@ resource "gridscale_ipv6" "public_ipv6_monitoring" { name = "public_ipv6_monitoring" } +resource "gridscale_ipv4" "public_ipv4_nginx" { + name = "public_ipv4_nginx" +} + +resource "gridscale_ipv6" "public_ipv6_nginx" { + name = "public_ipv6_nginx" +} + data "gridscale_template" "template_debian_13" { name = "Debian 13" } @@ -68,6 +76,13 @@ resource "gridscale_server" "server_caddy" { dst_port = 80 comment = "Allow HTTP access" } + rules_v4_in { + order = 15 + protocol = "tcp" + action = "accept" + dst_port = 443 + comment = "Allow HTTPS access" + } rules_v6_in { order = 0 protocol = "tcp" @@ -82,6 +97,13 @@ resource "gridscale_server" "server_caddy" { dst_port = 80 comment = "Allow HTTPv6 access" } + rules_v6_in { + order = 15 + protocol = "tcp" + action = "accept" + dst_port = 443 + comment = "Allow HTTPSv6 access" + } } network { # Private network @@ -95,8 +117,8 @@ resource "gridscale_server" "server_caddy" { src_cidr = "192.168.121.0/27" } } - ipv4 = gridscale_ipv4.public_ipv4.id - ipv6 = gridscale_ipv6.public_ipv6.id + ipv4 = gridscale_ipv4.public_ipv4_caddy.id + ipv6 = gridscale_ipv6.public_ipv6_caddy.id timeouts { create = "10m" } @@ -131,13 +153,6 @@ resource "gridscale_server" "server_monitoring" { dst_port = 22 comment = "Allow SSH access" } - rules_v4_in { - order = 10 - protocol = "tcp" - action = "accept" - dst_port = 80 - comment = "Allow HTTP access" - } rules_v6_in { order = 0 protocol = "tcp" @@ -145,13 +160,6 @@ resource "gridscale_server" "server_monitoring" { dst_port = 22 comment = "Allow SSHv6 access" } - rules_v6_in { - order = 10 - protocol = "tcp" - action = "accept" - dst_port = 80 - comment = "Allow HTTPv6 access" - } } network { # Private Network @@ -162,4 +170,60 @@ resource "gridscale_server" "server_monitoring" { timeouts { create = "10m" } +} + + +resource "gridscale_storage" "storage_nginx" { + name = "storage_nginx" + storage_type = "storage" + capacity = 20 + template { + sshkeys = [gridscale_sshkey.sshkey_richard.id] + template_uuid = data.gridscale_template.template_debian_13.id + hostname = var.nginx_hostname + } +} + +resource "gridscale_server" "server_nginx" { + name = var.nginx_hostname + cores = 2 + memory = 1 + power = true + storage { + object_uuid = gridscale_storage.storage_nginx.id + } + network { + # Public Network + object_uuid = "5557a73b-31ee-4b1f-aa15-7789ad6ae04c" + rules_v4_in { + order = 0 + protocol = "tcp" + action = "accept" + dst_port = 22 + comment = "Allow SSH access" + } + rules_v6_in { + order = 0 + protocol = "tcp" + action = "accept" + dst_port = 22 + comment = "Allow SSHv6 access" + } + } + network { + # Private Network + object_uuid = gridscale_network.network_internal.id + rules_v4_in { + order = 10 + protocol = "tcp" + action = "accept" + dst_port = 80 + comment = "Allow HTTP access for Loadbalancer" + } + } + ipv4 = gridscale_ipv4.public_ipv4_nginx.id + ipv6 = gridscale_ipv6.public_ipv6_nginx.id + timeouts { + create = "10m" + } } \ No newline at end of file diff --git a/variables.tf b/variables.tf index de97e23..d4df117 100644 --- a/variables.tf +++ b/variables.tf @@ -19,4 +19,10 @@ variable "monitoring_hostname" { type = string description = "Monitoring hostname" default = "monitoring01" +} + +variable "nginx_hostname" { + type = string + description = "Nginx hostname" + default = "nginx01" } \ No newline at end of file