Beispiel #1
0
def main():
    result = dict(changed=False)
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(name=dict(type="str"))

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    try:
        obj = VmwareRestClient(module)
        vm_svc = vcenter_client.VM(obj.connect)
    except Exception as e:
        module.fail_json(msg=str(e))

    vm_name = module.params["name"]
    if (vm_name):
        names = set([vm_name])
        vm = vm_svc.list(vcenter_client.VM.FilterSpec(names=names))
        r = list(map(lambda x: x.to_dict(), vm))
        if (len(r) > 0):
            result["virtual_machines"] = vm_svc.get(r[0]["vm"]).to_dict()
            module.exit_json(**result)
        else:
            module.fail_json(msg="%s not found." % vm_name)
    else:
        vms = vm_svc.list()
        r = list(map(lambda x: x.to_dict(), vms))

        r_array = []
        for vm in r:
            r_array.append(vm_svc.get(vm["vm"]).to_dict())

        result["virtual_machines"] = r_array
        module.exit_json(**result)
Beispiel #2
0
    def properties_facts(self):
        ansible_facts = self.to_json(self.host, self.params.get('properties'))
        if self.params.get('show_tag'):
            vmware_client = VmwareRestClient(self.module)
            tag_info = {
                'tags':
                vmware_client.get_tags_for_hostsystem(
                    hostsystem_mid=self.host._moId)
            }
            ansible_facts.update(tag_info)

        self.module.exit_json(changed=False, ansible_facts=ansible_facts)
Beispiel #3
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        ovf_template=dict(type='str',
                          aliases=['template_src', 'ovf'],
                          required=True),
        name=dict(type='str', required=True, aliases=['vm_name']),
        datacenter=dict(type='str', required=True),
        datastore=dict(type='str', required=True),
        folder=dict(type='str', required=True),
        host=dict(type='str', required=True),
        resource_pool=dict(type='str', required=False),
        cluster=dict(type='str', required=False),
    )
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)
    result = {'failed': False, 'changed': False}
    pyv = PyVmomi(module=module)
    vm = pyv.get_vm()
    if vm:
        module.exit_json(changed=False,
                         vm_deploy_info=dict(
                             msg="Virtual Machine '%s' already Exists." %
                             module.params['name'],
                             vm_id=vm._moId,
                         ))
    vmware_contentlib_create = VmwareContentDeployOvfTemplate(module)
    if module.check_mode:
        result.update(
            vm_name=module.params['name'],
            changed=True,
            desired_operation='Create VM with PowerOff State',
        )
        module.exit_json(**result)
    vmware_contentlib_create.deploy_vm_from_ovf_template()
Beispiel #4
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    vmware_tag_facts = VmTagFactManager(module)
    vmware_tag_facts.get_all_tags()
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    vmware_tag_facts = VmTagFactManager(module)
    vmware_tag_facts.get_all_tags()
Beispiel #6
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    vmware_category_facts = VmwareCategoryFactsManager(module)
    vmware_category_facts.get_all_tag_categories()
    def get_tags_for_object(self, dobj):
        """
        Return tags associated with an object
        Args:
            dobj: Dynamic object
        Returns: List of tags associated with the given object
        """
        vmware_client = VmwareRestClient(self.module)

        cluster_dynamic_obj = DynamicID(type='ClusterComputeResource',
                                        id=dobj._moId)
        self.tag_service = vmware_client.api_client.tagging.Tag
        self.tag_association_svc = vmware_client.api_client.tagging.TagAssociation
        self.category_service = vmware_client.api_client.tagging.Category

        tag_ids = self.tag_association_svc.list_attached_tags(
            cluster_dynamic_obj)
        tags = []
        for tag_id in tag_ids:
            tag_obj = self.tag_service.get(tag_id)
            tags.append({
                'id':
                tag_obj.id,
                'category_name':
                self.category_service.get(tag_obj.category_id).name,
                'name':
                tag_obj.name,
                'description':
                tag_obj.description,
                'category_id':
                tag_obj.category_id,
            })
        return tags
    def all_facts(self):
        ansible_facts = {}
        ansible_facts.update(self.get_cpu_facts())
        ansible_facts.update(self.get_memory_facts())
        ansible_facts.update(self.get_datastore_facts())
        ansible_facts.update(self.get_network_facts())
        ansible_facts.update(self.get_system_facts())
        ansible_facts.update(self.get_vsan_facts())
        ansible_facts.update(self.get_cluster_facts())
        if self.params.get('show_tag'):
            vmware_client = VmwareRestClient(self.module)
            tag_info = {
                'tags': vmware_client.get_tags_for_hostsystem(hostsystem_mid=self.host._moId)
            }
            ansible_facts.update(tag_info)

        self.module.exit_json(changed=False, ansible_facts=ansible_facts)
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)
    if module._name == 'vmware_tag_facts':
        module.deprecate("The 'vmware_tag_facts' module has been renamed to 'vmware_tag_info'", version='2.13')

    vmware_tag_info = VmTagInfoManager(module)
    vmware_tag_info.get_all_tags()
Beispiel #10
0
    def get_tag_facts(self):
        vmware_client = VmwareRestClient(self.module)

        host_dynamic_obj = DynamicID(type='HostSystem', id=self.host._moId)
        self.tag_service = vmware_client.api_client.tagging.Tag
        self.tag_association_svc = vmware_client.api_client.tagging.TagAssociation
        self.category_service = vmware_client.api_client.tagging.Category
        facts = {'tags': self.get_tags_for_object(host_dynamic_obj)}
        return facts
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        datacenter_name=dict(type='str', required=True),
    )

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=False)
    dc_status = VmDCManager(module)
    dc_status.ensure_state()
Beispiel #12
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        tag_name=dict(type='str', required=True),
        tag_description=dict(type='str', default='', required=False),
        category_id=dict(type='str', required=False),
        state=dict(type='str', choices=['present', 'absent'], default='present', required=False),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_tag = VmwareTag(module)
    vmware_tag.ensure_state()
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(library_id=dict(type='str', required=False), )
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    vmware_contentlib_info = VmwareContentLibInfo(module)
    if module.params.get('library_id'):
        vmware_contentlib_info.get_content_lib_details(
            module.params['library_id'])
    else:
        vmware_contentlib_info.get_all_content_libs()
Beispiel #14
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        tag_name=dict(type='str', required=True),
        tag_description=dict(type='str', default='', required=False),
        category_id=dict(type='str', required=False),
        state=dict(type='str', choices=['present', 'absent'], default='present', required=False),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_tag = VmwareTag(module)
    vmware_tag.ensure_state()
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        tag_names=dict(type='list', required=True),
        state=dict(type='str', choices=['absent', 'add', 'present', 'remove', 'set'], default='add'),
        object_name=dict(type='str', required=True),
        object_type=dict(type='str', required=True, choices=['VirtualMachine', 'Datacenter', 'ClusterComputeResource',
                                                             'HostSystem', 'DistributedVirtualSwitch',
                                                             'DistributedVirtualPortgroup']),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_tag_manager = VmwareTagManager(module)
    vmware_tag_manager.ensure_state()
Beispiel #16
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        vm=dict(type='str', required=True),
        uuid=dict(type='str', required=True),
        tags=dict(type='dict', required=True),
        category_ids=dict(type='dict', required=True),
    )
    module = AnsibleModule(argument_spec=argument_spec)
    pyv = PyVmomi(module)
    vm = pyv.get_vm()
    vmware_tag = VmwareTag(module)
    result = vmware_tag.apply_tags(vm._moId, module.params['category_ids'],
                                   module.params['tags'])
    module.exit_json()
Beispiel #17
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        tag_names=dict(type='list', required=True),
        state=dict(type='str',
                   choices=['absent', 'add', 'present', 'remove', 'set'],
                   default='add'),
        object_name=dict(type='str', required=True),
        object_type=dict(type='str', required=True,
                         choices=['VirtualMachine']),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_tag_manager = VmwareTagManager(module)
    vmware_tag_manager.ensure_state()
Beispiel #18
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        category_name=dict(type='str', required=True),
        category_description=dict(type='str', default='', required=False),
        category_cardinality=dict(type='str',
                                  choices=["multiple", "single"],
                                  default="multiple"),
        new_category_name=dict(type='str'),
        state=dict(type='str',
                   choices=['present', 'absent'],
                   default='present'),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_category = VmwareCategory(module)
    vmware_category.ensure_state()
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        object_name=dict(),
        object_moid=dict(),
        new_name=dict(aliases=['object_new_name'], required=True),
        object_type=dict(type='str', required=True),
    )
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['object_name', 'object_moid'],
        ]
    )

    vmware_object_rename = VmwareObjectRename(module)
    vmware_object_rename.ensure_state()
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        library_name=dict(type='str', required=False),
        library_description=dict(type='str', required=False),
        library_type=dict(type='str',
                          required=False,
                          choices=['local', 'subscribed'],
                          default='local'),
        datastore_name=dict(type='str', required=False, aliases=['datastore']),
        state=dict(type='str',
                   choices=['present', 'absent'],
                   default='present',
                   required=False),
    )
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    vmware_contentlib_create = VmwareContentLibCreate(module)
    vmware_contentlib_create.process_state()
Beispiel #21
0
def main():
    argument_spec = VmwareRestClient.vmware_client_argument_spec()
    argument_spec.update(
        category_name=dict(type='str', required=True),
        category_description=dict(type='str', default='', required=False),
        category_cardinality=dict(type='str',
                                  choices=["multiple", "single"],
                                  default="multiple"),
        new_category_name=dict(type='str'),
        state=dict(type='str',
                   choices=['present', 'absent'],
                   default='present'),
        associable_object_types=dict(
            type='list',
            choices=[
                'All objects',
                'Folder',
                'Cluster',
                'Datacenter',
                'Datastore',
                'Datastore Cluster',
                'Distributed Port Group',
                'Distributed Switch',
                'Host',
                'Content Library',
                'Library item',
                'Network',
                'Resource Pool',
                'vApp',
                'Virtual Machine',
            ],
            elements=str,
        ),
    )
    module = AnsibleModule(argument_spec=argument_spec)

    vmware_category = VmwareCategory(module)
    vmware_category.ensure_state()
Beispiel #22
0
 def get_tag_info(self, vm_dynamic_obj):
     vmware_client = VmwareRestClient(self.module)
     return vmware_client.get_tags_for_vm(vm_mid=vm_dynamic_obj._moId)
Beispiel #23
0
    def gather_cluster_info(self):
        """
        Gather information about cluster
        """
        results = dict(changed=False, clusters=dict())
        for cluster in self.cluster_objs:
            # Default values
            ha_failover_level = None
            ha_restart_priority = None
            ha_vm_tools_monitoring = None
            ha_vm_min_up_time = None
            ha_vm_max_failures = None
            ha_vm_max_failure_window = None
            ha_vm_failure_interval = None
            enabled_vsan = False
            vsan_auto_claim_storage = False

            # HA
            das_config = cluster.configurationEx.dasConfig
            if das_config.admissionControlPolicy:
                ha_failover_level = das_config.admissionControlPolicy.failoverLevel
            if das_config.defaultVmSettings:
                ha_restart_priority = das_config.defaultVmSettings.restartPriority,
                ha_vm_tools_monitoring = das_config.defaultVmSettings.vmToolsMonitoringSettings.vmMonitoring,
                ha_vm_min_up_time = das_config.defaultVmSettings.vmToolsMonitoringSettings.minUpTime,
                ha_vm_max_failures = das_config.defaultVmSettings.vmToolsMonitoringSettings.maxFailures,
                ha_vm_max_failure_window = das_config.defaultVmSettings.vmToolsMonitoringSettings.maxFailureWindow,
                ha_vm_failure_interval = das_config.defaultVmSettings.vmToolsMonitoringSettings.failureInterval,

            # DRS
            drs_config = cluster.configurationEx.drsConfig

            # VSAN
            if hasattr(cluster.configurationEx, 'vsanConfig'):
                vsan_config = cluster.configurationEx.vsanConfig
                enabled_vsan = vsan_config.enabled,
                vsan_auto_claim_storage = vsan_config.defaultConfig.autoClaimStorage,

            tag_info = []
            if self.params.get('show_tag'):
                vmware_client = VmwareRestClient(self.module)
                tag_info = vmware_client.get_tags_for_cluster(
                    cluster_mid=cluster._moId)

            results['clusters'][cluster.name] = dict(
                enable_ha=das_config.enabled,
                ha_failover_level=ha_failover_level,
                ha_vm_monitoring=das_config.vmMonitoring,
                ha_host_monitoring=das_config.hostMonitoring,
                ha_admission_control_enabled=das_config.
                admissionControlEnabled,
                ha_restart_priority=ha_restart_priority,
                ha_vm_tools_monitoring=ha_vm_tools_monitoring,
                ha_vm_min_up_time=ha_vm_min_up_time,
                ha_vm_max_failures=ha_vm_max_failures,
                ha_vm_max_failure_window=ha_vm_max_failure_window,
                ha_vm_failure_interval=ha_vm_failure_interval,
                enabled_drs=drs_config.enabled,
                drs_enable_vm_behavior_overrides=drs_config.
                enableVmBehaviorOverrides,
                drs_default_vm_behavior=drs_config.defaultVmBehavior,
                drs_vmotion_rate=drs_config.vmotionRate,
                enabled_vsan=enabled_vsan,
                vsan_auto_claim_storage=vsan_auto_claim_storage,
                tags=tag_info,
            )

        self.module.exit_json(**results)