基于ansible批量创建虚拟机
安装模块
pip install --upgrade ansible
pip install PyVmomi
ansible-galaxy collection install community.vmware
ansible vmware_guest 如果需要配置IP和计算机名需要安装VMware tools
---
- hosts: localhost ## 在本机执行
gather_facts: No
connection: local ## 本地连接
vars_files:
- myvars.yml
- vcenter.yml
tasks:
- name: Create a virtual machine from a template
vmware_guest:
hostname: "{{hostname}}"
username: "{{username}}"
password: "{{password}}"
validate_certs: no
folder: /
datacenter: "{{datacenter}}"
name: "{{item.name}}"
state: poweredon
esxi_hostname: "{{item.esxi_hostname}}"
template: "{{item.sys_ver}}"
disk:
- size_gb: 51
type: eagerzeroedthick ## eagerzeroedthick厚置备零延迟 thin 精简置备
datastore: "{{item.datastore}}" ##存储
- size_gb: "{{ item.d2_size_gb}}"
#type: eagerzeroedthick
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: "{{item.vlan}}" ##网络名
device_type: vmxnet3 ## win使用e1000e
ip: "{{item.ip}}"
netmask: 255.255.255.0
gateway: "{{item.gw}}"
customization:
hostname: "{{item.hostname}}"
dns_servers:
- "{{item.dns1}}"
- "{{item.dns2}}"
with_items:
- "{{myvms}}"
delegate_to: localhost
register: deploy
vcenter.yml 存放vcenter验证信息
# vcenter.yml
hostname: "www.qq.com" # Replace with your vCenter server hostname or IP address
username: "[email protected]" # Replace with your vCenter username
password: "Raywing@host8." # Replace with your vCenter password
datacenter: "Datacenter" # Replace with your vCenter datacenter name
myvars.yml 是需要创建的虚拟机信息
myvars.yml 示例如下
---
datacenter: center
myvms:
- name: x.x.x.x_xx #vcenter上显示的机器名
esxi_hostname: x.x.x.x
datastore: x
d1_size_gb: 50
d2_size_gb: 100
memory_mb: 16384
num_cpus: 8
ip:
gw:
dns1:
dns2:
vlan: vlan14
hostname: xxxx #克隆后机器的主机名
sys_ver: centos6_moban
- name: xxx
esxi_hostname: xx
datastore: xx
d1_size_gb: 50
d2_size_gb: 100
memory_mb: 8192
num_cpus: 4
ip:
gw:
dns1:
dns2:
vlan: vlan14
hostname: x
sys_ver: centos7_moban
myvars.yml 可以直接用Python脚本生成
import re
outfile2 = open('myvars.yml', 'w')
file_head = '''
---
datacenter: center
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)
if linetest[12] is not None and linetest[12] != '' and linetest[12] == 'centos6':
sys_version = 'centos6_moban'
elif linetest[12] is not None and linetest[12] != '' and linetest[12] == 'centos7':
sys_version = 'centos7_moban'
else:
sys_version = 'win2012_moban'
if linetest[5] is not None and linetest[5].isdigit():
memory = int(linetest[5]) * 1024
host2text = '''
- name: {0}_{1}
esxi_hostname: {2}
datastore: {3}
d1_size_gb: {4}
d2_size_gb: {5}
memory_mb: {6}
num_cpus: {7}
ip: {8}
gw: {9}
dns1: {10}
dns2: {11}
vlan: {12}
hostname: {13}
sys_ver: {14}'''.format(linetest[0], linetest[1], linetest[2], linetest[3], linetest[6], linetest[7], memory, linetest[4], linetest[0], linetest[8], linetest[9], linetest[10], linetest[11], linetest[1], sys_version)
print >> outfile2, host2text
安装完后的机器截图
查看虚拟机信息
[root@localhost vmware]# ansible-playbook -C test.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *********************************************************************************************************************************************************************************************
TASK [Get VM information] ************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
"vm_info": {
"changed": false,
"changes": {},
"failed": false,
"instance": {
"advanced_settings": {
"RemoteDisplay.maxConnections": "-1",
"cpuid.coresPerSocket.cookie": "1",
"ethernet0.pciSlotNumber": "192",
"hpet0.present": "TRUE",
"migrate.hostLog": "VM1-62db1586.hlog",
"migrate.hostLogState": "none",
"migrate.migrationId": "0",
"monitor.phys_bits_used": "45",
"numa.autosize.cookie": "10012",
"numa.autosize.vcpu.maxPerVirtualNode": "1",
"nvram": "VM1.nvram",
"pciBridge0.pciSlotNumber": "17",
"pciBridge0.present": "TRUE",
"pciBridge4.functions": "8",
"pciBridge4.pciSlotNumber": "21",
"pciBridge4.present": "TRUE",
"pciBridge4.virtualDev": "pcieRootPort",
"pciBridge5.functions": "8",
"pciBridge5.pciSlotNumber": "22",
"pciBridge5.present": "TRUE",
"pciBridge5.virtualDev": "pcieRootPort",
"pciBridge6.functions": "8",
"pciBridge6.pciSlotNumber": "23",
"pciBridge6.present": "TRUE",
"pciBridge6.virtualDev": "pcieRootPort",
"pciBridge7.functions": "8",
"pciBridge7.pciSlotNumber": "24",
"pciBridge7.present": "TRUE",
"pciBridge7.virtualDev": "pcieRootPort",
"sata0.pciSlotNumber": "32",
"sched.cpu.latencySensitivity": "normal",
"scsi0.pciSlotNumber": "160",
"scsi0.sasWWID": "50 05 05 6b cd 57 fa 70",
"softPowerOff": "FALSE",
"svga.guestBackedPrimaryAware": "TRUE",
"svga.present": "TRUE",
"tools.guest.desktop.autolock": "TRUE",
"tools.remindInstall": "TRUE",
"viv.moid": "1e091526-9d10-4572-9544-c0de1009e712:vm-17:FkTEKnLaZpO68ge8P5LN6ssRcpTvxlsW8QDA037ERdc=",
"vmotion.checkpointFBSize": "8388608",
"vmotion.checkpointSVGAPrimarySize": "8388608",
"vmotion.svga.graphicsMemoryKB": "8192",
"vmotion.svga.mobMaxSize": "8388608",
"vmware.tools.internalversion": "0",
"vmware.tools.requiredversion": "12384",
"vmxstats.filename": "centos7.scoreboard"
},
"annotation": "",
"current_snapshot": null,
"customvalues": {},
"guest_consolidation_needed": false,
"guest_question": null,
"guest_tools_status": "guestToolsNotRunning",
"guest_tools_version": "0",
"hw_cluster": null,
"hw_cores_per_socket": 1,
"hw_datastores": [
"data"
],
"hw_esxi_host": "192.168.17.5",
"hw_eth0": {
"addresstype": "assigned",
"ipaddresses": null,
"label": "Network adapter 1",
"macaddress": "00:50:56:b9:42:aa",
"macaddress_dash": "00-50-56-b9-42-aa",
"portgroup_key": null,
"portgroup_portkey": null,
"summary": "VM Network"
},
"hw_files": [
"[data] VM1/VM1.vmx",
"[data] VM1/VM1.nvram",
"[data] VM1/VM1.vmsd",
"[data] VM1/VM1.vmdk"
],
"hw_folder": "/Datacenter/vm",
"hw_guest_full_name": null,
"hw_guest_ha_state": null,
"hw_guest_id": null,
"hw_interfaces": [
"eth0"
],
"hw_is_template": false,
"hw_memtotal_mb": 2048,
"hw_name": "VM1",
"hw_power_status": "poweredOff",
"hw_processor_count": 1,
"hw_product_uuid": "42392d62-ccc0-bf90-bde8-186ec267d492",
"hw_version": "vmx-21",
"instance_uuid": "50394019-b544-53c7-2f59-649dfa65551d",
"ipv4": null,
"ipv6": null,
"module_hw": true,
"moid": "vm-17",
"snapshots": [],
"tpm_info": {
"provider_id": null,
"tpm_present": false
},
"vimref": "vim.VirtualMachine:vm-17",
"vnc": {}
}
}
}
Create-virtual-machine-esxi.yml
---
- hosts: localhost
gather_facts: no
vars:
vcenter_hostname: 'www.qq.com'
vcenter_username: '[email protected]'
vcenter_password: 'Raywing@host8.'
datacenter_name: 'Datacenter' #中心名称
esxi_hostname: '192.168.17.5' #esxi主机
tasks:
- name: Create a virtual machine on given ESXi hostname
vmware_guest:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
name: vm-03
guest_id: centos7_64Guest #虚拟机模板
datacenter: '{{ datacenter_name }}'
folder: /
datastore: data
disk:
- size_gb: 10
type: thin #磁盘精简模式
datastore: data #硬盘名称
hardware:
memory_mb: 2048
num_cpus: 2
scsi: paravirtual
networks:
- name: VM Network
mac: aa:bb:dd:aa:00:14
ip: 192.168.17.27
netmask: 255.255.255.0
gateway: 192.168.17.254
type: static
domain:
dns_servers:
- 8.8.8.8
- 9.9.9.9
device_type: vmxnet3
state: poweredoff
wait_for_ip_address: yes
delegate_to: localhost
vms_create.yaml
---
- hosts: localhost
#connection: localhost
gather_facts: false
#collections:
# - community.vmware
tasks:
- name: Create Virtual Machines
vmware_guest:
hostname: www.qq.com
username: [email protected]
password: Raywing@host8.
validate_certs: false
datacenter: Datacenter
folder: /
name: "{{ item.name }}"
state: poweredoff
guest_id: "{{ item.guest_id }}"
hardware:
memory_mb: "{{ item.memory }}"
num_cpus: "{{ item.cpus }}"
networks:
- name: "{{ item.network }}"
disk:
- size_gb: "{{ item.disk_size }}"
datastore: "{{ item.datastore }}"
with_items:
- { name: "vm-02", guest_id: "centos7_64Guest", memory: 2048, cpus: 2, network: "VM Network", disk_size: 10, datastore: "data" }
标签:name,linetest,虚拟机,hostname,hw,item,ansible,VCSA,guest
From: https://www.cnblogs.com/suixinnan/p/18410698