|
| 1 | +Type: `vsphere-virtualmachine` |
| 2 | +Artifact BuilderId: `vsphere.virtualmachine` |
| 3 | + |
| 4 | +This data source retrieves information about existing virtual machines from vSphere |
| 5 | +and return name of one virtual machine that matches all specified filters. This virtual |
| 6 | +machine can be used in the vSphere Clone builder to select a template. |
| 7 | + |
| 8 | +## Configuration Reference |
| 9 | + |
| 10 | +### Filters Configuration |
| 11 | + |
| 12 | +**Optional:** |
| 13 | + |
| 14 | +<!-- Code generated from the comments of the Config struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY --> |
| 15 | + |
| 16 | +- `name` (string) - Basic filter with glob support (e.g. `ubuntu_basic*`). Defaults to `*`. |
| 17 | + Using strict globs will not reduce execution time because vSphere API |
| 18 | + returns the full inventory. But can be used for better readability over |
| 19 | + regular expressions. |
| 20 | + |
| 21 | +- `name_regex` (string) - Extended name filter with regular expressions support |
| 22 | + (e.g. `ubuntu[-_]basic[0-9]*`). Default is empty. The match of the |
| 23 | + regular expression is checked by substring. Use `^` and `$` to define a |
| 24 | + full string. For example, the `^[^_]+$` filter will search names |
| 25 | + without any underscores. The expression must use |
| 26 | + [Go Regex Syntax](https://pkg.go.dev/regexp/syntax). |
| 27 | + |
| 28 | +- `template` (bool) - Filter to return only objects that are virtual machine templates. |
| 29 | + Defaults to `false` and returns all virtual machines. |
| 30 | + |
| 31 | +- `host` (string) - Filter to search virtual machines only on the specified ESX host. |
| 32 | + |
| 33 | +- `tags` ([]Tag) - Filter to return only that virtual machines that have attached all |
| 34 | + specifies tags. Specify one or more `tags` blocks to define list of tags |
| 35 | + for the filter. |
| 36 | + |
| 37 | +- `latest` (bool) - This filter determines how to handle multiple machines that were |
| 38 | + matched with all previous filters. Machine creation time is being used |
| 39 | + to find latest. By default, multiple matching machines results in an |
| 40 | + error. |
| 41 | + |
| 42 | +<!-- End of code generated from the comments of the Config struct in datasource/virtualmachine/data.go; --> |
| 43 | + |
| 44 | + |
| 45 | +### Tags Filter Configuration |
| 46 | + |
| 47 | +<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY --> |
| 48 | + |
| 49 | +HCL Example: |
| 50 | + |
| 51 | +```hcl |
| 52 | +
|
| 53 | + tags { |
| 54 | + category = "team" |
| 55 | + name = "operations" |
| 56 | + } |
| 57 | + tags { |
| 58 | + category = "sla" |
| 59 | + name = "gold" |
| 60 | + } |
| 61 | +
|
| 62 | +``` |
| 63 | + |
| 64 | +<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; --> |
| 65 | + |
| 66 | + |
| 67 | +**Required:** |
| 68 | + |
| 69 | +<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY --> |
| 70 | + |
| 71 | +- `name` (string) - Name of the tag added to virtual machine which must pass the `tags` |
| 72 | + filter. |
| 73 | + |
| 74 | +- `category` (string) - Name of the tag category that contains the tag. |
| 75 | + |
| 76 | + -> **Note:** Both `name` and `category` must be specified in the `tags` |
| 77 | + filter. |
| 78 | + |
| 79 | +<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; --> |
| 80 | + |
| 81 | + |
| 82 | +### Connection Configuration |
| 83 | + |
| 84 | +**Optional:** |
| 85 | + |
| 86 | +<!-- Code generated from the comments of the ConnectConfig struct in builder/vsphere/common/step_connect.go; DO NOT EDIT MANUALLY --> |
| 87 | + |
| 88 | +- `vcenter_server` (string) - The fully qualified domain name or IP address of the vCenter Server |
| 89 | + instance. |
| 90 | + |
| 91 | +- `username` (string) - The username to authenticate with the vCenter Server instance. |
| 92 | + |
| 93 | +- `password` (string) - The password to authenticate with the vCenter Server instance. |
| 94 | + |
| 95 | +- `insecure_connection` (bool) - Do not validate the certificate of the vCenter Server instance. |
| 96 | + Defaults to `false`. |
| 97 | + |
| 98 | + -> **Note:** This option is beneficial in scenarios where the certificate |
| 99 | + is self-signed or does not meet standard validation criteria. |
| 100 | + |
| 101 | +- `datacenter` (string) - The name of the datacenter object in the vSphere inventory. |
| 102 | + |
| 103 | + -> **Note:** Required if more than one datacenter object exists in the |
| 104 | + vSphere inventory. |
| 105 | + |
| 106 | +<!-- End of code generated from the comments of the ConnectConfig struct in builder/vsphere/common/step_connect.go; --> |
| 107 | + |
| 108 | + |
| 109 | +## Output |
| 110 | + |
| 111 | +<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY --> |
| 112 | + |
| 113 | +- `vm_name` (string) - Name of the found virtual machine. |
| 114 | + |
| 115 | +<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; --> |
| 116 | + |
| 117 | + |
| 118 | +## Example Usage |
| 119 | + |
| 120 | +This example demonstrates how to connect to vSphere cluster and search for the latest virtual machine |
| 121 | +that matches the filters. The name of the machine is then output to the console as an output variable. |
| 122 | +```hcl |
| 123 | +data "vsphere-virtualmachine" "default" { |
| 124 | + vcenter_server = "vcenter.example.com" |
| 125 | + insecure_connection = true |
| 126 | + |
| 127 | + password = "VMware1!" |
| 128 | + datacenter = "dc-01" |
| 129 | + latest = true |
| 130 | + tags { |
| 131 | + category = "team" |
| 132 | + name = "operations" |
| 133 | + } |
| 134 | + tags { |
| 135 | + category = "sla" |
| 136 | + name = "gold" |
| 137 | + } |
| 138 | +
|
| 139 | +} |
| 140 | +
|
| 141 | +locals { |
| 142 | + vm_name = data.vsphere-virtualmachine.default.vm_name |
| 143 | +} |
| 144 | +
|
| 145 | +source "null" "example" { |
| 146 | + communicator = "none" |
| 147 | +} |
| 148 | +
|
| 149 | +build { |
| 150 | + sources = [ |
| 151 | + "source.null.example" |
| 152 | + ] |
| 153 | +
|
| 154 | + provisioner "shell-local" { |
| 155 | + inline = [ |
| 156 | + "echo vm_name: ${local.vm_name}", |
| 157 | + ] |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
0 commit comments