Use ‘for’ Loops with the vSphere Management Assistant

by Bob Plankers on January 22, 2012 · 1 comment

in Virtualization

The VMware vSphere Management Assistant (vMA) claim to fame is that it has a UNIX shell and the vSphere CLI installed, making it handy for a lot of things, and makes cutting & pasting comands real easy when it’s paired with a decent SSH client. One of my favorite ways to use it is with ‘for’ loops in the shell, to make the same change to all of my ESXi hosts.

Let’s say you have a list of servers you want to make a change to, like using esxcli to set the HBA queue depth. My list is a text file I create in nano or vi (see my post on installing nano on the vMA), one host per line:

I will refer to this file as “FILEWITHALLYOURHOSTSINIT” below. The default shell on the vMA is called ‘bash’ (it’s the Bourne Again SHell, the original Bourne shell was just ‘sh’ and wasn’t open source). In bash you can feed the contents of the file into a for loop. Just type this in:

echo $NAME
esxcli -s $NAME -u root -p PASSWORD system module parameters list -m qla2xxx

A few points here. First, you can also collapse this into a one-liner, either by writing it with semicolons or just using the up arrow after running it once to see how bash collapses it in the history (try typing ‘history’ to get a list of all your commands):

for NAME in `cat FILEWITHALLYOURHOSTSINIT`; do echo $NAME; esxcli -s $NAME -u root -p PASSWORD system module parameters list -m qla2xxx; done

Second, you can change NAME to whatever you want, that’s just the variable that is created to hold the line-by-line output of the cat command there. Obviously you then have to update $NAME in the rest of the command…

Third, the ‘cat’ command is short for conCATenate. Given a list of files it’ll output them all, then you can redirect the output to another file. Give it one file and it’ll spit just that file out. Try it on another file, like “cat /etc/passwd” and see how it works. There’s also the ‘less’ command which will page output (like ‘more’ does on other OSes, but ‘more’ wasn’t open source, so they rewrote it and called it ‘less’). Try “cat /etc/passwd | less”

Fourth, if you don’t echo $NAME it’ll be hard to know which host you’re talking to if something goes wrong, especially with big lists.

Fifth, the `cat FILEWITHALLYOURHOSTSINIT` — those are backticks, not single quotes. Backticks tell the shell to execute that command and use the output from it in the next stage (the for loop in this case).

Sixth, if you omit the line with the esxcli command in my example above you can try it without actually doing anything.

Seventh, this is in no way a vMA-only trick, it’ll work on any system with bash installed. In fact, most shells have a way to do this, and a little Google action will get you the right syntax.

Last, this is a great way to do things fast, including screwing stuff up. With great power comes great responsibility. BE CAREFUL.

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: