@ -1,45 +1,52 @@
#!/usr/bin/python
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, Tim Rightnour <thegarbledone@gmail.com>
# Copyright (c) 201 7 Tim Rightnour <thegarbledone@gmail.com>
# Copyright (c) 201 8, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import , division , print_function
from __future__ import absolute_import , division , print_function
__metaclass__ = type
__metaclass__ = type
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
ANSIBLE_METADATA = {
' status ' : [ ' preview ' ] ,
' metadata_version ' : ' 1.1 ' ,
' supported_by ' : ' community ' }
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community '
}
DOCUMENTATION = '''
DOCUMENTATION = '''
- - -
- - -
module : vmware_datastore_facts
module : vmware_datastore_facts
short_description : Gather facts about datastores
short_description : Gather facts about datastores available in given vCenter
description :
description :
- Gather facts about datastores in VMWare
- This module can be used to gather facts about datastores in VMWare infrastructure .
- All values and VMware object names are case sensitive .
version_added : 2.5
version_added : 2.5
author :
author :
- Tim Rightnour ( @garbled1 )
- Tim Rightnour ( @garbled1 )
notes :
notes :
- Tested on vSphere 5.5
- Tested on vSphere 5.5 , 6.0 and 6.5
requirements :
requirements :
- " python >= 2.6 "
- " python >= 2.6 "
- PyVmomi
- PyVmomi
options :
options :
name :
name :
description :
description :
- Name of a datastore to match
- Name of the datastore to match .
- If set , facts of specific datastores are returned .
required : False
datacenter :
datacenter :
description :
description :
- Datacenter to search for datastores
- Datacenter to search for datastores .
- This is required if cluster is not supplied
- This parameter is required , if C ( cluster ) is not supplied .
required : False
cluster :
cluster :
description :
description :
- Cluster to search for datastores
- Cluster to search for datastores .
- This is required if datacenter is not supplied
- If set , facts of datastores belonging this clusters will be returned .
required : False
- This parameter is required , if C ( datacenter ) is not supplied .
required : False
extends_documentation_fragment : vmware . documentation
extends_documentation_fragment : vmware . documentation
'''
'''
@ -53,26 +60,48 @@ EXAMPLES = '''
validate_certs : no
validate_certs : no
delegate_to : localhost
delegate_to : localhost
register : facts
register : facts
- name : Gather facts from datacenter about specific datastore
vmware_datastore_facts :
hostname : 192.168 .1 .209
username : administrator @vsphere.local
password : vmware
datacenter : DC0
name : datastore1
validate_certs : no
delegate_to : localhost
register : facts
'''
'''
RETURN = """
RETURN = """
instance :
datastores :
description : metadata about the available datastores
description : metadata about the available datastores
returned : always
returned : always
type : dict
type : list
sample : None
sample : [
{
" accessible " : false ,
" capacity " : 42681237504 ,
" datastore_cluster " : " datacluster0 " ,
" freeSpace " : 39638269952 ,
" maintenanceMode " : " normal " ,
" multipleHostAccess " : false ,
" name " : " datastore2 " ,
" provisioned " : 12289211488 ,
" type " : " VMFS " ,
" uncommitted " : 9246243936 ,
" url " : " ds:///vmfs/volumes/5a69b18a-c03cd88c-36ae-5254001249ce/ "
} ,
]
"""
"""
try :
try :
import pyVmomi
from pyVmomi import vim
from pyVmomi import vim
except ImportError :
except ImportError :
pass
pass
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . _text import to_text
from ansible . module_utils . vmware import ( PyVmomi , vmware_argument_spec , get_all_objs ,
from ansible . module_utils . vmware import ( connect_to_api , vmware_argument_spec ,
get_all_objs , HAS_PYVMOMI , find_obj ,
find_cluster_by_name , get_parent_datacenter )
find_cluster_by_name , get_parent_datacenter )
@ -101,14 +130,9 @@ class PyVmomiCache(object):
return objects
return objects
class PyVmomiHelper ( object ) :
class PyVmomiHelper ( PyVmomi ) :
def __init__ ( self , module ) :
def __init__ ( self , module ) :
if not HAS_PYVMOMI :
super ( PyVmomiHelper , self ) . __init__ ( module )
module . fail_json ( msg = ' pyvmomi module required ' )
self . module = module
self . params = module . params
self . content = connect_to_api ( self . module )
self . cache = PyVmomiCache ( self . content , dc_name = self . params [ ' datacenter ' ] )
self . cache = PyVmomiCache ( self . content , dc_name = self . params [ ' datacenter ' ] )
def lookup_datastore ( self ) :
def lookup_datastore ( self ) :
@ -162,6 +186,10 @@ def main():
dds [ ' url ' ] = summary . url
dds [ ' url ' ] = summary . url
# Calculated values
# Calculated values
dds [ ' provisioned ' ] = summary . capacity - summary . freeSpace + summary . uncommitted
dds [ ' provisioned ' ] = summary . capacity - summary . freeSpace + summary . uncommitted
dds [ ' datastore_cluster ' ] = ' N/A '
if isinstance ( ds . parent , vim . StoragePod ) :
dds [ ' datastore_cluster ' ] = ds . parent . name
if module . params [ ' name ' ] :
if module . params [ ' name ' ] :
if dds [ ' name ' ] == module . params [ ' name ' ] :
if dds [ ' name ' ] == module . params [ ' name ' ] :
datastores . extend ( [ dds ] )
datastores . extend ( [ dds ] )
@ -172,10 +200,7 @@ def main():
# found a datastore
# found a datastore
if datastores :
if datastores :
try :
module . exit_json ( * * result )
module . exit_json ( * * result )
except Exception as exc :
module . fail_json ( msg = " Fact gather failed with exception %s " % to_text ( exc ) )
else :
else :
msg = " Unable to gather datastore facts "
msg = " Unable to gather datastore facts "
if module . params [ ' name ' ] :
if module . params [ ' name ' ] :