|
|
@ -55,10 +55,9 @@ DOCUMENTATION = '''
|
|
|
|
with_tags:
|
|
|
|
with_tags:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Include tags and associated virtual machines.
|
|
|
|
- Include tags and associated virtual machines.
|
|
|
|
- Requires 'vSphere Automation SDK' and 'vCloud Suite SDK' libraries to be installed on the given controller machine.
|
|
|
|
- Requires 'vSphere Automation SDK' library to be installed on the given controller machine.
|
|
|
|
- Please refer following URLs for installation steps
|
|
|
|
- Please refer following URLs for installation steps
|
|
|
|
- 'https://code.vmware.com/web/sdk/65/vsphere-automation-python'
|
|
|
|
- 'https://code.vmware.com/web/sdk/65/vsphere-automation-python'
|
|
|
|
- 'https://code.vmware.com/web/sdk/60/vcloudsuite-python'
|
|
|
|
|
|
|
|
default: False
|
|
|
|
default: False
|
|
|
|
type: boolean
|
|
|
|
type: boolean
|
|
|
|
'''
|
|
|
|
'''
|
|
|
@ -93,22 +92,13 @@ except ImportError:
|
|
|
|
HAS_PYVMOMI = False
|
|
|
|
HAS_PYVMOMI = False
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from vmware.vapi.lib.connect import get_requests_connector
|
|
|
|
|
|
|
|
from vmware.vapi.security.session import create_session_security_context
|
|
|
|
|
|
|
|
from vmware.vapi.security.user_password import create_user_password_security_context
|
|
|
|
|
|
|
|
from com.vmware.cis_client import Session
|
|
|
|
|
|
|
|
from com.vmware.vapi.std_client import DynamicID
|
|
|
|
from com.vmware.vapi.std_client import DynamicID
|
|
|
|
from com.vmware.cis.tagging_client import Tag, TagAssociation
|
|
|
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
|
|
|
HAS_VCLOUD = True
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
HAS_VCLOUD = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
|
|
|
|
|
|
|
|
HAS_VSPHERE = True
|
|
|
|
HAS_VSPHERE = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
HAS_VSPHERE = False
|
|
|
|
HAS_VSPHERE = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -144,28 +134,13 @@ class BaseVMwareInventory:
|
|
|
|
# Disable warning shown at stdout
|
|
|
|
# Disable warning shown at stdout
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
|
|
|
vcenter_url = "https://%s/api" % self.hostname
|
|
|
|
client = create_vsphere_client(server=self.hostname,
|
|
|
|
|
|
|
|
username=self.username,
|
|
|
|
# Get request connector
|
|
|
|
password=self.password,
|
|
|
|
connector = get_requests_connector(session=session, url=vcenter_url)
|
|
|
|
session=session)
|
|
|
|
# Create standard Configuration
|
|
|
|
if client is None:
|
|
|
|
stub_config = StubConfigurationFactory.new_std_configuration(connector)
|
|
|
|
|
|
|
|
# Use username and password in the security context to authenticate
|
|
|
|
|
|
|
|
security_context = create_user_password_security_context(self.username, self.password)
|
|
|
|
|
|
|
|
# Login
|
|
|
|
|
|
|
|
stub_config.connector.set_security_context(security_context)
|
|
|
|
|
|
|
|
# Create the stub for the session service and login by creating a session.
|
|
|
|
|
|
|
|
session_svc = Session(stub_config)
|
|
|
|
|
|
|
|
session_id = session_svc.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# After successful authentication, store the session identifier in the security
|
|
|
|
|
|
|
|
# context of the stub and use that for all subsequent remote requests
|
|
|
|
|
|
|
|
session_security_context = create_session_security_context(session_id)
|
|
|
|
|
|
|
|
stub_config.connector.set_security_context(session_security_context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if stub_config is None:
|
|
|
|
|
|
|
|
raise AnsibleError("Failed to login to %s using %s" % (self.hostname, self.username))
|
|
|
|
raise AnsibleError("Failed to login to %s using %s" % (self.hostname, self.username))
|
|
|
|
return stub_config
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def _login(self):
|
|
|
|
def _login(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -235,11 +210,6 @@ class BaseVMwareInventory:
|
|
|
|
" Please refer this URL for installation steps"
|
|
|
|
" Please refer this URL for installation steps"
|
|
|
|
" - https://code.vmware.com/web/sdk/65/vsphere-automation-python")
|
|
|
|
" - https://code.vmware.com/web/sdk/65/vsphere-automation-python")
|
|
|
|
|
|
|
|
|
|
|
|
if not HAS_VCLOUD and self.with_tags:
|
|
|
|
|
|
|
|
raise AnsibleError("Unable to find 'vCloud Suite SDK' Python library which is required."
|
|
|
|
|
|
|
|
" Please refer this URL for installation steps"
|
|
|
|
|
|
|
|
" - https://code.vmware.com/web/sdk/60/vcloudsuite-python")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not all([self.hostname, self.username, self.password]):
|
|
|
|
if not all([self.hostname, self.username, self.password]):
|
|
|
|
raise AnsibleError("Missing one of the following : hostname, username, password. Please read "
|
|
|
|
raise AnsibleError("Missing one of the following : hostname, username, password. Please read "
|
|
|
|
"the documentation for more information.")
|
|
|
|
"the documentation for more information.")
|
|
|
@ -394,8 +364,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
|
|
|
|
properties=['name'])
|
|
|
|
properties=['name'])
|
|
|
|
|
|
|
|
|
|
|
|
if self.pyv.with_tags:
|
|
|
|
if self.pyv.with_tags:
|
|
|
|
tag_svc = Tag(self.pyv.rest_content)
|
|
|
|
tag_svc = self.pyv.rest_content.tagging.Tag
|
|
|
|
tag_association = TagAssociation(self.pyv.rest_content)
|
|
|
|
tag_association = self.pyv.rest_content.tagging.TagAssociation
|
|
|
|
|
|
|
|
|
|
|
|
tags_info = dict()
|
|
|
|
tags_info = dict()
|
|
|
|
tags = tag_svc.list()
|
|
|
|
tags = tag_svc.list()
|
|
|
@ -428,7 +398,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
|
|
|
|
self._populate_host_properties(vm_obj, current_host)
|
|
|
|
self._populate_host_properties(vm_obj, current_host)
|
|
|
|
|
|
|
|
|
|
|
|
# Only gather facts related to tag if vCloud and vSphere is installed.
|
|
|
|
# Only gather facts related to tag if vCloud and vSphere is installed.
|
|
|
|
if HAS_VCLOUD and HAS_VSPHERE and self.pyv.with_tags:
|
|
|
|
if HAS_VSPHERE and self.pyv.with_tags:
|
|
|
|
# Add virtual machine to appropriate tag group
|
|
|
|
# Add virtual machine to appropriate tag group
|
|
|
|
vm_mo_id = vm_obj.obj._GetMoId()
|
|
|
|
vm_mo_id = vm_obj.obj._GetMoId()
|
|
|
|
vm_dynamic_id = DynamicID(type='VirtualMachine', id=vm_mo_id)
|
|
|
|
vm_dynamic_id = DynamicID(type='VirtualMachine', id=vm_mo_id)
|
|
|
|