0
  • 问题陈述:
    1. 从模板克隆虚拟机。
    2. 为创建的 VM 分配 IP 地址。

我正在使用 Ansible 来完成这项工作。以下是从指定模板克隆 VM 的脚本:

---
- hosts: localhost
  connection: local
  sudo: false
  user: root
  gather_facts: false
  serial: 1

  vars_files:
    - createVmVars.yml

  tasks:
    - name: Deploying VM from template.
      vsphere_guest:
        vcenter_hostname: "{{vcenter_hostname}}"
        username: "{{vcenter_username}}"
        password: "{{vcenter_password}}"
        guest: "{{guest_name}}"
        from_template: yes
        template_src: "{{template_src}}"
        cluster: "{{cluster}}"
        resource_pool: "{{resource_pool}}"
        vm_extra_config:
          folder: "{{folder_name}}"

我想扩展此脚本以将 IP 地址(和其他 n/w 配置)分配给新创建的 VM。

4

2 回答 2

0

您可以在 vm_extra_config 中指定静态 IP 地址

    .
    .
    .
    cluster: "{{cluster}}"
    resource_pool: "{{resource_pool}}"
    vm_extra_config:
      folder: "{{folder_name}}"
      ip_address: "{{my_static_ip}}"
    .
    .

我无法找到其他网络设置,例如网络掩码和网关。vsphere_guest 或 pysphere 模块上都没有这样的描述

于 2016-04-09T12:58:05.310 回答
0

经过大量搜索,我现在只有使用 python 直接将网络配置写入适当文件的唯一选项。以下对我来说很好。您可以使用带有所需参数的 Shell 模块从 Ansible 运行此脚本。根据您的需要更改它:

import subprocess
import sys
import shutil
import os

class NetworkConfiguration:
        def __init__(self):
                self.ntype=""
                self.hostname=""
                self.domain=""
                self.pdns=""
                self.sdns=""
                self.ip=""
                self.netmask=""
                self.gway=""
                self.files=["/etc/sysconfig/network/ifcfg-eth0",
                                "/etc/sysconfig/network/routes",
                                "/etc/resolv.conf",
                                "/etc/HOSTNAME"
                                ]

        def writeIntoFile(self,filename,s,mode):
                f=open(filename,mode)
                f.write(s)
                f.close()

        def buildString(self,filename):
                ret_str=""
                if filename == self.files[0] :
                        ret_str="DEVICE=eth0"
                        ret_str+="\n"+"STARTMODE=auto"
                        ret_str+="\n"+"BOOTPROTO="
                        if self.ntype=="s":
                                ret_str+="static"
                        else:
                                ret_str+="dhcp"
                        ret_str+="\n"+"USERCONTROL=no"
                        ret_str+="\n"+"ONBOOT=yes"
                        if self.ntype=="s":
                                ret_str+="\n"+"IPADDR="+self.ip
                                ret_str+="\n"+"NETMASK="+self.netmask
                elif filename==self.files[1]:
                        if self.ntype=="s":
                                ret_str="defualt "+self.gway+" - -"
                elif filename==self.files[2]:
                        ret_str="search "+self.domain
                        ret_str+="\n"+"nameserver "+self.pdns
                        if self.sdns!="":
                                ret_str+="\n"+"nameserver "+self.sdns
                elif filename==self.files[3]:
                        ret_str=self.hostname+"."+self.domain

                return ret_str


        def runProcess(self,args):
                p=subprocess.Popen(args,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
                return p.communicate()
#------------------------Class Ends Here---------------------------------------


nCog=NetworkConfiguration()

# Recieving arguments from commmand line.
#sequence is: network_type,hostname,domain,primary_dns,ip,netmask,gateway,secondry_dns
# note : IP, Netmask, gateway are not required in case of DHCP.
try:
        nCog.ntype=sys.argv[1]
        if nCog.ntype=="s" and  len(sys.argv)<8:
                raise Exception("Too few arguments passed for static configuration!!!")
        elif nCog.ntype=="d" and len(sys.argv)<5:
                raise Exception("Too few arguments passed for DHCP configuration!!!")
        elif nCog.ntype!="s" and nCog.ntype!="d":
                raise Exception("Not a valid network type!!!")

        nCog.hostname=sys.argv[2]
        nCog.domain=sys.argv[3]
        nCog.pdns=sys.argv[4]

        if nCog.ntype=="s":
                nCog.ip=sys.argv[5]
                nCog.netmask=sys.argv[6]
                nCog.gway=sys.argv[7]
                if len(sys.argv)==9:
                        nCog.sdns=sys.argv[8]
        elif len(sys.argv)==6:
                nCog.sdns=sys.argv[5]

except Exception, e:
        print(e)

# Writing network configurations into the appropriate files.
for f in nCog.files:
        ret_str=nCog.buildString(f)
        nCog.writeIntoFile(f,ret_str,"w")

# Setting the hostname
o, e=nCog.runProcess(['hostname',nCog.hostname])

# restart the network
o, e=nCog.runProcess(['service','network restart'])
print("restarting the network:\n------------------------------------")
print("output: "+o+"\n error: "+e)

# Adding gateway ip to the routing table in case static ip is assigned.
print os.system("sudo ip route add default via %s" % (nCog.gway))
于 2016-04-11T06:06:00.613 回答