Highlighted
Enthusiast
Enthusiast

vMA scripting wonders

Jump to solution

Ok, so I have been working on scripting from the vMA. I am pretty familiar with what you can and cannot do with it, but this is my first attempt at scripting a complete configuration. One thing I am having an issue with seems like a simple concept, but is stalling. For example, 2 hosts are already added to the vMA. When I try something simple like:

#!/bin/bash

vifptarget -s esx1

esxcfg-vswitch -L vmnic1 vSwitch0

vifptarget -s esx2

esxcfg-vswitch -L vmnic1 vSwitch0

What happens is that I still get asked for username and password. If I have already vifpassed to esx1 then the command runs (even though i get weird response about not being to find vifptarget), twice on esx1 I might add.

Is there something I don't know that is required when scripting with vifpass? I thought it sounded simple to script with the changing of several hosts.

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Community Manager
Community Manager

As I mentioned in the last reply, you can get it to work but it's not ideal, at least for me personally. If you're going to use vMA's fastpass library which is what this is all using with vifptarget and vifp, I would prefer to create a script that utilizes the fastpass module directly which means creating a Perl or Java utility. This is definitely not for beginners, but with regards to your original question, yes it can be done.

I'm going to assume you're using vMA 4.1, if you are not, recommend upgrading.

You should easily be able to convert this example into what you need to implement but I will leave that as an exercise for you.

My scenario is the following, I have two ESX 4.1 hosts which I need to add a new vSwitch called vSwitch2 and add a new portgroup called AppPG with VLAN ID 1234

Here is my configuration file:

[vi-admin@tancredi ~]$ cat config
esx4-1.primp-industries.com:vSwitch2:AppPg:1234
esx4-2.primp-industries.com:vSwitch2:AppPg:1234

Here is a very simple shell script that I've wrapped all the commands and parsed the configuration file:

#!/bin/sh

if [ $# -ne 1 ]; then
        echo "Please provide configuration input file"
        exit 1
fi

CONFIG=$1

FIRST_HOST=$(head -1 ${CONFIG} | awk -F ":" '{print $1}')
source /opt/vmware/vma/bin/vifptarget -s ${FIRST_HOST} > /dev/null 2>&1

if [ $? -eq 0 ]; then
        IFS=$'\n'
        for LINE in $(cat ${CONFIG});
        do
                VIHOST=$(echo ${LINE} | awk -F ":" '{print $1}')
                VSWITCH=$(echo ${LINE} | awk -F ":" '{print $2}')
                PORTGROUP=$(echo ${LINE} | awk -F ":" '{print $3}')
                VLAN=$(echo ${LINE} | awk -F ":" '{print $4}')
                echo "Creating new vSwitch ${VSWITCH} on target ${VIHOST}"
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -a ${VSWITCH}
                echo "Adding new portgroup ${PORTGROUP} with VLAN ${VLAN}"
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -A ${PORTGROUP} ${VSWITCH}
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -p ${PORTGROUP} -v ${VLAN} ${VSWITCH}
                echo
        done
        unset IFS
else
        echo "Unable to intialize vi-fastpass on target ${FIST_HOST}"
        exit 1
fi

One thing I would like to point out, you can't execute "vifptarget" from within a script, the reason is there is an alias associated with that command and you need to specify the actual command else you will get an error trying to locate the command. You can see this alias definition by running the following command:

[vi-admin@tancredi ~]$ which vifptarget
alias vifptarget='source /opt/vmware/vma/bin/vifptarget'

As you can see from the above, the actual utility is located in /opt/vmware/vma/bin and it is being sourced when you call vifptarget

Here is an example execution of the above script:

[vi-admin@tancredi ~]$ ./script.sh config
Creating new vSwitch vSwitch2 on target esx4-1.primp-industries.com
Adding new portgroup AppPg with VLAN 1234

Creating new vSwitch vSwitch2 on target esx4-2.primp-industries.com
Adding new portgroup AppPg with VLAN 1234

As mentioned in the previous reply, you only need to initialize a single fastpass target, and so long as you specify --server for other targets, you can be in the same fastpass context and issue commands w/o further credentials to other targets.

Hopefully this examples makes sense and give you a place to start. This is probably the easiest method to utilize in terms of automating operatons using vi-fastpass. I most likely will provide a blog article regarding this method but also a more ideal method which directly uses the vifpass library, I'll leave that example for another day.

=========================================================================

William Lam

VMware vExpert 2009,2010

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
5 Replies
Highlighted
Community Manager
Community Manager

When using vifptarget where you initialize a given fastpass target is really meant for interactive sessions versus scripted sessions. It can work but it's not ideal and there is definitely a better way. Another thing is if you add 5 targets into vMA's management using vifp and you set the target on esx1 and you perform an operation such as adding a vSwitch and now you want to add a vSwitch to another target, you actually do not need to set the new target. I'm not sure if this is a fluke but basically you just need to initialize 1 target and so long as you specify the target by using "--server", it will utilize the fastpass credentials.

There's a few way to automate in vMA, I'll describe one method as the second requires some comfort around Perl or Java and for beginners it can be confusing.

Take a look at my blog post here regarding "mcli" - http://www.virtuallyghetto.com/p/vmware-vma-vima.html

If you wanted to use one of the vCLI's esxcfg-* commands and say you wanted to create a vSwitch on 5 hosts. You can use mcli which allows you to specify a list of ESX(i) host in a file, a specific esxcfg-* command and the options and allow you to execute the command across all hosts under vMA management. If you're on VIMA 1.0 or vMA 4.0, you will need to initialize a single target using "vifpinit ". Then you can use mcli to perform the common operation against n-hosts.

Hopefully this will get you started.

=========================================================================

William Lam

VMware vExpert 2009,2010

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos
Highlighted
Enthusiast
Enthusiast

Wow, I guess I need to replan my method of attack for building new environments. I was hoping to build a custom config script that could configure any machine by parsing the data that would become variables from a file. Will this work as long as specify --server for the commands instead of using the interactive method?

vifptarget -s $(host1)

esxcfg-vswitch -L $(nic1) vSwitch0

esxcfg-vmknic -a -i $(vmk0) -n 255.255.255.0

-


flat file example

esx1:vmnic1:10.0.0.5

I was hoping to have one script that could configure any host by setting the variables from the flat file.

0 Kudos
Highlighted
Community Manager
Community Manager

As I mentioned in the last reply, you can get it to work but it's not ideal, at least for me personally. If you're going to use vMA's fastpass library which is what this is all using with vifptarget and vifp, I would prefer to create a script that utilizes the fastpass module directly which means creating a Perl or Java utility. This is definitely not for beginners, but with regards to your original question, yes it can be done.

I'm going to assume you're using vMA 4.1, if you are not, recommend upgrading.

You should easily be able to convert this example into what you need to implement but I will leave that as an exercise for you.

My scenario is the following, I have two ESX 4.1 hosts which I need to add a new vSwitch called vSwitch2 and add a new portgroup called AppPG with VLAN ID 1234

Here is my configuration file:

[vi-admin@tancredi ~]$ cat config
esx4-1.primp-industries.com:vSwitch2:AppPg:1234
esx4-2.primp-industries.com:vSwitch2:AppPg:1234

Here is a very simple shell script that I've wrapped all the commands and parsed the configuration file:

#!/bin/sh

if [ $# -ne 1 ]; then
        echo "Please provide configuration input file"
        exit 1
fi

CONFIG=$1

FIRST_HOST=$(head -1 ${CONFIG} | awk -F ":" '{print $1}')
source /opt/vmware/vma/bin/vifptarget -s ${FIRST_HOST} > /dev/null 2>&1

if [ $? -eq 0 ]; then
        IFS=$'\n'
        for LINE in $(cat ${CONFIG});
        do
                VIHOST=$(echo ${LINE} | awk -F ":" '{print $1}')
                VSWITCH=$(echo ${LINE} | awk -F ":" '{print $2}')
                PORTGROUP=$(echo ${LINE} | awk -F ":" '{print $3}')
                VLAN=$(echo ${LINE} | awk -F ":" '{print $4}')
                echo "Creating new vSwitch ${VSWITCH} on target ${VIHOST}"
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -a ${VSWITCH}
                echo "Adding new portgroup ${PORTGROUP} with VLAN ${VLAN}"
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -A ${PORTGROUP} ${VSWITCH}
                /usr/bin/esxcfg-vswitch --server ${VIHOST} -p ${PORTGROUP} -v ${VLAN} ${VSWITCH}
                echo
        done
        unset IFS
else
        echo "Unable to intialize vi-fastpass on target ${FIST_HOST}"
        exit 1
fi

One thing I would like to point out, you can't execute "vifptarget" from within a script, the reason is there is an alias associated with that command and you need to specify the actual command else you will get an error trying to locate the command. You can see this alias definition by running the following command:

[vi-admin@tancredi ~]$ which vifptarget
alias vifptarget='source /opt/vmware/vma/bin/vifptarget'

As you can see from the above, the actual utility is located in /opt/vmware/vma/bin and it is being sourced when you call vifptarget

Here is an example execution of the above script:

[vi-admin@tancredi ~]$ ./script.sh config
Creating new vSwitch vSwitch2 on target esx4-1.primp-industries.com
Adding new portgroup AppPg with VLAN 1234

Creating new vSwitch vSwitch2 on target esx4-2.primp-industries.com
Adding new portgroup AppPg with VLAN 1234

As mentioned in the previous reply, you only need to initialize a single fastpass target, and so long as you specify --server for other targets, you can be in the same fastpass context and issue commands w/o further credentials to other targets.

Hopefully this examples makes sense and give you a place to start. This is probably the easiest method to utilize in terms of automating operatons using vi-fastpass. I most likely will provide a blog article regarding this method but also a more ideal method which directly uses the vifpass library, I'll leave that example for another day.

=========================================================================

William Lam

VMware vExpert 2009,2010

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
Highlighted
Enthusiast
Enthusiast

Thanks, William. That helps a lot. I want to have the ability to rebuild any one of them and do the config. This is a great start.

P.S. I was specifying the absolute path, but do you think it wasn't working because the alias is for "source " ?

0 Kudos
Highlighted
Community Manager
Community Manager

Yes, you need to specify the full alias command

=========================================================================

William Lam

VMware vExpert 2009,2010

VMware scripts and resources at:

Twitter: @lamw

Getting Started with the vMA (tips/tricks)

Getting Started with the vSphere SDK for Perl

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos