VMware {code} Community
johnklassa
Contributor
Contributor

applying a host profile to an ESX host

Having trouble doing this, likely because I don't understand quite what I'm doing.  From the UI, you can associate a profile with a cluster.  Then, you can right-click on a host within the cluster and do "Apply Profile".  Works great, and requires no extra inputs.  From the perl SDK, it's not so easy.

From the docs, it looks like it's two steps: (a) ExecuteHostProfile followed by (b) ApplyHostConfig_Task.

So, my code looks like this.  First I look up the profile, by name:

     my $sc = Vim::get_service_content();
     my $hostProfileManager = Vim::get_view(mo_ref => $sc->hostProfileManager);
     my $profiles = $hostProfileManager->profile;
     my $profile;
     for my $ref (@{ $profiles }) {
          my $obj = Vim::get_view(mo_ref => $ref);
          if ($obj->name eq $profileName) {
               $profile = $obj;
          }
     }

     die unless defined $profile;

then I apply it like so:

     my $res = $profile->ExecuteHostProfile(host => $host);

     my $taskRef = $hostProfileManager->ApplyHostConfig_Task(host => $host, configSpec => $res->configSpec);

Using the 4.x API, this fails with "Cannot apply the host configuration".  If I take the contents of $res->requireInput (which contains two items) and feed it back into ExecuteHostProfile as a "deferredParam", the resulting configuration doesn't seem to change...  I continually get the same two items back in $res->requireInput, regardless how many times I feed them back tino ExecuteHostProfile.

If I use the 5.x API in order to get access to the "userInput" parameter in the ApplyHostConfig_Task call, and pass the same $res->requireInput via that parameter like so:

     my $taskRef = $hostProfileManager->ApplyHostConfig_Task(host => $hostObject, configSpec => $res->configSpec, userInput => $res->requireInput);

I get this instead:

     Can't call method "serialize" on unblessed reference at .../vsphere-sdk-5.0/lib/VMware/share/VMware/VICommon.pm line 2435.

Can anybody shed some light on how this is supposed to work, and what I might be doing wrong?

11 Replies
johnklassa
Contributor
Contributor

In case anybody else runs into this, it appears to be a known issue (i.e. the code presented in the original post isn't wrong; there's a bug in VMware).

Reply
0 Kudos
loooolaaaaa
Contributor
Contributor

johnklassa,

were you able to overcome the issue? same issue is seen when i run  ApplyHostConfig_Task and GenerateConfigTaskList.

Thanks

Reply
0 Kudos
johnklassa
Contributor
Contributor

Nope, no resolution...  Still waiting on VMware engineering to come up with a fix.

Reply
0 Kudos
loooolaaaaa
Contributor
Contributor

Have you reported this to VMware? apart from posting it in forum?

Thanks

Reply
0 Kudos
johnklassa
Contributor
Contributor

Yes.  I have an "official" case open, and it's being worked...

Reply
0 Kudos
loooolaaaaa
Contributor
Contributor

This issue is with vSphere SDK for Perl only right? because from PowerCLI and vSphere Client, i am able apply the profile. do we have any workaround? ( on i think of using powerCLI to apply the profile)

Thanks

Reply
0 Kudos
johnklassa
Contributor
Contributor

I don't know where all the problem occurs, as I haven't tried anything but perl.  So, I'm not aware if there is (or isn't) a workaround.  VMware suggested I use Java, but the software environment in which I need this functionality isn't well suited to it.

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Are you checking the 'status' property of your ProfileExecuteResult.  The configSpec will only be valid if it's 'success'.  You're probably getting 'needInput' as the status.

--Just saw you hit that, let me look into a bit more.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Ok, I found the problem.

In the WSDL, KeyAnyValue's property 'value' is defined as xsd:anyType, but is not defined as an array.  Similarly, the KeyAnyValue class in the Perl toolkit has the same configuration.

The host profile is spitting out KeyAnyValue with value => array in several places.  The first I found was in the SNMP Agent setting (community for example).  The next I found was in the NetworkProfile nicNames.  If you edit these in the HostProfile by blanking them out, they won't populate in the SOAP data as an array.  However, that's probably not the right fix for this.

You may have this problem with the other SDK kits as well, depends on how they handle anyType as an array (which isn't specified as an array in the VIM WSDL).

As for why this doesn't occur in PowerCLI, could be how they handle that serialization vs Perl's toolkit.  I'll leave it upto someone who knows SOAP/WSDL better than I to debate on the proper syntax for xsd:anyType with no minOccurs/maxOccurs values. Smiley Happy

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Ok, I mucked around with this for a bit.

I think there two issues here:

1. VIM25Stub.pm defines KeyAnyValue->value as a single value.  As a very unsupported workaround, the KeyAnyValue definition in VIM25Stub.pm was changed to:

##################################################################################
package KeyAnyValue;
our @ISA = qw(DynamicData);
our @property_list = (
   ['key', undef, undef, 1],
   ['value', 'anyType', 1, 1],
);
VIMRuntime::make_get_set('KeyAnyValue', 'key', 'value');
sub get_property_list {
   my $class = shift;
   my @super_list = $class->SUPER::get_property_list();
   return (@super_list, @property_list);  
}
1;
##################################################################################
Particularly the change is in the ['value', 'anyType', undef, 1] to ['value', 'anyType', 1, 1].  That change turns the value property into an array.  Without this change, the SOAP module will toss out parser errors.
2. The SDK HostProfile interface seems sensitive to the xsi:type declaration.  The ComplexType::serialize code in VICommon.pm basically falls back to 'xsi:string' for any non-specific values. Without the fix below, I was getting %d format expected number not string errors.  In this case, I don't believe it was related to the KeyAnyValue issue, but just a more strict type check than normally expected from the Perl SDK.
          if ($class_name ne $obj_type_name) {              
               $show_type = 1;
               if ($class_name eq 'anyType' &&  ! $obj_type_name) {
                  # primitive going out as any -- treat them as xsd:string
                  if (defined ($_) && isdigit($_)) {
                     $serialized_string .=      
                        "<$property_name xsi:type=\"xsd:int\">" .
                        XmlUtil::escape_xml_string($_) . "</$property_name>";
                  } elsif (defined ($_)) {
                     $serialized_string .=      
                        "<$property_name xsi:type=\"xsd:string\">" .
                        XmlUtil::escape_xml_string($_) . "</$property_name>";
                  }
                  next;
               }
            }

I used a simple Login Banner + Advanced Configuration Option enabled HostProfile with these changes successfully.

I've attached the script I used with the simple profile setting.  To run it, you'll need to CPAN install 'Sub::Override'.  It's a source module IIRC, so you can copy and paste it into a module file in your path if CPAN isn't an option.

> profiletest.pl --username=administrator --password=****** --server=172.16.254.50 --host=vlab5-esx-03.vlab5.local --profile=VLAB5-P2

I don't check the task status in the script, I did validations visually in the vCenter Client UI.

I'm not sure it will be of much use, particularly with the modification to the VIM25Stub.pm file.  Since it's going to require some more robust determination of the xsi:type in the serialization functions in the toolkit, it will need some testing before being production worthy. 

Also, just to feed some information.  I did manage to get one of my hosts in a state where I get general errors anytime I try to apply a HostProfile (even a simple one).  I can't seem to recover it, and it was the result of a applying a very complex HostProfile with some input answers.  It's possible this xsi:type issue caused the problem, but I feel the HostProfile system shouldn't be that easily laid out.  It may be a related bug.

Reuben Stump | http://www.virtuin.com | @ReubenStump
thale13
Contributor
Contributor

Does anyone know if this bug ever got resolved?  I am trying to do the same thing (roughly) and am getting the same serialize error.

T.

Reply
0 Kudos