使用 Ansible 在 VCenter 服务器上创建文件夹

Create folder on VCenter server using Ansible

比如我想在"Administrator Desktops"下克隆一个虚拟机,如下图所示: Click here to see the image

下面的脚本是从指定模板克隆虚拟机并将虚拟机放入指定文件夹。但当该文件夹在 VCenter 服务器上不存在时失败:

---
- hosts: localhost
  connection: local
  sudo: false
  user: root
  gather_facts: false
  serial: 1

  vars_files:
    - createVmVars.yml

  tasks:
    - name: Deploying VM from template.
      vsphere_guest:
        vcenter_hostname: "{{vcenter_hostname}}"
        username: "{{vcenter_username}}"
        password: "{{vcenter_password}}"
        guest: "{{guest_name}}"
        from_template: yes
        template_src: "{{template_src}}"
        cluster: "{{cluster}}"
        resource_pool: "{{resource_pool}}"
        vm_extra_config:
          folder: "{{folder_name}}"

需要帮助使此脚本灵活,以便当文件夹不存在时,它应该创建提到的文件夹,然后将 VM 克隆到该文件夹​​下。

您可以使用这个 Ansible 模块在 vsphere 中创建文件夹:

#!/usr/bin/python
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
author: "Shashank Awasthi"
module: vsphere_create_folder
short_description: Create a folder on VCenter if it does not exist
description:
  - This module requires login to VCenter Server
  - This module requires pysphere python module installed
  - This module creates a folder on mentioned VCenter Server
  - This module does not create any folder if the folder with the same name is already existing on the VCenter Server
  - This module supports nesting upto only 2 levels
version_added: "1.2"
options:
  host:
    description:
      - The vsphere Server on which the folder is to be created
    required: true
  login:
    description:
      - The login name to authenticate on VSphere
    required: true
  password:
    description:
      - The password to authenticate VSphere
    required: true
  folder_name:
    description:
      - The folder name which is to be created
    required: true
  parent_folder_name:
    description:
      The name of parent folder under which the folder_name is residing.
    required: true
  datacenter_name:
    description:
      - The name of the datacenter where the folder is to be created
    required: true
examples:
  - description: create the folder with name NewDeployments
    code:
       - local_action: vsphere_create_folder host=$eszserver login=$esxlogin password=$esxpassword folder_name=$folder_name
parent_folder_name=$parent_folder_name  datacenter_name=$dc_name
notes:
  - This module ought ot be run from a system which can access vsphere directly
'''

import sys

try:
        import pysphere
        from pysphere import *
        from pysphere.resources import VimService_services as VI
except ImportError:
        print "failed=true, msg=Pysphere Python module not available"
        sys.exit(1)

def main():
        module = AnsibleModule(
                argument_spec = dict(
                        host =  dict(requred = True),
                        login = dict(required = True),
                        password = dict(required = True),
                        folder_name = dict(required = True),
                        parent_folder_name =  dict(required = True),
                        datacenter_name = dict(required = True)
                )
        )

        host = module.params.get('host')
        login = module.params.get('login')
        password = module.params.get('password')
        folder_name = module.params.get('folder_name')
        parent_folder_name = module.params.get('parent_folder_name')
        datacenter_name = module.params.get('datacenter_name')

        server = pysphere.VIServer()
        try:
                server.connect(host,login,password)
        except Exception, e:
                module.fail_json(msg = 'Failed to connect to %s: %s' % (host, e))


        def createFolder(vm_folder,folder_name):
                try:
                        request = VI.CreateFolderRequestMsg()
                        _this = request.new__this(vm_folder)
                        _this.set_attribute_type(vm_folder.get_attribute_type())
                        request.set_element__this(_this)
                        request.set_element_name(folder_name)
                        server._proxy.CreateFolder(request)
                except pysphere.ZSI.FaultException, e:
                        pass

        try:
                datacenters = server._get_datacenters()
                dc = datacenters[datacenter_name]

                dc_props = VIProperty(server,dc)
                vm_folder = dc_props.vmFolder._obj

                createFolder(vm_folder,parent_folder_name)

                folders =  server._retrieve_properties_traversal(property_names=['name'], from_node =  dc, obj_type = 'Folder')

                for f in folders:
                        if f.PropSet[0].Val == parent_folder_name:
                                vm_folder = f.Obj
                                break
                createFolder(vm_folder,folder_name)
        except Exception, e:
                module.fail_json(msg = "failed to create folder: %s" % e)




        module.exit_json(changed = True, folder = folder_name, parent_folder = parent_folder_name)

#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()