1) How to automate a particular script and leverage vi-fastpass without having to change the context for each ESX or ESXi host
2) How to automate a particular script and leverage vi-fastpass via cron without having to provide username/password
This alias is defined in /etc/bashrc
You can even redefine the alias name to whatever you like, so long as it's aliasing the actual vifptarget script. Properly sourcing vifptarget script is the key in solving Question 1. If you look at /etc/bashrc, you will also notice that LD_LIBRARY_PATH environmental variable for shared libraries is also defined, this will be the key in solving Question 2 along with properly sourcing vifptarget. You will see these referenced in the example scripts.
There are two solutions for Question 1:
Solution A:
If you are leveraging vCenter Server and the ESX(i) hosts are being managed by vCenter, then you just need to add vCenter to vi-fastpass and run a simple for loop across the hosts.Example:
2 x ESXi 4.1 hosts being managed by vCenter
- esx4-4.primp-industries.com
- esx4-5.primp-industries.com
- ntp1.ucsb.edu
- ntp2.ucsb.edu
example1.sh
Download: example1.sh
Solution B:
In this solution, vCenter is not available or you each ESX(i) host being managed by vMA's vi-fastpass. The script will be very similar, but instead of setting vCenter as the target, you will be setting an ESX(i) host. What is interesting that may not be well known is if you are managing more than one ESX(i) vi-fastpass target, you only need to set the target once and you can perform the same operation on all other host. The caveat being, if you want to switch out of the default context of your target, you need to specify --server parameter. This script will actually do it for you behind to scenes. Example:
2 x ESXi 4.1 hosts being managed by vMA
- esx4-4.primp-industries.com
- esx4-5.primp-industries.com
- ntp1.ucsb.edu
- ntp2.ucsb.edu
example2.sh
Download: example2.sh
After executing either SolutionA or SolutionB, you now should have the configurations applied to your ESX(i) host as you would if you manually ran it on each host. This is just an example, you can easily substitute multiple esxcfg-* and/or vSphere SDK for Perl scripts. Here is also another example script for a user on the VMTN forums on configuring a vSwitch and adding portgroups.
Solution for Question 2:
Now that we know how to automate a particular operation using vi-fastpass in a script, the next logical question is how do we automate it using a cronjob. I will not go into details of how cron works, take a look at the link for more details. One thing I will mention is that cron does not have all the same PATH environmental variables defined as you normally would expect. You should always use full path to scripts and binaries and any shared library modules that is required to execute a particular utility. What this means for leveraging vi-fastpass via a cronjob is that you need to specify the LD_LIBRARY_PATH. In solving Question 1, all we had to do was properly source the vifptarget and the reason we did not have to specify the shared library path is that it was already defined as a default for vi-admin user. This is not the case for cron and you will need to specify that in either the script you are executing or within the crontab.
Example:
2 x ESXi 4.1
- esx4-4.primp-industries.com
- esx4-5.primp-industries.com
example3.sh
Download: example3.sh
After 5 minutes, you should now see a new file under /tmp/vms that contains all virtual machines registered on your ESX(i) host. Again, you can easily apply this to any other vCLI and/or vSphere SDK for Perl script.
Here another example that gets asked quite often in scheduling the popular VMware vSphere Health Check Script, here is the crontab entry for automatically running the healthcheck script every hour against your vCenter server.
example4.sh
Download: example4.sh
Again, the key is to make sure you specify the full path to not only the script you would like to run but also if there is any type of output, that you specify the full path to the output.






Thank you this really helped me with PCNS
ReplyDeleteIs there a way to embed vifptarget in a perl script? I've not had any luck...
ReplyDelete