1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add cloudinit support to ovirt.py module

This commit is contained in:
Karim Boumedhel 2016-04-15 20:23:37 +02:00 committed by Matt Clay
parent 567bcf1341
commit e57ef71ed3

View file

@ -144,6 +144,48 @@ options:
default: null default: null
required: false required: false
aliases: [] aliases: []
instance_dns:
description:
- define the instance's Primary DNS server
required: false
aliases: [ dns ]
version_added: "2.1"
instance_domain:
description:
- define the instance's Domain
required: false
aliases: [ domain ]
version_added: "2.1"
instance_hostname:
description:
- define the instance's Hostname
required: false
aliases: [ hostname ]
version_added: "2.1"
instance_ip:
description:
- define the instance's IP
required: false
aliases: [ ip ]
version_added: "2.1"
instance_netmask:
description:
- define the instance's Netmask
required: false
aliases: [ netmask ]
version_added: "2.1"
instance_rootpw:
description:
- define the instance's Root password
required: false
aliases: [ rootpw ]
version_added: "2.1"
instance_key:
description:
- define the instance's Authorized key
required: false
aliases: [ key ]
version_added: "2.1"
state: state:
description: description:
- create, terminate or remove instances - create, terminate or remove instances
@ -205,6 +247,19 @@ ovirt:
password: secret password: secret
url: https://ovirt.example.com url: https://ovirt.example.com
# starting an instance with cloud init information
ovirt:
instance_name: testansible
state: started
user: admin@internal
password: secret
url: https://ovirt.example.com
hostname: testansible
domain: ansible.local
ip: 192.168.1.100
netmask: 255.255.255.0
gateway: 192.168.1.1
rootpw: bigsecret
''' '''
@ -273,9 +328,23 @@ def create_vm_template(conn, vmname, image, zone):
# start instance # start instance
def vm_start(conn, vmname): def vm_start(conn, vmname, hostname=None, ip=None, netmask=None, gateway=None,
domain=None, dns=None, rootpw=None, key=None):
vm = conn.vms.get(name=vmname) vm = conn.vms.get(name=vmname)
vm.start() use_cloud_init = False
nics = None
if hostname or ip or netmask or gateway or domain or dns or rootpw or key:
use_cloud_init = True
if ip and netmask and gateway:
ipinfo = params.IP(address=ip, netmask=netmask, gateway=gateway)
nic = params.GuestNicConfiguration(name='eth0', boot_protocol='STATIC', ip=ipinfo, on_boot=True)
nics = params.Nics()
nics = params.GuestNicsConfiguration(nic_configuration=[nic])
initialization=params.Initialization(regenerate_ssh_keys=True, host_name=hostname, domain=domain, user_name='root',
root_password=rootpw, nic_configurations=nics, dns_servers=dns,
authorized_ssh_keys=key)
action = params.Action(use_cloud_init=use_cloud_init, vm=params.VM(initialization=initialization))
vm.start(action=action)
# Stop instance # Stop instance
def vm_stop(conn, vmname): def vm_stop(conn, vmname):
@ -302,7 +371,6 @@ def vm_remove(conn, vmname):
# Get the VMs status # Get the VMs status
def vm_status(conn, vmname): def vm_status(conn, vmname):
status = conn.vms.get(name=vmname).status.state status = conn.vms.get(name=vmname).status.state
print "vm status is : %s" % status
return status return status
@ -311,10 +379,8 @@ def get_vm(conn, vmname):
vm = conn.vms.get(name=vmname) vm = conn.vms.get(name=vmname)
if vm == None: if vm == None:
name = "empty" name = "empty"
print "vmname: %s" % name
else: else:
name = vm.get_name() name = vm.get_name()
print "vmname: %s" % name
return name return name
# ------------------------------------------------------------------- # # ------------------------------------------------------------------- #
@ -347,6 +413,14 @@ def main():
disk_int = dict(default='virtio', choices=['virtio', 'ide']), disk_int = dict(default='virtio', choices=['virtio', 'ide']),
instance_os = dict(aliases=['vmos']), instance_os = dict(aliases=['vmos']),
instance_cores = dict(default=1, aliases=['vmcores']), instance_cores = dict(default=1, aliases=['vmcores']),
instance_hostname = dict(aliases=['hostname']),
instance_ip = dict(aliases=['ip']),
instance_netmask = dict(aliases=['netmask']),
instance_gateway = dict(aliases=['gateway']),
instance_domain = dict(aliases=['domain']),
instance_dns = dict(aliases=['dns']),
instance_rootpw = dict(aliases=['rootpw']),
instance_key = dict(aliases=['key']),
sdomain = dict(), sdomain = dict(),
region = dict(), region = dict(),
) )
@ -375,6 +449,14 @@ def main():
vmcores = module.params['instance_cores'] # number of cores vmcores = module.params['instance_cores'] # number of cores
sdomain = module.params['sdomain'] # storage domain to store disk on sdomain = module.params['sdomain'] # storage domain to store disk on
region = module.params['region'] # oVirt Datacenter region = module.params['region'] # oVirt Datacenter
hostname = module.params['instance_hostname']
ip = module.params['instance_ip']
netmask = module.params['instance_netmask']
gateway = module.params['instance_gateway']
domain = module.params['instance_domain']
dns = module.params['instance_dns']
rootpw = module.params['instance_rootpw']
key = module.params['instance_key']
#initialize connection #initialize connection
try: try:
c = conn(url+"/api", user, password) c = conn(url+"/api", user, password)
@ -405,7 +487,8 @@ def main():
if vm_status(c, vmname) == 'up': if vm_status(c, vmname) == 'up':
module.exit_json(changed=False, msg="VM %s is already running" % vmname) module.exit_json(changed=False, msg="VM %s is already running" % vmname)
else: else:
vm_start(c, vmname) #vm_start(c, vmname)
vm_start(c, vmname, hostname, ip, netmask, gateway, domain, dns, rootpw, key)
module.exit_json(changed=True, msg="VM %s started" % vmname) module.exit_json(changed=True, msg="VM %s started" % vmname)
if state == 'shutdown': if state == 'shutdown':