2 Replies Latest reply on May 25, 2020 5:38 AM by lordZZ

    [VRA8.1] Setting hostname using cloud-base init

    lordZZ Lurker

      Hi guys ,

      I'm trying to make Windows deploments work in our POC of VRA8.1 and I struggle with hostname configuration.

      In my case I CANNOT use the vsphere customization. We have some very specific cases where sysprep is not something we want to run.

      I've put together the following blueprint which uses DHCP for IP assignment and cloud-config for hostname setup.

       

      The issue is with deployment where user requests multiple VMs which is a standard case in our env.

      I cant simply use : set_hostname: ${resource.Cloud_vSphere_Machine_1.resourceName[0]}

      If I do that deployment fails.

       

      So I've put together custom ABX action which captures the deployment details and updates the cloud-config section according to the resourcename.

      I've tried to subscribe this action to compute.provision.pre event . The cloud config is actually updated, I can see that in deployment details, but the VM is deployed with the original cloud config, therefore all vms in deployment end up with hostname temphostname.

       

      I found out that making changes when compute.provision event is fired is too late so I changed it to Compute.allocation.pre event. It turned out that this is the right place to update the cloud config, my changes to it were reflected into VMs but this event is fired for the whole deployment, not each resource.

      The event contains resourcenames for all resources, but there is only one cloud-config definition, so any changes I do here are applied to all VMs within this deployment.

      As a result I have all VMs within the deployment using the same hostname.

       

      Is there anyone who could help me as I' not sure how to proceed any further?

       

       

      formatVersion: 1
      inputs:
        vmcount:
          type: integer
          minimum: 1
          maximum: 5
          default: 1
          title: VM count
          description: How many VMs you get in a single deployment
        imagename:
          type: string
          oneOf:
            - title: Server 2019 Core
              const: w2k19cbi
        cpus:
          type: integer
          minimum: 1
          maximum: 16
          default: 2
          title: Number of CPUs
        memgb:
          type: integer
          minimum: 1024
          maximum: 16384
          default: 2048
          title: Memory (GB)
          description: 'Min: 1024 Max: 16384'
      resources:
        Cloud_vSphere_Machine_1:
          type: Cloud.vSphere.Machine
          properties:
            count: '${input.vmcount}'
            snapshotLimit: 2
            folderName: 'vRA-8.0-PoC/Deployments/${env.projectName}/${input.imagename}'
            image: '${input.imagename}'
            cpuCount: '${input.cpus}'
            totalMemoryMB: '${input.memgb}'
            networks:
              - network: '${resource.Cloud_NSX_Network_1.id}'
            cloudConfig: |
              #cloud-config
              write_files:
                content: test text
                path: c:\test.txt
              set_hostname: temphostname
        Cloud_NSX_Network_1:
          type: Cloud.NSX.Network
          properties:
            networkType: existing
            constraints:
              - tag: testnet
      

       

       

      function handler($context, $payload) {
          
          #write-host "display all"
          #$payload | Format-Custom -depth 3
          
          $generatedHostname = $payload.resourcenames[0]
          write-host "Hostname to be configured : $generatedHostname"
          
          $oldCLoudConfig = $payload.customproperties.cloudConfig
          write-host "This is old cloud config : $oldCLoudConfig"
          
          $newCLoudConfig = $oldCLoudConfig.replace('temphostname',$generatedHostname)
          
          write-host "This is new cloud config : $newCLoudConfig"
          
          $payload.customproperties.cloudConfig = $newCLoudConfig
          
        return $payload
      }
      
        • 1. Re: [VRA8.1] Setting hostname using cloud-base init
          eoinbyrne Expert

          First off I have to admit that I have not worked with vRA 8.x yet but have done lots of this sort thing with 6.x and 7.x so I'm making some assumptions but I reckon they should be logically sound here.

           

          Anyway, if as you say the Event you have selected fires for the deployment then maybe you need to consider chaining that with the first event you selected like this

           

          - compute.allocation.pre

          Add code to your ABX to compute N distinct hostnames where N = VM count and store them in new entries in the custom properties

           

          - compute.provision.pre

          I'd expect this to fire for each node where VM count > 1 so there should be some way to use a second ABX to process the event for each node and then have each assign one of the distinct hostnames you computed in the first ABX

           

           

          I've done exactly this before in 7.6 using a vRO workflow and an EventBroker subscription to handle N copies of the same Blueprint and ensure they all get unique names. While the architecture is very different in 8.x the logical flow of provisioning is still the same.

           

           

          HTH

          • 2. Re: [VRA8.1] Setting hostname using cloud-base init
            lordZZ Lurker

            The thing is that actually I can modify hostname in the compute.provision.pre but it seems to be to late. The configuration in custom properties is updated but the actuall cloud config that is passed to the VM is based on the old , unmodified config.

             

            It seems to me that the cloud config which is passed to VM is created when compute.allocation.pre is fired. But this is defined for the whole deployment.

            If I modify the custom properties in later stages, and I've successfully done that, it's ignored.