diff options
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | README.md | 25 | ||||
| -rw-r--r-- | cloud_init.cfg | 24 | ||||
| -rw-r--r-- | libvirt.tf | 53 | ||||
| -rw-r--r-- | main.tf | 13 |
5 files changed, 121 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..013e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +terraform.tfstate +terraform.tfstate.backup +.terraform/ +.terraform.lock.hcl +.terraform.tfstate.lock.info + diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b09d89 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# README - KVM-Terraform + +**Pour que le script Terraform puisse pousser son image, il faut décommenter dans /etc/libvirt/quemu.conf** + + #security_driver = "selinux" + security_driver = "none" + + systemctl restart libvirtd + +Après le déploiement de la machine virtuelle, les journaux sont dans : + + /var/log/cloud-init.log + +Et vos configurations de cloud_init.cfg se retrouvent dans : + + /var/lib/cloud/instances/nocloud/ + +N.B. Si à la fin de terraform apply, vous avez l'erreur : +Error: Invalid index +│ +│ on libvirt.tf line 53, in output "ip": + +C'est que l'adresse ip n'est pas encore connue. Relancez terraform apply ! + + diff --git a/cloud_init.cfg b/cloud_init.cfg new file mode 100644 index 0000000..4927615 --- /dev/null +++ b/cloud_init.cfg @@ -0,0 +1,24 @@ +#cloud-config + +users: + - name: jerome + ssh_authorized_keys: + - ssh-rsa AAAAB3xxx...x8= jerome@localhost + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + shell: /bin/bash + groups: wheel + +runcmd: + - cp /etc/skel/.bash* /home/jerome/ + - chown jerome:jerome /home/jerome/.bash* + - hostnamectl set-hostname centos + + +write_files: + - path: /home/jerome/helloworld + content: | + #!/bin/bash + echo "Hello World !" + permissions: 0755 + + diff --git a/libvirt.tf b/libvirt.tf new file mode 100644 index 0000000..56ab930 --- /dev/null +++ b/libvirt.tf @@ -0,0 +1,53 @@ +# Defining VM Volume +resource "libvirt_volume" "rocky8-qcow2" { + name = "rocky8.qcow2" + pool = "default" + source = "https://download.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2" + format = "qcow2" +} + +# get user data info +data "template_file" "user_data" { + template = file("${path.module}/cloud_init.cfg") +} + +# Use CloudInit to add the instance +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + pool = "default" # List storage pools using virsh pool-list + user_data = data.template_file.user_data.rendered +} + +# Define KVM domain to create +resource "libvirt_domain" "rocky8" { + name = "rocky8" + memory = "2048" + vcpu = 2 + + network_interface { + network_name = "default" + } + + disk { + volume_id = libvirt_volume.rocky8-qcow2.id + } + + cloudinit = libvirt_cloudinit_disk.commoninit.id + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +# Output Server IP +output "ip" { + value = libvirt_domain.rocky8.network_interface.0.addresses.0 +} @@ -0,0 +1,13 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + } + } +} + +provider "libvirt" { + ## Configuration options + uri = "qemu:///system" + #uri = "qemu+ssh://root@adresse_ip/system" +} |
