码迷,mamicode.com
首页 > 系统相关 > 详细

ansible创建vmware虚拟机

时间:2018-10-15 17:04:53      阅读:1130      评论:0      收藏:0      [点我收藏+]

标签:手动   win   with   精简   python脚本   直接   register   验证   log   

环境:vmware 虚拟化
需求:业务部门一次提几十台甚至几百台虚拟机需求

之前不管这一块,由于人员减少,临时需求也要搞搞了

使用ansible vmware_guest 创建虚拟机,避免手动一台一台创建的纯手工 工作
废话不多说,上代码

---
- hosts: localhost   ## 在本机执行
  gather_facts: No
  connection: local  ## 本地连接
  vars_files:
    - myvars.yml
    - vcenter.yml
  tasks:
  - name: Create a virtual machine on given ESXi hostname
    vmware_guest:
      hostname: "{{hostname}}"
      username: "{{username}}"
      password: "{{password}}"
      validate_certs: no
      folder: /
      datacenter: "{{datacenter}}"
      name: "{{item.name}}"
      state: poweredoff
      guest_id: centos64Guest
      esxi_hostname: "{{item.esxi_hostname}}"
      disk:
      - size_gb: "{{ item.d1_size_gb}}"
        type: eagerzeroedthick    ##厚置备    thin 精简置备
        datastore: "{{item.datastore}}"  ##存储
      - size_gb: "{{ item.d2_size_gb}}"
        #type: eagerzeroedthick
        type: thin
        datastore: "{{item.datastore}}"
      hardware:
        hotadd_cpu: true
        hotremove_cpu: true
        hotadd_memory: true
        memory_mb: "{{item.memory_mb}}"
        num_cpus: "{{item.num_cpus}}"
        scsi: lsilogic  ##linux用lsilogic,win用lsilogicsas,  默认使用paravirtual  这是VMware自己的准虚拟
      networks:
      - name: vlan10  ##网络名
        device_type: vmxnet3  ## win使用e1000e
    with_items:
      - "{{myvms}}"
    delegate_to: localhost
    register: deploy_vm

vcenter.yml 存放vcenter验证信息
myvars.yml 是需要创建的虚拟机信息
myvars.yml 示例如下


---
datacenter: zm-A10  
myvms:
 - { name: 10.20.15.99_test1, esxi_hostname: 10.20.10.26, datastore: 10-26-localradi10, d1_size_gb: 50, d2_size_gb: 30, memory_mb: 8196, num_cpus: 4 }
 - { name: 10.20.15.105_test2, esxi_hostname: 10.20.10.23, datastore: 10_23-localraid10, d1_size_gb: 50, d2_size_gb: 40, memory_mb: 8196, num_cpus: 4 }

myvars.yml 可以直接用Python脚本生成

import re

outfile2 = open(‘myvars.yml‘, ‘w‘)
file_head = ‘‘‘ 
---
datacenter: zm-A10
myvms:
‘‘‘
outfile2.write(file_head)
with open(‘vm_list‘, ‘r‘) as f:
    while 1:
        line = f.readline()
        if not line:
            break
        linetest = re.split(r‘[;,\s]+‘, line)
        host2text = ‘‘‘ - { name: %s_%s, esxi_hostname: %s, datastore: %s, d1_size_gb: %s, d2_size_gb: %s, memory_mb: %s, num_cpus: %s }‘‘‘ % (linetest[0], linetest[1], linetest[2], linetest[3], linetest[6], linetest[7], linetest[5], linetest[4])
        print >> outfile2, host2text

ansible创建vmware虚拟机

标签:手动   win   with   精简   python脚本   直接   register   验证   log   

原文地址:http://blog.51cto.com/qingwa/2300172

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!