方法一:pysphere

环境

pysphere0.1.7
vcenter6.7

set.py

自动配置ip地址以及修改root密码

import ssl
import re
import time
from pysphere import VIServer,VIProperty

# 密码及IP地址更改由脚本实现
centos_path="/root/set.sh"
centos_args1=["172.17.22.2","255.255.255.128","172.17.22.126","新密码"]
centos_args2=["172.17.22.3","255.255.255.128","172.17.22.126","新密码"]
centos_args3=["172.17.22.4","255.255.255.128","172.17.22.126","新密码"]

#ubuntu_path="/tmp/set.sh"
#ubuntu_args=["[172.23.3.51'\/'24]","172.23.3.1","新密码"]

ssl._create_default_https_context = ssl._create_unverified_context
server = VIServer()

#server.connect('192.168.110.100','administrator@vsphere.local','密码')
#print server.get_api_version()

virtual_machines = server.get_registered_vms()
#print(virtual_machines)

for i in virtual_machines:
     result1 = re.findall(r'.*shszmkt+\.vmx',i)
     if result1:
          # print (result) # 返回值类型list
          # print (result[0])
          vm=server.get_vm_by_path(result1[0])
          # print(vm.get_status())
          vm.login_in_guest("root","旧密码")
          pid1=vm.start_process(centos_path,args=centos_args1)
          # pid1=vm.start_process(ubuntu_path,args=ubuntu_args)
          # time.sleep(5)

     result2 = re.findall(r'.*othmrk+\.vmx',i)
     if result2:
          vm=server.get_vm_by_path(result2[0])
          vm.login_in_guest("root","旧密码")
          pid2=vm.start_process(centos_path,args=centos_args2)
          #pid2=vm.start_process(ubuntu_path,args=ubuntu_args)
     
     result3 = re.findall(r'.*storage+\.vmx',i)
     if result3:
          vm=server.get_vm_by_path(result3[0])
          vm.login_in_guest("root","旧密码")
          pid3=vm.start_process(centos_path,args=centos_args3)
          #pid3=vm.start_process(ubuntu_path,args=ubuntu_args)
"""
for i in virtual_machines:
     result = re.findall(r'.*linuxtest+\.vmx',i)
     if result:
          vm=server.get_vm_by_path(result[0])
          vm.login_in_guest("root","新密码")
          vm.delete_file('/root/set.sh')
"""

shell脚本

#!/bin/bash


# centos7,执行方法:./set.sh "地址" "掩码" "网关" "密码" 

# sed -i "s/dhcp/static/g" /etc/sysconfig/network-scripts/ifcfg-ens160
# sed -i "s/ONBOOT=no/ONBOOT=yes/g" /etc/sysconfig/network-scripts/ifcfg-ens192
echo ”IPADDR=$1
NETMASK=$2
GATEWAY=$3
DNS1=8.8.8.8
DNS2=114.114.114.114“ >> /etc/sysconfig/network-scripts/ifcfg-ens160
systemctl restart network
echo $4 | passwd root --stdin

ubuntu18.04系统,set.sh单独执行:

#!/bin/bash

# ubuntu18.04,执行方法:./set.sh "[地址\/掩码]" "网关" "密码"
sed -i "0,/addresses:/s/addresses:/& $1/" /etc/netplan/50-cloud-init.yaml
sed -i "s/gateway4:/& $2/" /etc/netplan/50-cloud-init.yaml
netplan apply
echo root:$3 | chpasswd

方法二(推荐):powercli

环境

# 在线安装nuget
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201
# 离线安装:将已下载的文件放置于C:\Users\Administrator\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll

官网下载,放置于C:\Program Files\WindowsPowerShell\Modules

# 导入
Import-Module vmware.powercli

csv模板

host datastore command numofcpu numofmem numofdisk
ip1 存储名称 cd /root && ./set.sh ‘ip1’ ‘掩码’ ‘网关’ ‘新密码’ 2 4 100
ip2 存储名称 cd /root && ./set.sh ‘ip2’ ‘掩码’ ‘网关’ ‘新密码’ 4 8 500
ip3 存储名称 cd /root && ./set.sh ‘ip3’ ‘掩码’ ‘网关’ ‘新密码’ 8 16 900

ps脚本-创建虚拟机

# Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
# Connect-VIServer -Server 地址 -Protocol https -Username 'administrator@vsphere.local' -Password '密码'

$vms = Import-CSV vminfo.csv

foreach ($i in $vms){

      $task1 = New-VM -Name $i.name -Template $i.template -VMHost $i.host -Datastore $i.datastore -RunAsync
	  Wait-Task -Task $task1
	  
	  Set-VM $i.name -NumCpu $i.numofcpu -MemoryGB $i.numofmem -Confirm:$False
	  
	  If($i.numofdisk)
	  {
      New-HardDisk -VM $i.name -CapacityGB $i.numofdisk -DataStore $i.datastore -StorageFormat Thin2GB
	  start-vm $i.name
      }
      Else
	  {
      start-vm $i.name
	  }
	  
}

ps脚本-配置网络

$vms = Import-CSV vminfo.csv
$rootpass = "aR023zP2t"

foreach ($i in $vms){
	  
	  Invoke-VMScript -VM $i.name -GuestUser root -GuestPassword $rootpass -ScriptType Bash -ScriptText $i.command
}

set-centos.sh

#!/bin/bash
sed -i "s/^IPADDR=.*/IPADDR=$1/" /etc/sysconfig/network-scripts/ifcfg-enxxxx
sed -i "s/^NETMASK=.*/NETMASK=$2/" /etc/sysconfig/network-scripts/ifcfg-enxxxx
sed -i "s/^GATEWAY=.*/GATEWAY=$3/" /etc/sysconfig/network-scripts/ifcfg-enxxxx

systemctl restart network
echo $4 | passwd root --stdin

set-ubuntu.sh

#!/bin/bash
sed -i "0,/addresses:.*/s/addresses:.*/addresses: $1/" /etc/netplan/50-cloud-init.yaml
sed -i "s/gateway4:.*/gateway4: $2/" /etc/netplan/50-cloud-init.yaml
netplan apply
echo root:$3 | chpasswd

ps脚本-创建快照

$vms = Import-CSV snapshotinfo.csv

foreach ($i in $vms){

	  $task1 = New-Snapshot -VM $i.name -Name 1216bak -Confirm:$False -RunAsync
	  Wait-Task -Task $task1
}
Logo

一站式 AI 云服务平台

更多推荐