以前的腳本是串行的,效率並不高
更改成並行模式linux
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.cfgdjango
[defaults] # some basic default values... library = ./library # additional paths to search for roles in, colon separated roles_path = ./roles
linux-vm-templatecentos
--- - hosts: all gather_facts: false vars_files: - vcenter.yml roles: - linux_vm_template
linux-vm-template taskapi
--- --- - 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
使用 ide
ansible-playbook -i inventory linux-vm-template
再經過ansible api 添加到django 界面,效率大大提高code