You need to enable JavaScript to run this app. Ansible 使用vmware_guest模块实现多vCenter跨ESXi并行部署VM的方案求助
优惠活动
大模型
产品
解决方案
定价
更多

Ansible 使用vmware_guest模块实现多vCenter跨ESXi并行部署VM的方案求助

多vCenter环境下Ansible并行创建VM的实现方案

当前实现的核心缺陷

  • 架构适配不足:Playbook内硬编码单vCenter连接参数,无法兼容3台独立vCenter的分布式管控拓扑
  • 执行效率瓶颈:任务级同步loop依赖vmware_guest模块的阻塞返回逻辑,每次只有当前VM克隆完成才会发起下一个创建请求,完全串行执行无法满足并行要求
  • 可扩展性差:所有ESXi目标硬塞在单vCenter的loop列表里,后续新增vCenter或ESXi主机需要大幅改动任务逻辑

落地实现步骤

1. 重构结构化资产变量

去掉硬编码的单vCenter参数,将多vCenter的连接信息、关联ESXi目标映射成可遍历的结构化列表,实现执行逻辑和配置数据解耦:

---
- name: 多vCenter批量并行部署VM
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    # 全局VM通用配置
    vm_disk_size: 100
    validate_vcenter_cert: false
    vm_annotation: "Ansible Test"
    # 多vCenter结构化资产清单
    vcenter_infra:
      - vcenter_hostname: 10.0.0.1
        vcenter_user: john.doe
        vcenter_pass: Passw0rd
        datacenter: Datacenter1
        template_fullpath: templates/test-template
        target_hosts:
          - {esxhost: '10.0.46.142', datastore: 'Datastore_XXX', vm_name: 'newvm-vc01-esx01'}
          - {esxhost: '10.0.46.143', datastore: 'Datastore_ZZZ', vm_name: 'newvm-vc01-esx02'}
          - {esxhost: '10.0.46.144', datastore: 'Datastore_AAA', vm_name: 'newvm-vc01-esx03'}
      - vcenter_hostname: 10.0.0.2
        vcenter_user: admin@vsphere.local
        vcenter_pass: Passw0rd
        datacenter: Datacenter2
        template_fullpath: templates/test-template
        target_hosts:
          - {esxhost: '10.0.47.142', datastore: 'Datastore_BBB', vm_name: 'newvm-vc02-esx01'}
          - {esxhost: '10.0.47.143', datastore: 'Datastore_CCC', vm_name: 'newvm-vc02-esx02'}
          - {esxhost: '10.0.47.144', datastore: 'Datastore_DDD', vm_name: 'newvm-vc02-esx03'}
      - vcenter_hostname: 10.0.0.3
        vcenter_user: admin@vsphere.local
        vcenter_pass: Passw0rd
        datacenter: Datacenter3
        template_fullpath: templates/test-template
        target_hosts:
          - {esxhost: '10.0.48.142', datastore: 'Datastore_EEE', vm_name: 'newvm-vc03-esx01'}
          - {esxhost: '10.0.48.143', datastore: 'Datastore_FFF', vm_name: 'newvm-vc03-esx02'}
          - {esxhost: '10.0.48.144', datastore: 'Datastore_GGG', vm_name: 'newvm-vc03-esx03'}

2. 替换同步loop为异步并行执行

通过async + poll:0机制将VM克隆任务放入Ansible后台进程池,实现所有目标的并行API调用:

  • 用subelements过滤器将vCenter条目和对应ESXi目标展平为独立执行单元,每个单元对应一个VM创建任务
  • 配置async为单任务最大超时时间,poll:0表示启动任务后不等待返回,直接发起下一个任务的API请求
  • 单独增加后台任务轮询环节,等所有任务全部启动完成后再统一等待执行结果
tasks:
    - name: 后台并行触发所有VM克隆任务
      vmware_guest:
        validate_certs: "{{ validate_vcenter_cert }}"
        hostname: "{{ item.0.vcenter_hostname }}"
        username: "{{ item.0.vcenter_user }}"
        password: "{{ item.0.vcenter_pass }}"
        esxi_hostname: "{{ item.1.esxhost }}"
        datacenter: "{{ item.0.datacenter }}"
        folder: "{{ item.0.template_fullpath | dirname }}"
        name: "{{ item.1.vm_name }}"
        template: "{{ item.0.template_fullpath | basename }}"
        annotation: "{{ vm_annotation }}"
        disk:
          - size_gb: "{{ vm_disk_size }}"
            datastore: "{{ item.1.datastore }}"
        wait_for_ip_address: False
        state: present
      loop: "{{ vcenter_infra | subelements('target_hosts') }}"
      async: 3600  # 单台VM克隆最长超时1小时
      poll: 0  # 不阻塞等待,启动即进入下一个任务
      register: vm_deploy_jobs

    - name: 等待所有后台克隆任务执行完成
      async_status:
        jid: "{{ item.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 120  # 每10秒轮询一次,最多等待20分钟
      delay: 10
      loop: "{{ vm_deploy_jobs.results }}"
      loop_control:
        label: "等待VM {{ item.item.1.vm_name }} 部署完成"

3. 运行参数校准

  • 执行Playbook时需要调整forks参数(默认值为5),确保9个并行任务不会被进程池排队限制,可直接在命令行传入参数:
    ansible-playbook create_multi_vm.yml --forks 15
    
  • 修正原有代码中的拼写错误:原变量中test-teplate存在拼写错误,会导致模板查找失败,需调整为实际的模板名称

原实现中的同步loop迭代无异步机制加持,每发起一次克隆请求需要等待vCenter返回全流程执行完成才会启动下一个请求,9台VM的部署时长等于单台克隆时间*9,完全达不到并行要求。


内容的提问来源于stack exchange,提问作者Disbalance

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.25 11:15:07