# vmware/esx/vsphere module - planning

**URL:** <https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [April 18, 2013, 9:11pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696 "2013-04-18T21:11:01Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![kesten\_broughton1](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kesten\_broughton1](https://forum.ansible.com/u/kesten_broughton1)\
**Post date:** [August 21, 2014, 2:03am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/21 "2014-08-21T02:03:09Z")

</div>

I started with vsphere\_guest but had to give up for lack of the ability to clone from a vm. There was talk about it, but i don’t think it ever made it to trunk.

It’s unfortunate perhaps that vsphere\_guest relies on pysphere whereas the current effort depends on pyvmomi which is vmware’s officially supported python API.

I need to clone from vm and supply disks. vsphere\_guest has the disk support and ansible-vsphere has clone from template.

pyshere has a headstart in terms of exposed (documented) functionality and community, but will likely be eclipsed by pyvmomi if it can garner a community. Maybe for now a hybrid approach is necessary with pip-pointed modules dependent on both. It’s no big deal for for a vsphere lover to install both dependencies, although likely one will win in the end.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 21, 2014, 2:34am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/22 "2014-08-21T02:34:45Z")

</div>

It would be much better to add that capability to the existing one than bifurcate the modules.

See this pull request, though it hasn’t had comments answered on it - [https://github.com/ansible/ansible/pull/7690](https://github.com/ansible/ansible/pull/7690)

Though I would tend to think maybe snapshot is a different module, and cloning is a parameter of the existing one.

Send me a pull request for an update and we can definitely prioritize it, I don’t want to see these start to fork - there’s no good reason for that.

---

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [August 21, 2014, 2:36am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/23 "2014-08-21T02:36:20Z")

</div>

Well it’s unfortunate that their is so duplicated functionality. I actually started this before that module was part of core, and when it finally did get added to core it did not meet my needs. That said my module does a lot more than the one in core. Like I said before you can manage VM snapshots, and interact with the guest operations manager which I use to reconfigure the networking on the devices after I clone the VMs. My module can also power the VMs on and off.

Unfortunately my module does not provide good output after the tasks, and I really could use some help extending it.

As for attaching volumes I have never done that but I think you should be able too. Are you talking about attaching them during a Clone or Create process or attaching a volume to an existing VM?

I think something like this should work. If you look at the spec values they match up to the vsphere API identically. The spec type is the name of a function to run on the guest managed object and the value of the spec is the parameters to that function ( which in this case happens to be called spec ). You do need to fill in any required fields for those objects, which are specified in the vmware documenation.

`  
-name:attach volume  
local\_action:  
module:vsphere  
guest:  
name:test  
state: running  
action: task  
spec:  
type: ReconfigVM\_Task  
value:  
spec:  
VirtualMachineConfigSpec :  
deviceChange:

- VirtualDeviceConfigSpec:  
operation: add  
device:  
VirtualDisk:  
diskObjectId: 

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 21, 2014, 2:40am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/24 "2014-08-21T02:40:25Z")

</div>

"hat said my module does a lot more than the one in core. "

Right, so I’m suggesting let’s add functionality to the core one 🙂

Please send me a pull request if this is something you think you’d like to do - it’s way better long term, and we’ve always been about “batteries included” and getting everybody together to make modules that work for everyone.

We do need to keep the existing signature for module parameters it currently takes, and I do want to have the conversation about the form factor of extensions to it.

---

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [August 21, 2014, 2:54am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/25 "2014-08-21T02:54:02Z")

</div>

After using both, I definitely like using pyvmomi better. Since the API is almost identical to the Vsphere API I was able to use not only the API docs from vmware, but docs for rbvmomi, rvc, and even the java vmware sdk and apply it towards getting the module to do what I want.

As for adding a drive to the cloned VM, that’s definitely doable. Here is my example ( that I actually use ) that shows adding a network device.

`

- name: clone vsphere machine  
local\_action:  
module: vsphere  
host: “{{ vcenter\_host }}”  
login: “{{ vcenter\_login }}”  
password: “{{ vcenter\_pwd }}”  
timeout: 60  
guest:  
name: “{{ host }}”  
state: present  
folder: “{{ group }}”  
clone\_from: “{{ template }}”  
spec:  
type: VirtualMachineCloneSpec  
value:  
config:  
VirtualMachineConfigSpec:  
name: “{{ schema.hostname }}”  
memoryMB: 4096  
numCPUs: 1  
deviceChange:
- VirtualDeviceConfigSpec:  
operation: add  
device:  
VirtualVmxnet3 :  
key: 0  
backing:  
VirtualEthernetCardNetworkBackingInfo:  
deviceName: “{{ networks[0].name }}”  
location:  
VirtualMachineRelocateSpec:  
pool:  
ManagedObjectReference:  
type: ResourcePool  
name: ‘{{ resource\_pool }}’  
host:  
ManagedObjectReference:  
type: HostSystem  
name: ‘{{ compute\_host }}’  
datastore:  
ManagedObjectReference:  
type: Datastore  
name: ‘{{ datastore }}’  
powerOn: True  
template: False

`

To add a disk you would just need to change the deviceChange list to include a VirtualDeviceConfigSpec for the hard drive that you want. I know this is not nearly as easy as just specifying a hard drive, but by taking this approach you can now get any feature that the vmware api provides such as the type of disk backing you want.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 21, 2014, 2:59am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/26 "2014-08-21T02:59:53Z")

</div>

I’m open to anything that keeps the same compatible Ansible module signature.

---

<div class="post-metadata">

**Author:** ![Dave\_Hatton](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@Dave\_Hatton](https://forum.ansible.com/u/Dave_Hatton)\
**Post date:** [August 21, 2014, 11:36am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/27 "2014-08-21T11:36:54Z")

</div>

One thing that is missing from the current vsphere module is the ability reconfigure the cdrom device. My current build process uses a boot.iso to start the build (dont ask why) and so at the end of the create I want to remove the cdrom.

I’m currently using an external script that calls pysphere to do this, it would be nice to have this built in to the ansible module - I’m struggling to work out how to do this. Tony would your module handle this?

---

<div class="post-metadata">

**Author:** ![kesten\_broughton1](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kesten\_broughton1](https://forum.ansible.com/u/kesten_broughton1)\
**Post date:** [August 21, 2014, 1:12pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/28 "2014-08-21T13:12:53Z")

</div>

Snapshots let you store the state of a vm to roll back to later. It’s different from cloning. I searched the module for “clone” and “template” and neither is there.

The examples don’t cover all the config options, nor do the docs.

| 

vm\_hardware:

 |
| | 

memory\_mb: 2048

 |
| | 

num\_cpus: 2

 |
| | 

osid: centos64Guest

 |
| | 

scsi: paravirtual

 |

but when I played with vsphere\_guest my conclusion was that the only support for selecting the base for the vm was def add\_cdrom(module, s, config\_target, config, devices, default\_devs, type="client", vm\_cd\_iso\_path=None):

In vmware, there are two very different paths for creating a vm. One, you select the cd\_rom which is like a bootable iso. The other (more common) is to build up a template (i believe it is backed by a vmdk) and then clone from the template. The latter is the functionality i need, and the former is all that is supported. I could be wrong, but would need an example to show cloning with the given api.

Tony, I am talking about adding a volume after boot, for logs or data drives, although creating partitions on boot would be nice too.

Vsphere is capable of sooo much, I think the idea of adding hooks that expose the functionality in an ‘extra-params’ like way is the way to go, otherwise you end up with a dozen vsphere-x modules. If vsphere\_guest has that capability and it just needs exposing, great. If it is fundamentally not extend then perhaps other options should be explored.

I think apt and synchronize already offer this type of “deeper through extra-args” functionality. Perhaps the best option would be a master module capable of pretty much everything (with good documentation) and then a few facades modules for the most common usages. The code should be factored out into a handful of modules plus some helper classes that are used by all modules, like authentication and connection.

The trouble with the current vsphere\_guest is it offers insufficient capabilities to satisfy any real-world usage pattern, and there doesn’t seem to be much activity in extending it to do so. Tony’s module with the built in power via spec can do orders of magnitude more but would need lots of examples for the various use cases.

I will try to play with the spec today to see if it meets my needs.

---

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [August 22, 2014, 3:04am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/29 "2014-08-22T03:04:14Z")

</div>

I’m fairly confident my module can do it, I just have never done that with the vsphere API. If you have already written a script to do this in pysphere it should be pretty simple to port that. I am assuming that you want to do a ReconfigVM\_Task and it would look something like this below. One thing my module is definitely missing is a good facts gathering system. So you would need to gather the device key information in your inventory script.

`

local\_action:  
module: vsphere  
host: “{{ vcenter\_host }}”  
login: “{{ vcenter\_login }}”  
password: “{{ vcenter\_password }}”  
timeout: 60  
guest:  
name: “{{ deleteme }}”  
state: running  
action: task  
spec:  
type: ReconfigVM\_Task  
value:  
spec:  
VirtualMachineConfigSpec:  
deviceChange:

- VirtualDeviceConfigSpec:  
operation: remove  
device:  
VirtualCdrom:  
key: 

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 22, 2014, 2:06pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/30 "2014-08-22T14:06:58Z")

</div>

“I think the idea of adding hooks that expose the functionality in an ‘extra-params’ like way is the way to go”  
It’s not.

We really want to model things as nouns, so the vsphere\_guest module should take a parameter that can boot from a cloned image.

---

<div class="post-metadata">

**Author:** ![Dann\_Bohn](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Dann\_Bohn](https://forum.ansible.com/u/Dann_Bohn)\
**Post date:** [August 23, 2014, 11:41am UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/31 "2014-08-23T11:41:19Z")

</div>

Hi all,  
I’ve added quite a bit to the vsphere\_guest module to fit our needs, but haven’t submitted PR due to lack of time to generalize / create documentation. I’m also the author of PR 7609 referenced above. Things that I’ve added are the ability to change dvs switch as part of reconfigure, clone, and expand disk as part of reconfigure\_vm. If anyone is interested is taking a look at my crude implementation, I can put it up on github. Like I said, I haven’t put in PR yet becuase, for example, reconfigure\_vm only reconfigures dvs switches, and not standard. and the other bits aren’t tested/documented well enough for me to be comfortable submitting. The green button is scary.

I’ll open a new thread regarding PR 7690

---

<div class="post-metadata">

**Author:** ![Dann\_Bohn](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Dann\_Bohn](https://forum.ansible.com/u/Dann_Bohn)\
**Post date:** [August 23, 2014, 1:30pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/32 "2014-08-23T13:30:59Z")

</div>

It seems a few of us have different implementations of cloning a template, or VM. here’s mine: Mine sets state to ‘clone’ (which probably should be ‘cloned’ or something more descriptive?) and then adds a parameter: template: to specify a template/VM in which to clone from. I had also noticed there is another PR out there to deploy from template.

[https://github.com/whereismyjetpack/ansible/tree/clone](https://github.com/whereismyjetpack/ansible/tree/clone)

If you look at the branch you’ll see there is a lot of logic regarding folders, our organization uses VM folders heavily to grant access and divide systems, so I baked that in, too (it’s not documented yet)

I’m waiting on feedback from this PR to document the clone/new vm folders  
[https://github.com/ansible/ansible/pull/7792](https://github.com/ansible/ansible/pull/7792)

P.S I’m also LogLevel9 – i somehow got two github accounts and I’m trying to merge them into one.

---

<div class="post-metadata">

**Author:** ![John-Paul\_Herold](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@John-Paul\_Herold](https://forum.ansible.com/u/John-Paul_Herold)\
**Post date:** [December 17, 2014, 6:59pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/33 "2014-12-17T18:59:21Z")

</div>

Hey guys, I’ve recently been looking to control vsphere via Ansible which lead me to this thread. Noticed a couple pull requests that were before the September 26th git core-module/extra-module split by the Ansible team. Just wanted to revive this thread for those that had PRs that were closed before resolution.

Cheers,

---

<div class="post-metadata">

**Author:** ![jnitin80](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jnitin80](https://forum.ansible.com/u/jnitin80)\
**Post date:** [February 25, 2015, 12:05pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/34 "2015-02-25T12:05:20Z")

</div>

Hi Tony,

I was playing with vsphere module which you had written.

Vmware tools option am trying

---

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [February 25, 2015, 4:05pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/35 "2015-02-25T16:05:28Z")

</div>

Hey Nitin,

That error looks really strange, I do not know where “Status API Training Shop Blog About” comes from. But syntax errors are usually pretty easy to figure out.

Try `export ANSIBLE_KEEP_REMOTE_FILES=1` and rerun the playbook verbose again. After it fails it will not clean up the module that it copies over to the host that gets executed on, so you should be able to navigate to the directory it stored it in and view the module itself. Then you can check out what line of code corresponds to line 2491.

Also what version of ansible are you using? Not sure how well you know the inner workings of ansible, but basically it takes the custom modules and injects them into a much larger python file that contains loads of helper functions and classes that can be used by the custom modules. I think in newer versions ansible injects the custom module at the top of the file so when you see a backtrace and a line number it matches up with the module itself. Seeing the issue at line number 2491 also makes me think there might be something else going on.

Let me know how it goes.  
Tony

---

<div class="post-metadata">

**Author:** ![jnitin80](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jnitin80](https://forum.ansible.com/u/jnitin80)\
**Post date:** [February 25, 2015, 6:23pm UTC](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696/36 "2015-02-25T18:23:51Z")

</div>

Hi Tony,

Ansible version am using is 1.8.2. I checked line of code corresponding to line 2491. It says " **Status API Training Shop Blog About"**. No other clue:(.

Last few lines are as below,

def pretty\_bytes(self,size):  
ranges = (  
(1\<\<70L, ‘ZB’),  
(1\<\<60L, ‘EB’),  
(1\<\<50L, ‘PB’),  
(1\<\<40L, ‘TB’),  
(1\<\<30L, ‘GB’),  
(1\<\<20L, ‘MB’),  
(1\<\<10L, ‘KB’),  
(1, ‘Bytes’)  
)  
for limit, suffix in ranges:  
if size \>= limit:  
break  
return ‘%.2f %s’ % (float(size)/ limit, suffix)

def get\_module\_path():  
return os.path.dirname(os.path.realpath( **file** ))  
main()  
**Status API Training Shop Blog About**  
Â© 2015 GitHub, Inc. Terms Privacy Security Contact

and verbose output is as below,

PLAY [127.0.0.1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [Upgrade Tools] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<127.0.0.1\> REMOTE\_MODULE vsphere host=xxx password=VALUE\_HIDDEN login=administrator@vsphere.local  
\<127.0.0.1\> EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957 && echo $HOME/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957’]  
\<127.0.0.1\> PUT /tmp/tmp3gLdMW TO /home/osboxes/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957/vsphere  
\<127.0.0.1\> EXEC [‘/bin/sh’, ‘-c’, u’LANG=C LC\_CTYPE=C /usr/bin/env python /home/osboxes/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957/vsphere; rm -rf /home/osboxes/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957/ \>/dev/null 2\>&1’]  
failed: [127.0.0.1 → 127.0.0.1] =\> {“failed”: true, “parsed”: false}  
File “/home/osboxes/.ansible/tmp/ansible-tmp-1424886684.83-207068257700957/vsphere”, line 2491  
Status API Training Shop Blog About  
^  
SyntaxError: invalid syntax

[Previous page](https://forum.ansible.com/t/vmware-esx-vsphere-module-planning/12696.md?page=1)
