terraform_kvm
Differences
This shows you the differences between two versions of the page.
| terraform_kvm [2025/07/26 17:09] – - Imported by DokuWiki Advanced Plugin wikiadm | terraform_kvm [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Terraform libvirt ====== | ||
| - | <file bash> | ||
| - | $ mkdir -p ~/ | ||
| - | $ cd ~/ | ||
| - | </ | ||
| - | <file bash> | ||
| - | $ sudo usermod -aG kvm,libvirt $USER | ||
| - | $ sudo echo ' | ||
| - | $ sudo systemctl restart libvirtd | ||
| - | </ | ||
| - | |||
| - | ===== Provider ===== | ||
| - | <file tf main.tf> | ||
| - | terraform { | ||
| - | required_providers{ | ||
| - | libvirt = { | ||
| - | source = " | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | provider " | ||
| - | uri = " | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | | ||
| - | |||
| - | Initializing the backend... | ||
| - | |||
| - | Initializing provider plugins... | ||
| - | - Finding latest version of dmacvicar/ | ||
| - | - Installing dmacvicar/ | ||
| - | - Installed dmacvicar/ | ||
| - | |||
| - | Partner and community providers are signed by their developers. | ||
| - | If you'd like to know more about provider signing, you can read about it here: | ||
| - | https:// | ||
| - | |||
| - | Terraform has created a lock file .terraform.lock.hcl to record the provider | ||
| - | selections it made above. Include this file in your version control repository | ||
| - | so that Terraform can guarantee to make the same selections by default when | ||
| - | you run " | ||
| - | |||
| - | Terraform has been successfully initialized! | ||
| - | |||
| - | You may now begin working with Terraform. Try running " | ||
| - | any changes that are required for your infrastructure. All Terraform commands | ||
| - | should now work. | ||
| - | |||
| - | If you ever set or change modules or backend configuration for Terraform, | ||
| - | rerun this command to reinitialize your working directory. If you forget, other | ||
| - | commands will detect it and remind you to do so if necessary. | ||
| - | </ | ||
| - | |||
| - | ===== Cloudinit ===== | ||
| - | <file bash> | ||
| - | $ cat cloud_init.cfg | ||
| - | # | ||
| - | ssh_pwauth: True | ||
| - | chpasswd: | ||
| - | list: | | ||
| - | root:centos | ||
| - | expire: False | ||
| - | </ | ||
| - | |||
| - | ===== Definição da VM ===== | ||
| - | <file tf centos.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | pool = " | ||
| - | source = "/ | ||
| - | format = " | ||
| - | } | ||
| - | |||
| - | resource " | ||
| - | name = " | ||
| - | memory = " | ||
| - | vcpu = 2 | ||
| - | |||
| - | cpu { | ||
| - | mode = " | ||
| - | } | ||
| - | |||
| - | network_interface { | ||
| - | network_name = " | ||
| - | } | ||
| - | |||
| - | disk { | ||
| - | volume_id = libvirt_volume.rhel.id | ||
| - | } | ||
| - | | ||
| - | cloudinit = libvirt_cloudinit_disk.commoninit.id | ||
| - | |||
| - | } | ||
| - | |||
| - | data " | ||
| - | template = file(" | ||
| - | } | ||
| - | |||
| - | resource " | ||
| - | name = " | ||
| - | pool = " | ||
| - | user_data = data.template_file.user_data.rendered | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | FIXME para criar a ISO do cloudinit e necessário ter o pacote '' | ||
| - | |||
| - | <file bash> | ||
| - | $ sudo apt install mkisofs | ||
| - | </ | ||
| - | |||
| - | FIXME ao inserir as informações do cloudinit e necessário rodar o comando '' | ||
| - | |||
| - | <file bash> | ||
| - | $ terraform init | ||
| - | |||
| - | Initializing the backend... | ||
| - | |||
| - | Initializing provider plugins... | ||
| - | - Reusing previous version of dmacvicar/ | ||
| - | - Reusing previous version of hashicorp/ | ||
| - | - Using previously-installed dmacvicar/ | ||
| - | - Using previously-installed hashicorp/ | ||
| - | |||
| - | Terraform has been successfully initialized! | ||
| - | |||
| - | You may now begin working with Terraform. Try running " | ||
| - | any changes that are required for your infrastructure. All Terraform commands | ||
| - | should now work. | ||
| - | |||
| - | If you ever set or change modules or backend configuration for Terraform, | ||
| - | rerun this command to reinitialize your working directory. If you forget, other | ||
| - | commands will detect it and remind you to do so if necessary. | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ terraform validate | ||
| - | Success! The configuration is valid. | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ terraform plan | ||
| - | data.template_file.user_data: | ||
| - | data.template_file.user_data: | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | + create | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # libvirt_cloudinit_disk.commoninit will be created | ||
| - | + resource " | ||
| - | + id = (known after apply) | ||
| - | + name = " | ||
| - | + pool = " | ||
| - | + user_data = << | ||
| - | # | ||
| - | ssh_pwauth: True | ||
| - | chpasswd: | ||
| - | list: | | ||
| - | root:centos | ||
| - | expire: False | ||
| - | EOT | ||
| - | } | ||
| - | |||
| - | # libvirt_domain.centos will be created | ||
| - | + resource " | ||
| - | + arch = (known after apply) | ||
| - | + autostart | ||
| - | + cloudinit | ||
| - | + emulator | ||
| - | + fw_cfg_name = " | ||
| - | + id = (known after apply) | ||
| - | + machine | ||
| - | + memory | ||
| - | + name = " | ||
| - | + qemu_agent | ||
| - | + running | ||
| - | + vcpu = 2 | ||
| - | |||
| - | + cpu { | ||
| - | + mode = " | ||
| - | } | ||
| - | |||
| - | + disk { | ||
| - | + scsi = false | ||
| - | + volume_id = (known after apply) | ||
| - | } | ||
| - | |||
| - | + network_interface { | ||
| - | + addresses | ||
| - | + hostname | ||
| - | + mac = (known after apply) | ||
| - | + network_id | ||
| - | + network_name = " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | # libvirt_volume.rhel will be created | ||
| - | + resource " | ||
| - | + format = " | ||
| - | + id = (known after apply) | ||
| - | + name = " | ||
| - | + pool = " | ||
| - | + size = (known after apply) | ||
| - | + source = "/ | ||
| - | } | ||
| - | |||
| - | Plan: 3 to add, 0 to change, 0 to destroy. | ||
| - | |||
| - | ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | ||
| - | |||
| - | Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run " | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ terraform apply | ||
| - | data.template_file.user_data: | ||
| - | data.template_file.user_data: | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | + create | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # libvirt_cloudinit_disk.commoninit will be created | ||
| - | + resource " | ||
| - | + id = (known after apply) | ||
| - | + name = " | ||
| - | + pool = " | ||
| - | + user_data = << | ||
| - | # | ||
| - | ssh_pwauth: True | ||
| - | chpasswd: | ||
| - | list: | | ||
| - | root:centos | ||
| - | expire: False | ||
| - | EOT | ||
| - | } | ||
| - | |||
| - | # libvirt_domain.centos will be created | ||
| - | + resource " | ||
| - | + arch = (known after apply) | ||
| - | + autostart | ||
| - | + cloudinit | ||
| - | + emulator | ||
| - | + fw_cfg_name = " | ||
| - | + id = (known after apply) | ||
| - | + machine | ||
| - | + memory | ||
| - | + name = " | ||
| - | + qemu_agent | ||
| - | + running | ||
| - | + vcpu = 2 | ||
| - | |||
| - | + cpu { | ||
| - | + mode = " | ||
| - | } | ||
| - | |||
| - | + disk { | ||
| - | + scsi = false | ||
| - | + volume_id = (known after apply) | ||
| - | } | ||
| - | |||
| - | + network_interface { | ||
| - | + addresses | ||
| - | + hostname | ||
| - | + mac = (known after apply) | ||
| - | + network_id | ||
| - | + network_name = " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | # libvirt_volume.rhel will be created | ||
| - | + resource " | ||
| - | + format = " | ||
| - | + id = (known after apply) | ||
| - | + name = " | ||
| - | + pool = " | ||
| - | + size = (known after apply) | ||
| - | + source = "/ | ||
| - | } | ||
| - | |||
| - | Plan: 3 to add, 0 to change, 0 to destroy. | ||
| - | |||
| - | Do you want to perform these actions? | ||
| - | Terraform will perform the actions described above. | ||
| - | Only ' | ||
| - | |||
| - | Enter a value: yes | ||
| - | |||
| - | libvirt_cloudinit_disk.commoninit: | ||
| - | libvirt_volume.rhel: | ||
| - | libvirt_volume.rhel: | ||
| - | libvirt_cloudinit_disk.commoninit: | ||
| - | libvirt_domain.centos: | ||
| - | libvirt_domain.centos: | ||
| - | |||
| - | Apply complete! Resources: 3 added, 0 changed, 0 destroyed. | ||
| - | </ | ||
| - | |||
| - | ===== Acessando a vm ===== | ||
| - | <file bash> | ||
| - | $ virsh list | ||
| - | | ||
| - | ---------------------------- | ||
| - | | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ virsh domiflist rhel-vm-01 | ||
| - | | ||
| - | ------------------------------------------------------------- | ||
| - | | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ virsh net-dhcp-leases default | ||
| - | | ||
| - | --------------------------------------------------------------------------------------------------------- | ||
| - | | ||
| - | | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ ssh root@192.168.122.171 | ||
| - | The authenticity of host ' | ||
| - | ED25519 key fingerprint is SHA256: | ||
| - | This key is not known by any other names | ||
| - | Are you sure you want to continue connecting (yes/ | ||
| - | Warning: Permanently added ' | ||
| - | root@192.168.122.171' | ||
| - | [root@localhost ~]# | ||
| - | </ | ||
| - | |||
| - | ===== Destruindo a vm ===== | ||
| - | <file bash> | ||
| - | $ terraform destroy | ||
| - | data.template_file.user_data: | ||
| - | data.template_file.user_data: | ||
| - | libvirt_cloudinit_disk.commoninit: | ||
| - | libvirt_volume.rhel: | ||
| - | libvirt_domain.centos: | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | - destroy | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # libvirt_cloudinit_disk.commoninit will be destroyed | ||
| - | - resource " | ||
| - | - id = "/ | ||
| - | - name = " | ||
| - | - pool = " | ||
| - | - user_data = << | ||
| - | # | ||
| - | ssh_pwauth: True | ||
| - | chpasswd: | ||
| - | list: | | ||
| - | root:centos | ||
| - | expire: False | ||
| - | EOT -> null | ||
| - | } | ||
| - | |||
| - | # libvirt_domain.centos will be destroyed | ||
| - | - resource " | ||
| - | - arch = " | ||
| - | - autostart | ||
| - | - cloudinit | ||
| - | - cmdline | ||
| - | - emulator | ||
| - | - fw_cfg_name = " | ||
| - | - id = " | ||
| - | - machine | ||
| - | - memory | ||
| - | - name = " | ||
| - | - qemu_agent | ||
| - | - running | ||
| - | - vcpu = 2 -> null | ||
| - | |||
| - | - cpu { | ||
| - | - mode = " | ||
| - | } | ||
| - | |||
| - | - disk { | ||
| - | - scsi = false -> null | ||
| - | - volume_id = "/ | ||
| - | } | ||
| - | |||
| - | - network_interface { | ||
| - | - addresses | ||
| - | - " | ||
| - | ] -> null | ||
| - | - mac = " | ||
| - | - network_id | ||
| - | - network_name | ||
| - | - wait_for_lease = false -> null | ||
| - | } | ||
| - | } | ||
| - | |||
| - | # libvirt_volume.rhel will be destroyed | ||
| - | - resource " | ||
| - | - format = " | ||
| - | - id = "/ | ||
| - | - name = " | ||
| - | - pool = " | ||
| - | - size = 8589934592 -> null | ||
| - | - source = "/ | ||
| - | } | ||
| - | |||
| - | Plan: 0 to add, 0 to change, 3 to destroy. | ||
| - | |||
| - | Do you really want to destroy all resources? | ||
| - | Terraform will destroy all your managed infrastructure, | ||
| - | There is no undo. Only ' | ||
| - | |||
| - | Enter a value: yes | ||
| - | |||
| - | libvirt_domain.centos: | ||
| - | libvirt_domain.centos: | ||
| - | libvirt_cloudinit_disk.commoninit: | ||
| - | libvirt_volume.rhel: | ||
| - | libvirt_cloudinit_disk.commoninit: | ||
| - | libvirt_volume.rhel: | ||
| - | |||
| - | Destroy complete! Resources: 3 destroyed. | ||
| - | </ | ||
| - | ===== Referências ===== | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
terraform_kvm.1753560544.txt.gz · Last modified: by wikiadm
