之前的脚本是串行的,效率并不高
更改为并行模式

tree
.
├── ansible.cfg
├── create_vm
├── linux-vm-template
├── linux-vm-template.retry
├── roles
│   ├── create_vm
│   │   └── tasks
│   │       └── main
│   ├── linux_vm_template
│   │   └── tasks
│   │       └── main
│   └── win_vm_template
│       └── tasks
│           └── main
├── vcenter.yml
├── vm-to-deploy
├── vm_list
├── vm_list.py
├── vm_list.xlsx
├── win2012-vm-template

ansible.cfg

[defaults]

# some basic default values...
library        = ./library

# additional paths to search for roles in, colon separated
roles_path    = ./roles

linux-vm-template

---
- hosts: all
  gather_facts: false
  vars_files:
    - vcenter.yml
  roles:
     - linux_vm_template

linux-vm-template task

---
---
- name: Create a virtual machine from a template
  vmware_guest:
    hostname: "{{vcenterhostname}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: no
    folder: /
    datacenter: "{{datacenter}}"
    name: "{{ ip }}_{{ hostname }}"
    state: poweredon
    esxi_hostname: "{{esxi_hostname}}"
    template: "{{os_version}}"
    disk:
    - size_gb: 51
      type: eagerzeroedthick    ## eagerzeroedthick厚置备零延迟   thin 精简置备
      datastore: "{{datastore}}"  ##存储
    - size_gb: "{{ datasize }}"
      #type: eagerzeroedthick
      datastore: "{{datastore}}"
    hardware:
      hotadd_cpu: true
      hotremove_cpu: true
      hotadd_memory: true
      memory_mb: "{{memory_mb}}"
      num_cpus: "{{num_cpus}}"
      scsi: lsilogic  ##linux用lsilogic,win用lsilogicsas,  默认使用paravirtual  这是VMware自己的准虚拟
    networks:
    - name: "{{vlan}}"  ##网络名
      device_type: vmxnet3  ## win使用e1000e
      ip: "{{ip}}"
      netmask: 255.255.255.0
      gateway: "{{gw}}"
    customization:
      hostname: "{{ hostname }}"
      dns_servers:
      - "{{dns1}}"
      - "{{dns2}}"
    wait_for_ip_address: no
  delegate_to: localhost
  register: deploy

inventory

10.20.14.69_disktest esxi_hostname=10.20.10.56 datastore=56_Localdisk d1_size_gb=50 d2_size_gb=100 memory_mb=16384 num_cpus=8 ip=10.20.14.69 gw=10.20.14.1 dns1=10.20.10.12 dns2=10.20.10.13 vlan=vlan14 hostname=disktest sys_ver=centos6_moban

使用

ansible-playbook -i inventory linux-vm-template

再通过ansible api 添加到django 界面,效率大大提升

ansible并行创建vm虚拟机_第1张图片