--- - name: Create a VM in resource pool hosts: localhost connection: local gather_facts: False vars_prompt: - name: "user" prompt: "Enter your username to virtualcenter" private: no - name: "password" prompt: "Enter your password to virtualcenter" private: yes - name: "guest" prompt: "Enter you guest VM name: " private: no tasks: - name: create VM vsphere_guest: vcenter_hostname: vcenter.example.com validate_certs: no username: '{{ user }}' password: '{{ password }}' guest: '{{ guest }}' state: powered_off vm_extra_config: vcpu.hotadd: yes mem.hotadd: yes notes: This is a test VM vm_disk: disk1: size_gb: 10 type: thick datastore: mydatastore vm_nic: nic1: type: vmxnet3 network: mynetwork network_type: standard vm_hardware: memory_mb: 1024 num_cpus: 1 osid: centos64Guest scsi: paravirtual resource_pool: "/Resources/MyResourcePool" esxi: datacenter: mysite #hostname: myesxhost01
provider "vsphere" { user = “mylogin@example.com" password = "${var.vsphere_password}" vsphere_server = “virtualcenter.example.com" allow_unverified_ssl = "true" } resource "vsphere_virtual_machine" "test" { name = "${var.machine_name}" vcpu = 1 memory = 1024 domain = “test.example.com” datacenter = "mysite" resource_pool = "Production Cluster #1/Resources/myresourcepool" network_interface { label = "test" ipv4_address = "192.168.1.2" ipv4_prefix_length = "24" ipv4_gateway = "192.168.1.1" } disk { datastore = "${var.datastore}" size = "10" name = "${var.datastore}/${var.machine_name}/${var.machine_name}.vmdk" template = "mytemplate" } }
variable "vsphere_password" {} variable "machine_name" { type = "string" default = "test" } variable "datastore" { type = "string" default = "mysite/mydatastore" }
$ terraform plan var.vsphere_password Enter a value: supersecurepassword Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Cyan entries are data sources to be read. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + vsphere_virtual_machine.test datacenter: "mysite" detach_unknown_disks_on_delete: "false" disk.#: "1" disk.1370406802.bootable: "" disk.1370406802.controller_type: "scsi" disk.1370406802.datastore: "" disk.1370406802.iops: "" disk.1370406802.keep_on_remove: "" disk.1370406802.key: "<computed>" disk.1370406802.name: "" disk.1370406802.size: "" disk.1370406802.template: "mytemplate" disk.1370406802.type: "eager_zeroed" disk.1370406802.uuid: "<computed>" disk.1370406802.vmdk: "" domain: “test.example.com” enable_disk_uuid: "false" linked_clone: "false" memory: "1024" memory_reservation: "0" name: "test" network_interface.#: "1" network_interface.0.ip_address: "<computed>" network_interface.0.ipv4_address: “192.168.1.2" network_interface.0.ipv4_gateway: "192.168.1.1" network_interface.0.ipv4_prefix_length: "24" network_interface.0.ipv6_address: "<computed>" network_interface.0.ipv6_gateway: "<computed>" network_interface.0.ipv6_prefix_length: "<computed>" network_interface.0.label: "test" network_interface.0.mac_address: "<computed>" network_interface.0.subnet_mask: "<computed>" resource_pool: "Production Cluster #1/Resources/myresourcepool" skip_customization: "false" time_zone: "Etc/UTC" uuid: "<computed>" vcpu: "1" Plan: 1 to add, 0 to change, 0 to destroy.
Error applying plan: 1 error(s) occurred: * vsphere_virtual_machine.test: Datastore 'mysite/mydatastore not found. Terraform does not automatically rollback in the face of errors. Instead, your Terraform state file has been partially updated with any resources that successfully completed. Please address the error above and apply again to incrementally change your infrastructure.
Error applying plan: 1 error(s) occurred: * vsphere_virtual_machine.test: Datastore 'mysite/mydatastore/mydatastore-vol01' not found. Terraform does not automatically rollback in the face of errors. Instead, your Terraform state file has been partially updated with any resources that successfully completed. Please address the error above and apply again to incrementally change your infrastructure.
Param( [string]$Name, [string]$ResourcePool, [string]$Location, [int]$NumCPU, [int]$MemoryGB, [int]$DiskGB) $ErrorActionPreference = "Stop" Try { $credential = Get-Credential Add-PSSnapin VMware.VimAutomation.Core [string] $username = $credential.GetNetworkCredential().UserName $username = 'example\' + $username Connect-VIServer -Server virtualcenter.example.com -User $username -Password $credential.GetNetworkCredential().Password -Force $params = @{ name = $Name ResourcePool = $ResourcePool Location = $Location NumCPU = $NumCPU MemoryGB = $MemoryGB DiskGB = $DiskGB } new-vm @params } Catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.DuplicateName] {"VM exists"}
.\createvm.ps1 -Name mytestvm -ResourcePool myresourcepool -Location myteam -NumCPI 1 - MemoryGB 1 -DisckGB 10
[string] $username = $credential.GetNetworkCredential().UserName $username = 'example\' + $username
Source: https://habr.com/ru/post/317188/
All Articles