Comment importer des machines virtuelles VMWare existantes dans Terraform

Nous invitons les futurs étudiants du cours "Infrastructure as a code in Ansible" , ainsi que toutes les personnes intéressées, à participer au webinaire ouvert sur "Gérer Kubernetes avec Kubespray" .







Et selon la tradition établie, nous partageons avec vous la traduction d'un article utile.














Terraform  est un outil formidable pour automatiser la gestion de l'infrastructure. Tous les paramètres de votre infrastructure peuvent être écrits sous la forme d'un code qui sera maintenu par le groupe correspondant. Cela signifie que votre infrastructure sera transparente et non sujette à des changements inattendus.





Publié à l'origine sur le blog techbeatly ; d'autres articles connexes y sont également disponibles.





À lire aussi:  Curriculum de l'associé Terraform certifié HashiCorp et conseils pour passer l'examen .





, , Terraform , Terraform. , , VMWare. , VMWare Terraform. .





1. VMWare vCenter

VMWare vCenter . ,   ,  ,  , , ,   . .





, , , Terraform.





2. Terraform

Terraform Terraform. Terraform, , .





Terraform, : /DC1/vm/DEV/DEV2



.





.  vmware-import-vm.tf.







provider "vsphere" {
  user           = var.vsphere_user
  password       = var.vsphere_password
  vsphere_server = var.vsphere_server

  # If you have a self-signed cert
  allow_unverified_ssl = true
}

data "vsphere_datacenter" "dc" {
  name = "DC1"
}

data "vsphere_datastore" "datastore" {
  name          = "datastore1"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_compute_cluster" "cluster" {
  name          = "AZ1"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_network" "network" {
  name          = "VM Network"
  datacenter_id = data.vsphere_datacenter.dc.id
}

resource "vsphere_virtual_machine" "vm" {
  name             = "DEV2"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  wait_for_guest_net_timeout = 0
  wait_for_guest_ip_timeout  = 0

  # only if you DO NOT want to wait for an IP address
  wait_for_guest_net_routable = false

  num_cpus = 1
  memory   = 2048
  #guest_id = "other3xLinux64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
  }

  
  disk {
    label            = "disk0"
    size             = 20
    thin_provisioned = false
  }
}
      
      



VMWare.





$ cat variables.tf 
variable "vsphereuser" {}
variable "vspherepassword" {}
      
      



VMWare vCenter (. ).





$ export TFVARvsphereuser='Administrator@lab.local'
$ export TFVARvspherepassword='mypassword'
      
      



3. Terraform

,  terraform init, .





$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/vsphere...
- Installing hashicorp/vsphere v1.24.2...
- Installed hashicorp/vsphere v1.24.2 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/vsphere: version = "~> 1.24.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
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.
      
      



, Terraform:





$ terraform show
No state.
      
      



, .





. ,  terraform apply



? , , . ,  DEV2



  . , .





4. Terraform

, Terraform.





$ terraform import vsphere_virtual_machine.vm /DC1/vm/DEV/DEV2
vsphere_virtual_machine.vm: Importing from ID "/DC1/vm/DEV/DEV2"...
vsphere_virtual_machine.vm: Import prepared!
  Prepared vsphere_virtual_machine for import
vsphere_virtual_machine.vm: Refreshing state... [id=4219040f-5842-ba52-b7e4-cd9064c1f36c]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
      
      



,  — Terraform,  Terraform, , , . .





,  terraform show



  .





$ terraform show
# vsphere_virtual_machine.vm:
resource "vsphere_virtual_machine" "vm" {
    boot_delay                              = 0
    boot_retry_delay                        = 10000
    boot_retry_enabled                      = false
    change_version                          = "2020-11-03T08:33:13.180937Z"
    cpu_hot_add_enabled                     = false
    cpu_hot_remove_enabled                  = false
    cpu_limit                               = -1
    cpu_performance_counters_enabled        = false
    cpu_reservation                         = 0
    cpu_share_count                         = 1000
    cpu_share_level                         = "normal"
    custom_attributes                       = {}
    datastore_id                            = "datastore-13"
    efi_secure_boot_enabled                 = false
    enable_disk_uuid                        = false
    enable_logging                          = true
    ept_rvi_mode                            = "automatic"
    extra_config                            = {}
    firmware                                = "bios"
    folder                                  = "DEV"
    force_power_off                         = true
    guest_id                                = "rhel7_64Guest"
    guest_ip_addresses                      = []
    hardware_version                        = 14
    host_system_id                          = "host-12"
    hv_mode                                 = "hvAuto"
    id                                      = "4219040f-5842-ba52-b7e4-cd9064c1f36c"
    ide_controller_count                    = 2
    imported                                = true
    latency_sensitivity                     = "normal"
    memory                                  = 2048
    memory_hot_add_enabled                  = false
    memory_limit                            = -1
    memory_reservation                      = 0
    memory_share_count                      = 20480
    memory_share_level                      = "normal"
    migrate_wait_timeout                    = 30
    moid                                    = "vm-47"
    name                                    = "DEV2"
    nested_hv_enabled                       = false
    num_cores_per_socket                    = 1
    num_cpus                                = 1
    pci_device_id                           = []
    poweron_timeout                         = 300
    reboot_required                         = false
    resource_pool_id                        = "resgroup-8"
    run_tools_scripts_after_power_on        = true
    run_tools_scripts_after_resume          = true
    run_tools_scripts_before_guest_reboot   = false
    run_tools_scripts_before_guest_shutdown = true
    run_tools_scripts_before_guest_standby  = true
    sata_controller_count                   = 1
    scsi_bus_sharing                        = "noSharing"
    scsi_controller_count                   = 1
    scsi_type                               = "pvscsi"
    shutdown_wait_timeout                   = 3
    swap_placement_policy                   = "inherit"
    sync_time_with_host                     = false
    tags                                    = []
    uuid                                    = "4219040f-5842-ba52-b7e4-cd9064c1f36c"
    vapp_transport                          = []
    vmware_tools_status                     = "guestToolsRunning"
    vmx_path                                = "DEV2/DEV2.vmx"
    wait_for_guest_ip_timeout               = 0
    wait_for_guest_net_routable             = true
    wait_for_guest_net_timeout              = 5

    cdrom {
        client_device  = false
        datastore_id   = "datastore-13"
        device_address = "sata:0:0"
        key            = 16000
        path           = "ISO/rhel-server-7.7-x86_64-dvd.iso"
    }

    disk {
        attach           = false
        controller_type  = "scsi"
        datastore_id     = "datastore-13"
        device_address   = "scsi:0:0"
        disk_mode        = "persistent"
        disk_sharing     = "sharingNone"
        eagerly_scrub    = false
        io_limit         = -1
        io_reservation   = 0
        io_share_count   = 1000
        io_share_level   = "normal"
        keep_on_remove   = true
        key              = 2000
        label            = "disk0"
        path             = "DEV2/DEV2.vmdk"
        size             = 20
        thin_provisioned = false
        unit_number      = 0
        uuid             = "6000C29b-c4f0-764a-9054-a042931350c4"
        write_through    = false
    }
}
      
      



, ,   (provisioners).





. vsphere_virtual_machine.





GitHub  terraform-vmware-demos.






"Infrastructure as a code in Ansible".



" Kubernetes Kubespray".












All Articles