diff --git a/cloud_init.cfg b/cloud_init.cfg index f3ff57c..1023821 100644 --- a/cloud_init.cfg +++ b/cloud_init.cfg @@ -10,17 +10,21 @@ users: shell: /usr/bin/bash sudo: ALL=(ALL) NOPASSWD:ALL ssh_authorized_keys: - - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDfDcjMFmWd6qy9KIlnIHNbEfeNLHC885UUH3jGwESmMTpFfPUn01t9hq5GGaFDrBR55VgdKebAv2JSVl209+r3tE5XxUX5/s2Pu3o2283PiZhA+D18skL7fzaolygOY8mxi9CZSDFia//lLbqT/OE45VGahVBRtda4gmjrade0XRKqjJUCkIo6huG9Ub6yP4gFtFU/C1rRvQo0hqT/imsMYU0Q5XzrKVWv3CpzA7EIQq8llU0fRGMuXWYYOXznPeqqf5BTbWhMWUXVS0o7Cz+zvbxwq1dOR1qHbJ8Vrkt30Cz5QEd159dIM3LHCtOHnveeOpkFo0RqkhQdpZM+2cKzESvivGNGP9h+PrSjcveADxVwDHcxguumUyM012M3yR8cK9KY+GqW5jPdAs13yXGTG4OWiQKeKEgX910l/FndhQi0tSpSEhIlfcEpa3k3P8RrhKJbwiRgR7Qvus4R/KU+lxKOiOr4RKyPQJobC0i0/bvqkw+UHWp4U0Hqivjsb6k= admin + - ${admin-pub-key} - name: root ssh_authorized_keys: - - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDfDcjMFmWd6qy9KIlnIHNbEfeNLHC885UUH3jGwESmMTpFfPUn01t9hq5GGaFDrBR55VgdKebAv2JSVl209+r3tE5XxUX5/s2Pu3o2283PiZhA+D18skL7fzaolygOY8mxi9CZSDFia//lLbqT/OE45VGahVBRtda4gmjrade0XRKqjJUCkIo6huG9Ub6yP4gFtFU/C1rRvQo0hqT/imsMYU0Q5XzrKVWv3CpzA7EIQq8llU0fRGMuXWYYOXznPeqqf5BTbWhMWUXVS0o7Cz+zvbxwq1dOR1qHbJ8Vrkt30Cz5QEd159dIM3LHCtOHnveeOpkFo0RqkhQdpZM+2cKzESvivGNGP9h+PrSjcveADxVwDHcxguumUyM012M3yR8cK9KY+GqW5jPdAs13yXGTG4OWiQKeKEgX910l/FndhQi0tSpSEhIlfcEpa3k3P8RrhKJbwiRgR7Qvus4R/KU+lxKOiOr4RKyPQJobC0i0/bvqkw+UHWp4U0Hqivjsb6k= admin + - ${admin-pub-key} ssh_pwauth: true disable_root: false chpasswd: list: + - root:${admin-passwd} + - admin:${admin-passwd} expire: false +hostname: ${hostname} + # Use this when it's determined that we need a bigger disk image. # This must be used in conjuction with 'size' in 'libvirt_volume' # growpart: diff --git a/main.tf b/main.tf index 295bcea..3c1f9e5 100644 --- a/main.tf +++ b/main.tf @@ -35,31 +35,55 @@ resource "libvirt_volume" "worker-volumes" { count = var.worker-nodes } -data "template_file" "user_data" { +data "template_file" "master-user-data" { template = file("${path.module}/cloud_init.cfg") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "k8s-tf-master" + } +} + +data "template_file" "worker-user-data" { + template = file("${path.module}/cloud_init.cfg") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "k8s-tf-worker-${count.index}" + } + count = var.worker-nodes } data "template_file" "network_config" { template = file("${path.module}/network_config.cfg") } -resource "libvirt_cloudinit_disk" "commoninit" { - name = "commoninit.images" - user_data = data.template_file.user_data.rendered +resource "libvirt_cloudinit_disk" "master-init" { + name = "k8s-tf-master-init" + user_data = data.template_file.master-user-data.rendered network_config = data.template_file.network_config.rendered pool = libvirt_pool.images.name } +resource "libvirt_cloudinit_disk" "worker-init" { + name = "k8s-tf-worker-${count.index}-init" + user_data = element(data.template_file.worker-user-data.*.rendered, count.index) + network_config = data.template_file.network_config.rendered + pool = libvirt_pool.images.name + count = var.worker-nodes +} + # Create the machine resource "libvirt_domain" "master-domain" { name = "k8s-tf-master" memory = var.node-memory vcpu = var.node-vcpus - cloudinit = libvirt_cloudinit_disk.commoninit.id + cloudinit = libvirt_cloudinit_disk.master-init.id network_interface { network_name = "default" + hostname = "k8s-tf-master" } # IMPORTANT: this is a known bug on cloud images, since they expect a console @@ -94,10 +118,11 @@ resource "libvirt_domain" "worker-domains" { memory = var.node-memory vcpu = var.node-vcpus - cloudinit = libvirt_cloudinit_disk.commoninit.id + cloudinit = element(libvirt_cloudinit_disk.worker-init.*.id, count.index) network_interface { network_name = "default" + hostname = "k8s-tf-worker-${count.index}" } # IMPORTANT: this is a known bug on cloud images, since they expect a console diff --git a/variables.tf b/variables.tf index acece76..95b161e 100644 --- a/variables.tf +++ b/variables.tf @@ -15,6 +15,14 @@ variable "node-vcpus" { type = number } +variable "root-admin-passwd" { + description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module." +} + +variable "root-admin-pub-key" { + description = "The public key to be added to authorized_keys for the root and admin accounts." +} + variable "worker-nodes" { default = "2" description = "The number of worker nodes to create."