almost working...
This commit is contained in:
135
main.tf
135
main.tf
@@ -12,12 +12,26 @@ resource "libvirt_pool" "tf_pool" {
|
||||
resource "libvirt_volume" "ubuntu-qcow2" {
|
||||
name = "ubuntu-qcow2"
|
||||
pool = libvirt_pool.tf_pool.name
|
||||
source = var.ubuntu_18_img_url
|
||||
#source = var.ubuntu_18_img_url
|
||||
source = var.ubuntu_20_img_url
|
||||
format = "qcow2"
|
||||
#size = var.vm_disk_size # not allowed if source is specified
|
||||
}
|
||||
|
||||
# Create a 5GB root volume
|
||||
#resource "libvirt_volume" "rootfs" {
|
||||
# name = "rootfs"
|
||||
# pool = libvirt_pool.tf_pool.name
|
||||
# #base_volume_id = "..."
|
||||
# size = "5120"
|
||||
#}
|
||||
|
||||
|
||||
data "template_file" "user_data" {
|
||||
template = file("${path.module}/config/cloud_init.yml")
|
||||
vars = {
|
||||
vm_hostname = var.vm_hostname
|
||||
}
|
||||
}
|
||||
|
||||
data "template_file" "network_config" {
|
||||
@@ -33,16 +47,17 @@ resource "libvirt_cloudinit_disk" "commoninit" {
|
||||
|
||||
resource "libvirt_domain" "domain-ubuntu" {
|
||||
qemu_agent = true
|
||||
name = var.vm_hostname
|
||||
memory = "512"
|
||||
vcpu = 1
|
||||
name = var.vm_name
|
||||
memory = "4096"
|
||||
vcpu = 2
|
||||
|
||||
cloudinit = libvirt_cloudinit_disk.commoninit.id
|
||||
|
||||
network_interface {
|
||||
network_name = "host-bridge"
|
||||
wait_for_lease = true
|
||||
hostname = var.vm_hostname
|
||||
#network_name = "host-bridge"
|
||||
bridge = "br0"
|
||||
#wait_for_lease = true
|
||||
hostname = var.vm_name
|
||||
}
|
||||
|
||||
console {
|
||||
@@ -80,17 +95,111 @@ resource "libvirt_domain" "domain-ubuntu" {
|
||||
#bastion_host = "my-jump-host."
|
||||
#bastion_user = "deploys"
|
||||
#bastion_private_key = file("~/.ssh/deploys")
|
||||
timeout = "2m"
|
||||
timeout = "4m"
|
||||
}
|
||||
}
|
||||
|
||||
provisioner "local-exec" {
|
||||
command = <<EOT
|
||||
echo "[nginx]" > nginx.ini
|
||||
echo "${libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]}" >> nginx.ini
|
||||
echo "[nginx:vars]" >> nginx.ini
|
||||
echo "ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -q hqt\"'" >> nginx.ini
|
||||
ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i nginx.ini ansible/playbook.yml
|
||||
echo "[k8s_master]" > inventory.ini
|
||||
echo "${libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]} node_ip=${libvirt_domain.domain-ubuntu.network_interface[0].addresses[0]} vm_hostname=${var.vm_hostname}" >> inventory.ini
|
||||
echo "[k8s_master:vars]" >> inventory.ini
|
||||
echo "ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -q hqt\"'" >> inventory.ini
|
||||
echo "[k8s_slaves:vars]" >> inventory.ini
|
||||
echo "ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh -W %h:%p -q hqt\"'" >> inventory.ini
|
||||
echo "[k8s_slaves]" >> inventory.ini
|
||||
#ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i nginx.ini ansible/playbook.yml
|
||||
ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i inventory.ini ansible/k8s-master-playbook.yml
|
||||
EOT
|
||||
}
|
||||
}
|
||||
|
||||
resource "libvirt_volume" "k8sslaves-qcow2" {
|
||||
count = var.slaves
|
||||
name = "k8sslaves-${count.index}.qcow2"
|
||||
pool = libvirt_pool.tf_pool.name
|
||||
#source = "${path.module}/sources/${var.distros[count.index]}.qcow2"
|
||||
source = var.ubuntu_20_img_url
|
||||
format = "qcow2"
|
||||
}
|
||||
|
||||
data "template_file" "slaves_user_data" {
|
||||
count = var.slaves
|
||||
template = file("${path.module}/config/cloud_init.yml")
|
||||
vars = {
|
||||
vm_hostname = "${var.vm_slave_hostname}${count.index}"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
resource "libvirt_cloudinit_disk" "slaves_commoninit" {
|
||||
count = var.slaves
|
||||
name = "slaves-commoninit-${count.index}.iso"
|
||||
user_data = data.template_file.slaves_user_data[count.index].rendered
|
||||
network_config = data.template_file.network_config.rendered
|
||||
pool = libvirt_pool.tf_pool.name
|
||||
}
|
||||
|
||||
resource "libvirt_domain" "domain-k8s-slave" {
|
||||
count = var.slaves
|
||||
qemu_agent = true
|
||||
name = "${var.vm_slave_name}-${count.index}"
|
||||
memory = "4096"
|
||||
vcpu = 2
|
||||
|
||||
cloudinit = libvirt_cloudinit_disk.slaves_commoninit[count.index].id
|
||||
|
||||
network_interface {
|
||||
#network_name = "host-bridge"
|
||||
bridge = "br0"
|
||||
#wait_for_lease = true
|
||||
hostname = "${var.vm_slave_name}-${count.index}"
|
||||
#hostname = var.vm_slave_name
|
||||
}
|
||||
|
||||
console {
|
||||
type = "pty"
|
||||
target_port = "0"
|
||||
target_type = "serial"
|
||||
}
|
||||
|
||||
console {
|
||||
type = "pty"
|
||||
target_type = "virtio"
|
||||
target_port = "1"
|
||||
}
|
||||
|
||||
disk {
|
||||
volume_id = element(libvirt_volume.k8sslaves-qcow2.*.id, count.index)
|
||||
}
|
||||
|
||||
graphics {
|
||||
type = "spice"
|
||||
listen_type = "address"
|
||||
autoport = true
|
||||
}
|
||||
|
||||
provisioner "remote-exec" {
|
||||
inline = [
|
||||
"echo 'Hello World'"
|
||||
]
|
||||
|
||||
connection {
|
||||
type = "ssh"
|
||||
user = var.ssh_username
|
||||
host = self.network_interface[0].addresses[0]
|
||||
private_key = file(var.ssh_private_key)
|
||||
#bastion_host = "my-jump-host."
|
||||
#bastion_user = "deploys"
|
||||
#bastion_private_key = file("~/.ssh/deploys")
|
||||
timeout = "4m"
|
||||
}
|
||||
}
|
||||
|
||||
provisioner "local-exec" {
|
||||
command = <<EOT
|
||||
echo "${self.network_interface[0].addresses[0]} node_ip=${self.network_interface[0].addresses[0]}" >> inventory.ini
|
||||
ansible-playbook -u ${var.ssh_username} --private-key ${var.ssh_private_key} -i inventory.ini ansible/k8s-slave-playbook.yml
|
||||
EOT
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user