├── .gitignore ├── README.md ├── main.tf ├── outputs.tf ├── templates ├── metadata.yaml └── userdata.yaml ├── terraform.tfvars.example ├── variables.tf └── vsphere_data.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | 11 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 12 | # .tfvars files are managed as part of configuration and so should be included in 13 | # version control. 14 | # 15 | # example.tfvars 16 | terraform.tfvars 17 | 18 | # Ignore override files as they are usually used to override resources locally and so 19 | # are not checked in 20 | override.tf 21 | override.tf.json 22 | *_override.tf 23 | *_override.tf.json 24 | 25 | # Include override files you do wish to add to version control using negated pattern 26 | # 27 | # !example_override.tf 28 | 29 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 30 | # example: *tfplan* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cloud-Init Sample 2 | 3 | This repo provides an example of deploying a Ubuntu machine with some hardcoded values in both the metadata and userdata files included in the templates directory. 4 | 5 | Cloud-init configuration is provided via the [VMware guestinfo datasource](https://github.com/vmware/cloud-init-vmware-guestinfo). A sample Packer build that has the relevant dependencies pre-installed can be found [here](https://github.com/grantorchard/packer-vsphere-cloudinit). -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | provider "vsphere" { 2 | user = var.vsphere_user 3 | password = var.vsphere_password 4 | vsphere_server = var.vsphere_server 5 | 6 | # If you have a self-signed cert 7 | allow_unverified_ssl = true 8 | } 9 | 10 | resource vsphere_virtual_machine "this" { 11 | name = var.hostname 12 | resource_pool_id = data.vsphere_compute_cluster.this.resource_pool_id 13 | datastore_id = data.vsphere_datastore.this.id 14 | 15 | num_cpus = 2 16 | memory = 1024 17 | guest_id = data.vsphere_virtual_machine.template.guest_id 18 | 19 | network_interface { 20 | network_id = data.vsphere_network.this.id 21 | adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0] 22 | } 23 | wait_for_guest_net_timeout = 0 24 | 25 | disk { 26 | label = "disk0" 27 | size = data.vsphere_virtual_machine.template.disks.0.size 28 | eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub 29 | thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned 30 | } 31 | 32 | clone { 33 | template_uuid = data.vsphere_virtual_machine.template.id 34 | } 35 | 36 | extra_config = { 37 | "guestinfo.metadata" = base64encode(file("${path.module}/templates/metadata.yaml")) 38 | "guestinfo.metadata.encoding" = "base64" 39 | "guestinfo.userdata" = base64encode(file("${path.module}/templates/userdata.yaml")) 40 | "guestinfo.userdata.encoding" = "base64" 41 | } 42 | } -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /templates/metadata.yaml: -------------------------------------------------------------------------------- 1 | network: 2 | version: 2 3 | ethernets: 4 | ens192: 5 | dhcp4: false 6 | addresses: 7 | - 10.0.0.200/24 8 | gateway4: 10.0.0.1 9 | nameservers: 10 | addresses: 11 | - 192.168.1.5 12 | local-hostname: ubuntu-01 13 | instance-id: ubuntu01 -------------------------------------------------------------------------------- /templates/userdata.yaml: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | users: 3 | - name: grant 4 | ssh-authorized-keys: 5 | - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8rqxon4hRyV5cLNZczuJTe8dsZ33hpWHDU993r4iiY3t9bXqfmIHlIZ7dTL93nlvsgzVdOYMVGMOHMg/a1ZK0VRoKTS5BBhBGJejjDUfWRAtedZbM9JE5HHpks+L+nf8cOM14Os+Q3BV+z4MjYfIK5ZbV0IvUaY0kscQcE8cZoOTC2hHu/MPDneKJxG+HRQJfvqvnWz69/EXyi9iqtmOn0Xy9905qtbPNlDs1c4qF+zZ1qQCkMYP0Z4AVvLaPEJZlPmDnGqz5s1vVb130aXe1A11eq4RwgvZRxXW8i88pKqCGPuLRh7anqvSI15SLpA2KWvu7wD5CvhTisc/6TfVf 6 | sudo: ['ALL=(ALL) NOPASSWD:ALL'] 7 | groups: sudo 8 | shell: /bin/bash 9 | 10 | packages: 11 | - jq -------------------------------------------------------------------------------- /terraform.tfvars.example: -------------------------------------------------------------------------------- 1 | datacenter_name = "Core" 2 | cluster_name = "Tenant" 3 | datastore_name = "hl-core-ds01" 4 | vm_network_name = "Common" 5 | vsphere_user = "administrator@vsphere.local" 6 | vsphere_password = "VMware123!" 7 | vsphere_server = "hlcorevc01.humblelab.com" 8 | template_name = "ubuntu-18.04-packer" 9 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | # --------------------------------------------------------------------------------------------------------------------- 2 | # VMWARE PROVIDER VARIABLES 3 | # These are used to connect to vCenter. 4 | # --------------------------------------------------------------------------------------------------------------------- 5 | 6 | variable "vsphere_server" { 7 | type = string 8 | } 9 | 10 | variable "vsphere_user" { 11 | type = string 12 | } 13 | 14 | variable "vsphere_password" { 15 | type = string 16 | } 17 | 18 | # --------------------------------------------------------------------------------------------------------------------- 19 | # VMWARE DATA SOURCE VARIABLES 20 | # These are used to discover unmanaged resources used during deployment. 21 | # --------------------------------------------------------------------------------------------------------------------- 22 | 23 | variable datacenter_name { 24 | type = string 25 | description = "The name of the vSphere Datacenter into which resources will be created." 26 | } 27 | 28 | variable cluster_name { 29 | type = string 30 | description = "The vSphere Cluster into which resources will be created." 31 | } 32 | 33 | variable datastore_name { 34 | type = string 35 | description = "The vSphere Datastore into which resources will be created." 36 | } 37 | 38 | variable datastore_cluster_name { 39 | type = string 40 | default = "" 41 | } 42 | 43 | variable vm_network_name { 44 | type = string 45 | } 46 | 47 | variable template_name { 48 | type = string 49 | } 50 | 51 | # --------------------------------------------------------------------------------------------------------------------- 52 | # VMWARE RESOURCE VARIABLES 53 | # Variables used during the creation of resources in vSphere. 54 | # --------------------------------------------------------------------------------------------------------------------- 55 | 56 | variable hostname { 57 | type = string 58 | default = "ubuntu" 59 | description = "The virtual machine name in vCenter." 60 | } 61 | 62 | variable nameservers { 63 | type = list 64 | default = [] 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /vsphere_data.tf: -------------------------------------------------------------------------------- 1 | data vsphere_datacenter "this" { 2 | name = var.datacenter_name 3 | } 4 | 5 | data vsphere_compute_cluster "this" { 6 | name = var.cluster_name 7 | datacenter_id = data.vsphere_datacenter.this.id 8 | } 9 | 10 | data vsphere_datastore "this" { 11 | name = var.datastore_name 12 | datacenter_id = data.vsphere_datacenter.this.id 13 | } 14 | 15 | /* 16 | data vsphere_datastore_cluster "this" { 17 | name = var.datastore_name 18 | datacenter_id = data.vsphere_datacenter.this.id 19 | } 20 | */ 21 | 22 | data vsphere_network "this" { 23 | name = var.vm_network_name 24 | datacenter_id = data.vsphere_datacenter.this.id 25 | } 26 | 27 | data vsphere_virtual_machine "template" { 28 | name = var.template_name 29 | datacenter_id = data.vsphere_datacenter.this.id 30 | } --------------------------------------------------------------------------------