josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
544 stars 154 forks source link

Injecting properties #57

Closed xeor closed 5 years ago

xeor commented 5 years ago

I was trying to get the ubuntu (https://cloud-images.ubuntu.com/releases/19.04/release/ubuntu-19.04-server-cloudimg-amd64.ova) images to work using cloud-init. After reading a lot, it seams to be no way to do this as the examples would... Without, there is really not a good way of using this image at all, since you can't login after :/

I did however see using govc import.spec ubuntu-19.04-server-cloudimg-amd64.ova | python -m json.tool that the image has a couple of properties, both public-keys, user-data and password.

ovftoolsupports setting those if --X:injectOvfEnv is set, using params like --prop:password="myP4ssw0rd".

This would be extremely useful to have in the esxi_guest resource.

Suggestion

Relevant code @ https://github.com/josenk/terraform-provider-esxi/blob/8409d0bd4f262ad22e58d044e460b668fd9fb588/esxi/guest-create.go#L203-L204

Thoughts?

I have little experience with go, terraform and those vmware tools.. But I can try to create a PR on this if it looks like a good idea..

josenk commented 5 years ago

At this point, I'm not interested to add a feature if the current method works. Why do you say cloud-init doesn't work with that Ubuntu cloud image? Can you provide details to that problem.

xeor commented 5 years ago

I found a couple of articles about this, and when I tested, I could not make the examples using cloud-init in this repo work.

https://blah.cloud/kubernetes/creating-an-ubuntu-18-04-lts-cloud-image-for-cloning-on-vmware/ kinda shows a workaround, but you need to edit the image.

It seams like these properties can't be edited in esxi alone, you would need the vcenter as well.

I also found https://www.virtuallyghetto.com/2014/05/how-to-finally-inject-ovf-properties-into-vcsa-when-deploying-directly-onto-esxi.html which describes a way to edit the needed properties and expose them to the vm. It uses what I described earlier.

I have really no idea why it's like this with the ubuntu-cloud-image and why they need the properties instead of what everyone else is using.

The properties is something that is needed for this image, but might also be useful down the line for other configurations as well.

josenk commented 5 years ago

I don't think those Ubuntu cloud images have cloud-init for vmware installed (they just have the generic cloud-init). You need this installed to make it work for this plugin to work.

https://github.com/vmware/cloud-init-vmware-guestinfo

Most people do this and It's very easy to just install the base/minimal Ubuntu through the console. Install all requirements (including open-vm-tools, cloud-init for vmware, etc...). You can then either clone it, or create your own ova/ovf.

xeor commented 5 years ago

Since Ubuntu didn't have those packages, I thought it would be nice to support another alternative which didn't require a rebuild of the image. Using the upstream ova would be nice.

I did some more experimenting on this, and it also looks like it only works if ovftool have the -PowerOn flag as well (a bug maybe)? If it's powered on later, the props won't have any impact at all it seams..

I'll look into it more, create a bug report with vmware maybe, then see of it's easier to just rebuild the image.

xeor commented 5 years ago

Just found --X:injectOvfEnv Force the given OVF properties to be inserted into an OVF Environment and injected through VMware Tools. Must be used together with --powerOn the VI target for an ESX host and a single VM source.

Which explains that at least :(

josenk commented 5 years ago

I need to manipulate the vmx file before powering up the VM... So, I cannot power-on during the ovftool transfer. Also, there's no guarantee that the ovf/ova source will have VMware tools installed.

xeor commented 5 years ago

I found someone with the same problem and possible a hint for a solution at https://github.com/ansible/ansible/issues/50299#issuecomment-468299332, I'll investigate that.

Maybe it is possible to do after creation editing the for this as well.. :)

xeor commented 5 years ago

I ended up with a tiny script to make a template, then using that template using this plugin.. Here are some code:

#!/bin/sh

esxi_host=esxihosthere
esxi_password="passwordhere"

ova_image="ubuntu-19.04-server-cloudimg-amd64.ova"

cloud_config=$(cat << EOF | base64
#cloud-config
snappy: 
  ssh_enabled: true
ssh_authorized_keys:
  - ssh-rsa AAAA...Lan7SWE= xeor
runcmd:
  - 'echo "disable_vmware_customization: false" >> /etc/cloud/cloud.cfg'
  - sed -i 's/D \/tmp 1777 root root -/#D \/tmp 1777 root root -/g' /usr/lib/tmpfiles.d/tmp.conf
  - echo -n > /etc/machine-id
power_state:
  timeout: 30
  mode: poweroff
EOF
)

ovftool --acceptAllEulas --noSSLVerify --X:useMacNaming=false -dm=thin --powerOn \
        --name='template-ubuntu-19.04' --X:injectOvfEnv --prop:user-data=${cloud_config} --overwrite \
        -ds='datastore1' --network='VM Network' "${ova_image}" "vi://root:${esxi_password}@${esxi_host}/"

After that is done, this works:

resource "esxi_guest" "..." {
  ...
  clone_from_vm = "template-ubuntu-19.04"
  guestos = "ubuntu-64"
}

and you can see the ip in the esxi console (open-vm-tools is in).