Setting passwords and keys working via variables.

- Setting hostnames is working also.
ansible-test
shnee 4 years ago
parent af17823d68
commit 474f6c746a

@ -10,17 +10,21 @@ users:
shell: /usr/bin/bash shell: /usr/bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys: 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 - name: root
ssh_authorized_keys: 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 ssh_pwauth: true
disable_root: false disable_root: false
chpasswd: chpasswd:
list: list:
- root:${admin-passwd}
- admin:${admin-passwd}
expire: false expire: false
hostname: ${hostname}
# Use this when it's determined that we need a bigger disk image. # Use this when it's determined that we need a bigger disk image.
# This must be used in conjuction with 'size' in 'libvirt_volume' # This must be used in conjuction with 'size' in 'libvirt_volume'
# growpart: # growpart:

@ -35,31 +35,55 @@ resource "libvirt_volume" "worker-volumes" {
count = var.worker-nodes count = var.worker-nodes
} }
data "template_file" "user_data" { data "template_file" "master-user-data" {
template = file("${path.module}/cloud_init.cfg") 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" { data "template_file" "network_config" {
template = file("${path.module}/network_config.cfg") template = file("${path.module}/network_config.cfg")
} }
resource "libvirt_cloudinit_disk" "commoninit" { resource "libvirt_cloudinit_disk" "master-init" {
name = "commoninit.images" name = "k8s-tf-master-init"
user_data = data.template_file.user_data.rendered user_data = data.template_file.master-user-data.rendered
network_config = data.template_file.network_config.rendered network_config = data.template_file.network_config.rendered
pool = libvirt_pool.images.name 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 # Create the machine
resource "libvirt_domain" "master-domain" { resource "libvirt_domain" "master-domain" {
name = "k8s-tf-master" name = "k8s-tf-master"
memory = var.node-memory memory = var.node-memory
vcpu = var.node-vcpus vcpu = var.node-vcpus
cloudinit = libvirt_cloudinit_disk.commoninit.id cloudinit = libvirt_cloudinit_disk.master-init.id
network_interface { network_interface {
network_name = "default" network_name = "default"
hostname = "k8s-tf-master"
} }
# IMPORTANT: this is a known bug on cloud images, since they expect a console # 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 memory = var.node-memory
vcpu = var.node-vcpus vcpu = var.node-vcpus
cloudinit = libvirt_cloudinit_disk.commoninit.id cloudinit = element(libvirt_cloudinit_disk.worker-init.*.id, count.index)
network_interface { network_interface {
network_name = "default" network_name = "default"
hostname = "k8s-tf-worker-${count.index}"
} }
# IMPORTANT: this is a known bug on cloud images, since they expect a console # IMPORTANT: this is a known bug on cloud images, since they expect a console

@ -15,6 +15,14 @@ variable "node-vcpus" {
type = number 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" { variable "worker-nodes" {
default = "2" default = "2"
description = "The number of worker nodes to create." description = "The number of worker nodes to create."

Loading…
Cancel
Save