Alex Scoble
2014-Apr-28 19:34 UTC
[Puppet Users] Looking for a better way to use hiera hashes than create_resources
Hi All, I'm working on a module that builds KVM/libvirt hosts and populates them with predefined VMs. So far I have the module to where it can create any number of virtual nets, storage pools and volumes using virsh, but it isn't pretty. I've read on various threads here that create_resources is not a good function to use. This was stated quite emphatically by R.I. Pienaar and others. Already I've run into the situation where it's hard to control order in which two separate create_resources functions are run and I've seen some kludges to fix it, but I'm looking for a better way and hoping that it doesn't involve the use of custom types because that's currently more than I want to deal with. Here's what I have and hopefully someone can help me do this in a cleaner way. Sorry that it's not in Github as I'm not ready to put it there yet. *This is an example of the hiera data that I'm working with:* kvm::servertype: 'kvm' kvm::virtnet_name: 'br0' kvm::virtnet_forwardmode: 'bridge' kvm::virtbridge_name: 'br0' kvm::virtnet_macaddress: '52:54:00:1F:95:6C' kvm::virtpool_hash: default: virtpool_size: '429496729601' iso-images: virtpool_target: '/var/lib/libvirt/iso-images' virtpool_format: 'iso' kvm::virtvol_hash: dtlrazorts1.img: volcapacity: '80G' volformat: 'qcow2' dtlrepots1.img: volcapacity: '80G' volformat: 'qcow2' dtlwebvirtmants1.img: volcapacity: '60G' volformat: 'qcow2' *My init.pp* class kvm ( $servertype = $kvm::params::servertype, $virtnet_name = 'undef', $virtnet_forwardmode = 'undef', $virtbridge_name = 'undef', $virtnet_macaddress = 'undef', $virtpool_hash = 'undef', $virtvol_hash = 'undef', ) inherits kvm::params { include kvm::fw if ($servertype == 'kvm') { file { '/var/opt/lib/pe-puppet/temp': ensure => directory, owner => 'pe-puppet', group => 'pe-puppet', } package { ['libvirt', 'python-virtinst', 'qemu-kvm', 'qemu-kvm-tools', 'bridge-utils', 'virt-manager', 'libguestfs-tools',]: ensure => present, } service { 'libvirtd': ensure => running, enable => true, hasstatus => true, hasrestart => true, require => Package['libvirt'], } if $virtpool_hash { create_resources('kvm::virtpool', $virtpool_hash) } include kvm::virtnet if $virtvol_hash { create_resources('kvm::virtvol', $virtvol_hash) } } if $servertype == 'kvmwebmgr' { include kvm::kvmwebmgr } } *The virtpool.pp file:* define kvm::virtpool ( $virtpool = $name, $virtpool_size = undef, $virtpool_target = '/var/lib/libvirt/images', $virtpool_type = 'dir', $virtpool_format = 'raw', ) { file { "${virtpool_target}": ensure => directory, } -> exec { "virsh pool-define-as ${name} --target ${virtpool_target} --type ${virtpool_type} --source-format ${virtpool_format} && virsh pool-autostart ${name} && virsh pool-start ${name}": path => '/usr/bin', unless => "virsh pool-list | /bin/grep ${name}", # refreshonly => true, } } *The virtnet.pp file:* class kvm::virtnet inherits kvm { file { "/var/opt/lib/pe-puppet/temp/${hostname}_virtnet_${virtnet_name}.xml": ensure => file, content => template('kvm/virtnet.xml.erb'), owner => 'pe-puppet', group => 'pe-puppet', } -> exec { 'virsh net-destroy default && virsh net-undefine default': path => '/usr/bin', onlyif => 'virsh net-list | /bin/grep default', } -> exec { "virsh net-define /var/opt/lib/pe-puppet/temp/${hostname}_virtnet_${virtnet_name}.xml && virsh net-autostart ${virtnet_name} && virsh net-start ${virtnet_name}": path => '/usr/bin', unless => "virsh net-list | /bin/grep ${virtnet_name}", } } *The virtvol.pp file:* define kvm::virtvol ( $virtvol = $name, $pool = 'default', $volcapacity = '60G', $volformat = 'qcow2', ) { exec { "virsh vol-create-as ${pool} ${name} ${volcapacity} --format ${volformat}": path => '/usr/bin', unless => "virsh vol-list ${name} | /bin/grep ${name}", onlyif => "virsh pool-list ${pool} | /bin/grep ${pool}", } } *The virtnet.xml.erb:* <network> <name><%= @virtnet_name %></name> <forward mode='<%= @virtnet_forwardmode %>' /> <bridge name='<%= @virtbridge_name %>'/> <mac address='<%= @virtnet_macaddress %>'/> </network> ----------------------------------------------------------------------------- end files ----------------------------------------------------------------------------- It would be super helpful if anyone could point me to a puppet module on github that presents me with a better pattern to use with the hiera data. Any other help or criticisms are also welcome. Thanks, Alex -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c41ed0f0-f7df-446d-861f-2c9bee9693bc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.