Using cloud-init with vSphere and openSUSE 15.4
Tags: cloud, fosslinux, homelab, linux
Published: 2022-09-24 00:52:11

---
title: Using cloud-init with vSphere and openSUSE 15.4
published: true
date: 2022-09-24 00:30:00 UTC
tags: Cloud,FOSSLinux,HomeLab,Linux
canonical_url: https://blog.engyak.co/2022/09/using-cloud-init-with-vsphere-and.html
---

Rapidly deploying Linux servers to meet a whim represents the essence of home lab activities, but we spend a great deal of time spinning/configuring machines to meet our specs.

Worse, we lose a great deal of time keeping them properly configured and up to date, and none have the privilege of unlimited lab time.

Let's explore a way to get a base template implemented in vSphere 7 and enable the machine to boot with customizations like hostname, IP address, startup scripts, etc.

### Constructing a VM Template   

First, let's pick up a fresh operating system installer ISO from [opensuse.org](https://get.opensuse.org/leap/). Since this is a home lab / server-style deployment, I'd recommend using the **network image** - we'll add everything we want later.

Upload the ISO file to a datastore. This step will allow the installation process to run unattended, even if you shut down the client:

  

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-5DBhC_N9rGBCffE92nywDYTuoHYd63rxBhMhxZ65TGm9XwwQpVAYUS6Kj9ZSBQK3MbrGxisGk2zk04cu7ruP868E7ewfZkQ0VDw7fPuHMxg6qgg2uevmfyw0jPRt0ZQeh5ht1RPvN6ZxfUkCiVHNPQdr_NGzwVj0XWS2IubGpcHl-0q0ZKYtlDDd/w640-h248/2022-09-11%2017_15_06-vSphere%20-%20eng-lab-nfs-iso-liburatt%20-%20Files%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-5DBhC_N9rGBCffE92nywDYTuoHYd63rxBhMhxZ65TGm9XwwQpVAYUS6Kj9ZSBQK3MbrGxisGk2zk04cu7ruP868E7ewfZkQ0VDw7fPuHMxg6qgg2uevmfyw0jPRt0ZQeh5ht1RPvN6ZxfUkCiVHNPQdr_NGzwVj0XWS2IubGpcHl-0q0ZKYtlDDd/s634/2022-09-11%2017_15_06-vSphere%20-%20eng-lab-nfs-iso-liburatt%20-%20Files%20%E2%80%94%20Mozilla%20Firefox.png)

Create a virtual machine, and name it accordingly. Attach the datastore ISO:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzw_UNSZFTmm_6lbysFLwUZWa9m76-tLKmhw4zRu9CBxqVcRLDO_0VvZ-g3RAQ34ahYwYbAiyYMxufFsvln5FAY50BCG6rOSkHBu0ou4fv-4ZAL2f2ryefJhCpk2cpx2fUFVutHDrwwpVek2-I-bKF8MakrMA7g0ug9fA3Y-brEmoy4sMLrtUT1Hv8/w640-h360/2022-09-11%2017_17_11-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzw_UNSZFTmm_6lbysFLwUZWa9m76-tLKmhw4zRu9CBxqVcRLDO_0VvZ-g3RAQ34ahYwYbAiyYMxufFsvln5FAY50BCG6rOSkHBu0ou4fv-4ZAL2f2ryefJhCpk2cpx2fUFVutHDrwwpVek2-I-bKF8MakrMA7g0ug9fA3Y-brEmoy4sMLrtUT1Hv8/s839/2022-09-11%2017_17_11-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)

Boot the Linux machine. During the installation wizard, ensure that a logical volume manager (LVM2). I've found that when you build a clone template, any disk size you choose will be wrong in the application owner's mind, so plan for the future.

After the installation is complete, **disconnect the CD/DVD virtual drive!** If you fail to do this on shared infrastructure, the VI admins will have a difficult time with the VM - and in a home lab, that's you. Establish good habits to make responsible tenancy easy.

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqPTTEVvj-8dssxKucwAssSeDun731XF985LS0yQ3-ODEXtWW9t9bxbiW_dD04uBkMAaGgDTjYFAdKQvm9srqkYLgsFJeP18BxVmkmI063c-OWqqxYejvgZikTxmhysvoUhZAApxUj0NCHRkmbNQl_LzlGaHyj_dLj6CS6yvBHUbuuINZERJKkHm3I/w640-h152/2022-09-11%2017_21_23-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqPTTEVvj-8dssxKucwAssSeDun731XF985LS0yQ3-ODEXtWW9t9bxbiW_dD04uBkMAaGgDTjYFAdKQvm9srqkYLgsFJeP18BxVmkmI063c-OWqqxYejvgZikTxmhysvoUhZAApxUj0NCHRkmbNQl_LzlGaHyj_dLj6CS6yvBHUbuuINZERJKkHm3I/s800/2022-09-11%2017_21_23-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)

Start up the machine, and use _ **zypper** _ to install any packages or keys that may be required to administer the device. In a home lab, CA certificates and SSH keys are OK - but an enterprise environment should have an automated, repeatable way to lifecycle trust in the event of a compromise.

Once that's done, let's install [_ **cloud-init** _](https://cloudinit.readthedocs.io/en/latest/). This software package is incredibly useful, but it isn't available by default with OpenSUSE Leap:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUttY-JESI2GEt2wgTVNlrZHbbYYVXThNXFoSlnHs97hqM6S1R8e-ExIIYATTpGegZPUk44FEm8Q6SHdXfKtpQI08HbrNCYB35p0Us-klh5gBE8T8iQWb_IeE87jAQ2K1kfmAYwRPLnzCuzzvaAnmKS41V1UUjYBO-XfJQZI_RYcdQ5p0Qiy9vPRcE/w640-h186/2022-09-11%2008_13_26-rancher-base%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUttY-JESI2GEt2wgTVNlrZHbbYYVXThNXFoSlnHs97hqM6S1R8e-ExIIYATTpGegZPUk44FEm8Q6SHdXfKtpQI08HbrNCYB35p0Us-klh5gBE8T8iQWb_IeE87jAQ2K1kfmAYwRPLnzCuzzvaAnmKS41V1UUjYBO-XfJQZI_RYcdQ5p0Qiy9vPRcE/s1660/2022-09-11%2008_13_26-rancher-base%20%E2%80%94%20Mozilla%20Firefox.png)

After installing the package, ensure it's enabled with:

```
systemctl enable cloud-initcloud-init clean
```

### Cloud-Init

_ **Cloud-init** _ is a project managed by Canonical to standardize VM customization on boot, making IaaS more "cloudy", regardless of hosted location. It is structured to receive configuration data from a datasource and abstracts the specific inputs from other "clouds" to the IaaS workload (VM) as consistent instructions. The customization software will use these data sources as "drop points" to transform the cloud-specific instructions (OVF, Azure, EC2) to a common configuration (Metadata, Userdata).

_ **metadata** _ should represent the workload's system configuration, like hostname, network configuration, and mounts.

_ **userdata** _ should represent the workload's user space configuration, like Ansible playbooks, SSH keys, and first-run scripts. With the current state, I would tend towards using automation to register a workload with Ansible and perform that configuration centrally. It's neat that this level of customization is offered, though - _ **cloud-init** _ can automatically register with centralized orchestrators like SaltStack and Puppet on startup.

_ **cloud-init** _ has a ton of goodness available as boot-time customization, and this will only scratch the surface of how it can be used. _ **cloud-init** _ accepts a YAML configuration that can include:

- Users/Groups
- CA certificates
- SSH keys
- Hostnames
- Packages/Repositories
- Ansible Playbooks
- External mounts (NFS)

VMware offers two data sources for workloads provisioned on vSphere:

- OVF (the scope of this post)
- Direct Metadata ([VMware Documentation](https://developer.vmware.com/docs/14558/vsphere-web-services-sdk-programming-guide--7-0-update-3-/GUID-CBB197F1-9D5B-4738-9FA7-3736ECA162F0.html))

VMware's new RESTful API has built-in documentation. From the vSphere GUI, select the triple ellipsis and select "Developer Center":

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDfEpkKKla5N2AIt8I92b6KlF10eXPLi9cVJqOPlRoojeN1n0KtpUcHadFBFzXTP4CMXxYMVAO-62fdWk6h-8JGJLD8HyqWMpdhotD9zts3hYA1pJ1lBYGMfHJBjy5mjaFS-zlgzCqC7QjYHm4Izw0o-Gzggldhkrj_bY7dzlX4Xm0NVSQTVobelKi/w640-h170/2022-09-22%2015_16_50-vSphere%20-%20API%20Explorer%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDfEpkKKla5N2AIt8I92b6KlF10eXPLi9cVJqOPlRoojeN1n0KtpUcHadFBFzXTP4CMXxYMVAO-62fdWk6h-8JGJLD8HyqWMpdhotD9zts3hYA1pJ1lBYGMfHJBjy5mjaFS-zlgzCqC7QjYHm4Izw0o-Gzggldhkrj_bY7dzlX4Xm0NVSQTVobelKi/s979/2022-09-22%2015_16_50-vSphere%20-%20API%20Explorer%20%E2%80%94%20Mozilla%20Firefox.png)

**Unfortunately,** VMware's new [metadata source](https://github.com/vmware-archive/cloud-init-vmware-guestinfo) does not appear to function with this distribution. According to Canonical's changelog, _ **cloud-init** _ Version **21.3+** is required to recognize the new datasource. I tested with OpenSUSE 15.4 (Ships with _ **cloud-init 21.4** _) and received the following error:

```
# A new feature in cloud-init identified possible datasources for ## this system as: ## [] ## However, the datasource used was: OVF ## ## In the future, cloud-init will only attempt to use datasources that ## are identified or specifically configured. ## For more information see ## https://bugs.launchpad.net/bugs/1669675 ## ## If you are seeing this message, please file a bug against ## cloud-init at ## https://bugs.launchpad.net/cloud-init/+filebug?field.tags=dsid ## Make sure to include the cloud provider your instance is ## running on. ## ## After you have filed a bug, you can disable this warning by launching ## your instance with the cloud-config below, or putting that content ## into /etc/cloud/cloud.cfg.d/99-warnings.cfg ## ## #cloud-config ## warnings: ## dsid_missing_source: off # **************************************************************************
```

To view the provided and applied _ **metadata** _ for a system, _ **cloud-init** _ provides the following file handle:

```
/run/cloud-init/instance-data.json
```

To view the _ **userdata** _ for a system, use the following command:

```
cloud-init query userdata
```

This indicates that we probably have an upstream issue with the new data source type. Reviewing the [change log](https://github.com/canonical/cloud-init/blob/22.3.1/ChangeLog) we see several fixes applied to this data source. 

### Applying Workload Templates

**Note: This feature is only available on vSphere 7 and up!**

Here's how to leverage the OVF data source with vSphere and OpenSUSE.

The flag `disable_vmware_customization` is a directive that functions as a switch to choose between the metadata source and the OVF data source. following to `/etc/cloud/cloud.cfg:`

```
disable_vmware_customization: falsedatasource: OVF: allow_raw_data: truevmware_cust_file_max_wait: 25
```

Once installed, shut the virtual machine down. Right-click on the VM, and select Clone -> Clone as Template to Library:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWOnpg1KcAsm3GJPEUkB81ZqBXSxpUaKRLc512jTy92qSsOhZlZRPEUKp38SdmIQUYRVvM-j31m5qslzCs-CnJyPWcrZi77hquso2YGpSmlzyKzixQ2G0-rIYu5kYJFp1NwjxHXJdU3_mwkt6GLofmo0igOQF1hh-Bfbg5mmTw63XOfLl2esCefCKJ/w640-h428/2022-09-11%2017_33_33-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWOnpg1KcAsm3GJPEUkB81ZqBXSxpUaKRLc512jTy92qSsOhZlZRPEUKp38SdmIQUYRVvM-j31m5qslzCs-CnJyPWcrZi77hquso2YGpSmlzyKzixQ2G0-rIYu5kYJFp1NwjxHXJdU3_mwkt6GLofmo0igOQF1hh-Bfbg5mmTw63XOfLl2esCefCKJ/s502/2022-09-11%2017_33_33-vSphere%20-%20rancher-base%20-%20Summary%20%E2%80%94%20Mozilla%20Firefox.png)

This vCenter feature will orchestrate the conversion to a template object and publish it to a Content Library as one step.

### Deploying a customized machine

The next process needs to be executed via vCenter's Content Library vSphere API:

- Establish API Session Key (required authentication for the endpoints used to deploy)
- Deploy Content Library Object (`/api/vcenter/vm-template/library-items/`)

  - Find the correct content library
  - Find the correct content library item
  - Find the content library item via the `vsphere` API (ID to use in deployment command)
  - Find vSphere Cluster
  - Find vSphere Folder
  - Find vSphere Datastore
  - Deploy Content Library Item
- Wait until deployment is complete, periodically checking to see if it's complete

  - Normally, an API will respond immediately that the command was successful, and subsequent calls would be required to validate readiness. Instead, vSphere's RESTful API responds with a `200` response only **if and when the deployment is complete** , which simplifies our code
- Locate the Virtual Machine. The previous API call responds with a `200 OK`, and Postman conveniently times the operation for you as well!
- Apply Guest Customization
- Start VM

To replicate this lab, the Postman Environment and Collection will be provided at the bottom of this post. Postman provides a powerful platform to educate engineers unfamiliar with a particular API by expanding the behaviors an HTTP client may have. Automated processes are typically very terse, and do not effectively explain each step and behavior. To import this collection and environment, download the files, and import them:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVnS1Bf5f7fuV5qXQ8UjdB-iU6EsZpxUmB-PwW_-pimnqHzxgVLmFhAxnWBmMtuuJHMAmhiURhCilt--bCLsfNNMJpLap-IYZSI60aBoPIN_UxjV57WHGsUpGHOvDQEL9eAn0hV-MGeQRndso-jnGyum8lidudfrr7Qslzgiz5XLgnB9-LLM3TwYGb/w400-h275/2022-09-22%2016_03_50-Postman.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVnS1Bf5f7fuV5qXQ8UjdB-iU6EsZpxUmB-PwW_-pimnqHzxgVLmFhAxnWBmMtuuJHMAmhiURhCilt--bCLsfNNMJpLap-IYZSI60aBoPIN_UxjV57WHGsUpGHOvDQEL9eAn0hV-MGeQRndso-jnGyum8lidudfrr7Qslzgiz5XLgnB9-LLM3TwYGb/s376/2022-09-22%2016_03_50-Postman.png)

Postman Environments will stage variables for consumption by Collections.

I have sorted the Postman Collection based on the order of execution. The final customization step will return a 204 if successful, with an empty body. To verify that the configuration was correctly applied, browse to the individual VM in vCenter, and look under **Monitor -> Events** for an event of the type "Reconfigure VM". If you see the task on the correct VM, **start** it, and you will see the following:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4jXP8IqGVJGx69NHoIcz4F5jy41VAxQd0kOaTIsc7WkUMuqPWyMC4woN08gT-CQlj9MfYanw6OrRNzAf8uLhByjcvS-ZbTchjme-HvqkitUx5xDfcTAae9xDqp8DcphEC9m_L4Mi8cz9_pXECcHHD9JD59S6AnGW9U0wC6ojJOdmL-EziVpTI8dWc/w640-h476/2022-09-22%2016_43_44-vSphere%20-%20fizz_buzz%20-%20Events%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4jXP8IqGVJGx69NHoIcz4F5jy41VAxQd0kOaTIsc7WkUMuqPWyMC4woN08gT-CQlj9MfYanw6OrRNzAf8uLhByjcvS-ZbTchjme-HvqkitUx5xDfcTAae9xDqp8DcphEC9m_L4Mi8cz9_pXECcHHD9JD59S6AnGW9U0wC6ojJOdmL-EziVpTI8dWc/s852/2022-09-22%2016_43_44-vSphere%20-%20fizz_buzz%20-%20Events%20%E2%80%94%20Mozilla%20Firefox.png)

Soon after, look at the virtual machine to review its customized attributes!

### Debugging/Troubleshooting Tips

This process is slightly opaque, and a little confusing at first. Here are some key points for troubleshooting, and the methods to manage it:

- The vSphere `/vm/guest/customization` URI will only respond with a `204` if working correctly.

  - If it returns a 400, the error will indicate what part of the JSON spec is having issues. Keep in mind that it may only give you the parent key - tools like [JSONLint](https://jsonlint.com/) offer a method to quickly validate payloads as well
- When locating resources, the Content Library and Templates are returned as a UUID with no description. `GET` the individual objects to match with names, or use the find API
- All other resources (datastore, VM name) are listed with their MOB name, e.g. `domain-c1008`
- Save the response from the deployment action, it has the VM ID when it finally completes
- VM Customization can only be applied to a VM that is `OFF`, and doesn't customize until the VM starts.

Troubleshooting customization after boot can be done by viewing the metadata (`/run/cloud-init/`) or by reviewing logs at the following locations:

```
/var/log//var/log/vmware/imcjournalctl -xesystemctl restart cloud-init
```

The classic "wipe and restart" method is also quite valuable:

```
cloud-init clean -l -ssystemctl restart cloud-init
```

Finally, after a host is successfully configured, I'd recommend **disabling cloud-init** to prevent further customization. This is just as easily achieved with an Ansible playbook

```
systemctl disable cloud-init
```

### [Code](https://gist.github.com/ngschmidt/e5e25dac60ffc23479c03c9b487f2ebd)
<script src="https://gist.github.com/ngschmidt/e5e25dac60ffc23479c03c9b487f2ebd.js"></script>