-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone a template example #8
Comments
If you are interested, i'm working on a multi-clone script that can be used as an example, i'll try and finish it tomorrow and place a link here (and if VMware accepts it, they are allowed to add it to their samples set) Basically, you need to do the following steps:
(very basic) Code example: from pyVmomi import vim
def find_vm(si,name):
content = si.content
obj_view = content.viewManager.CreateContainerView(content.rootFolder,[vim.VirtualMachine],True)
vm_list = obj_view.view
for vm in vm_list:
if vm.name == name:
return vm
return None
# Connecting to host
si = None
try:
si = SmartConnect(host='server',user='username',pwd='password',port=443)
except IOError, e:
pass
# Finding source VM
template_vm = find_vm(si,'template_name')
# Creating relocate spec and clone spec
relocateSpec = vim.vm.RelocateSpec()
cloneSpec = vim.vm.CloneSpec(powerOn=True,template=False,location=relocateSpec)
# Creating clone task
clone = template_vm.Clone(name='clonedVM01',folder=template_vm.parent,spec=cloneSpec) |
@pdellaert thanks! I started working on a script yesterday and it is progressing nicely. Your example is showing I'm on the right track though, so it is helpful. I'll share mine once I'm finished with it. |
@pdellaert Here is what I have so far: https://gist.github.com/snobear/8788977 I'm able to deploy a VM from the template, but I'm stuck on setting the network of the network adapter. When I uncomment the line that sets the device, vsphere complains:
No more details than that. I must be setting up the device incorrectly. My template has a NIC device already; I just need to change the network on it. Any idea how to do that, or am I close? |
Nice example! I have no experience with changing the hardware on a virtual machine, so i don't think i would be of much help. Altho i'm thinking of looking into it... My example is a bit different: |
I updated the gist with my working example. It takes a few command-line arguments that you can see with
We use static IPs for everything and have several different networks, so what my example script does is take the static IP supplied by the user and determine where the new cloned VM should reside, what datastore, etc... So my networks and vSphere settings are hardcoded into the script. Well, I've changed them to fake networks for the purpose of this example but you get the idea. We don't have the most dynamic setup in our environment, so this works really well. Come to think of it, I bet hardcoding these things is unnecessary and I could probably add some better lookups. I'm using python 2.7 and my pip requirements are:
Not sure if wsgiref was a carrover from me testing another vsphere py module (pysphere). I believe this works with cloning any VM, and not just a template. I'll close this issue since I'm satisfied :). I'm happy to submit a pull request to add this to the examples directory, but I'm not sure if it's generic enough. Let me know, VMware folks. |
Thanks for this amazing script, I took the liberty of modifying some lines though, Specifically I would suggest the check for the ethernet device to be done as:
since some VMs (those at 32 bit for example) are configured with flexible devices and not with E1000 ones. the |
I moved this example to a more generic tool: https://github.com/snobear/ezmomi Feel free to fork and use what you want, or help me beef it up and add some features. |
Hello, I have a problem when I use this method device = template_vm.config.hardware.device Do you have an idea to help me? raceback (most recent call last): Thanks. Regards |
@jon67400 Which object are you calling the method on? |
Hi can you please be more specific (give some more code) as what you wrote Thanks Davide Papini [email protected]On Fri, Jan 16, 2015 at 3:33 PM, jon67400 [email protected] wrote:
|
Here is the content of the template_vm:
where template is the name of the vm template. Here is the content of the get_obj method def get_obj(content, vimtype, name): What I don't understand, why the xml parsed is in binary code not understood by human? |
Try using some print statements to see what's going on. For example:
This will allow you to know exactly where the error is occurring. |
with prints... 'vim.VirtualMachine:vm-228' |
@jon67400 I'm sorry but it seems you've reached the limit of my knowledge. I've tried googling around for your error, but haven't found anything. |
Hi, Probably this is a stupid question to ask, but do you have problems with Does the version of the VMware sdk support the vsphere environment version Davide Papini [email protected]On Fri, Jan 16, 2015 at 8:44 PM, Reina Abolofia [email protected]
|
This should have been opened as a new issue. |
I'm having trouble parsing this thread. It would help if we could get a failing sample in a gist to work with. |
Hi All, I am seeing the below issue when trying to do vm migration from one node to another one.
<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<>>>>>>> |
Do you have any examples or docs on using the Clone() function, specifically how to clone/deploy a VM from a template?
The text was updated successfully, but these errors were encountered: