From 38fe6805a43ffa3ed83e2c939c0d29d0ab743bff Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Wed, 6 Jun 2018 23:55:05 -0600 Subject: [PATCH] Add Libvirt support Add support for Libvirt Signed-off-by: Scott Lowe --- kvm/kvm-generic/Vagrantfile | 42 ++++++++++++++++++++++------------- kvm/kvm-generic/ansible.cfg | 2 +- kvm/kvm-generic/machines.yml | 12 ++++++---- kvm/kvm-generic/provision.yml | 6 ++++- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/kvm/kvm-generic/Vagrantfile b/kvm/kvm-generic/Vagrantfile index a0408dc..c150bf9 100644 --- a/kvm/kvm-generic/Vagrantfile +++ b/kvm/kvm-generic/Vagrantfile @@ -5,14 +5,10 @@ Vagrant.require_version '>= 1.6.0' VAGRANTFILE_API_VERSION = '2' -# Explicitly specify the VMware provider (req'd for nested virtualization) -ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion' - # Require 'yaml' module require 'yaml' -# Read YAML file with VM details (box, CPU, RAM, IP addresses) -# Edit machines.yml to change VM configuration details +# Read YAML file with VM details (box, CPU, and RAM) machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml')) # Create and configure the VMs @@ -30,11 +26,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Don't check for box updates srv.vm.box_check_update = false - # Specify the hostname of the VM + # Set machine's hostname srv.vm.hostname = machine['name'] - # Specify the Vagrant box to use (must use VMware box) - srv.vm.box = machine['vmw_box'] + # Use VMware box by default + srv.vm.box = machine['box']['vmw'] # Configure default synced folder (disable by default) if machine['sync_disabled'] != nil @@ -43,10 +39,14 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| srv.vm.synced_folder '.', '/vagrant', disabled: true end #if machine['sync_disabled'] - # Assign additional private network - if machine['ip_addr'] != nil - srv.vm.network 'private_network', ip: machine['ip_addr'] - end # if machine['ip_addr'] + # Iterate through networks as per settings in machines.yml + machine['nics'].each do |net| + if net['ip_addr'] == 'dhcp' + srv.vm.network net['type'], type: net['ip_addr'] + else + srv.vm.network net['type'], ip: net['ip_addr'] + end # if net['ip_addr'] + end # machine['nics'].each # Configure CPU & RAM per settings in machines.yml (Fusion) srv.vm.provider 'vmware_fusion' do |vmw| @@ -57,10 +57,20 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| vmw.vmx['vhv.enable'] = 'TRUE' end # srv.vm.provider 'vmware_fusion' - # Provision the VM with Ansible - config.vm.provision 'ansible' do |ansible| - ansible.playbook = 'provision.yml' - end # config.vm.provision + # Configure CPU & RAM per settings in machines.yml (Libvirt) + srv.vm.provider 'libvirt' do |lv,override| + lv.memory = machine['ram'] + lv.cpus = machine['vcpu'] + override.vm.box = machine['box']['lv'] + + # Enable nested virtualization (required for KVM) + lv.nested = true + end # srv.vm.provider 'libvirt' end # config.vm.define end # machines.each + + # Provision the VM with Ansible + config.vm.provision 'ansible' do |ansible| + ansible.playbook = 'provision.yml' + end # config.vm.provision end # Vagrant.configure diff --git a/kvm/kvm-generic/ansible.cfg b/kvm/kvm-generic/ansible.cfg index b3a4567..a975d51 100644 --- a/kvm/kvm-generic/ansible.cfg +++ b/kvm/kvm-generic/ansible.cfg @@ -2,4 +2,4 @@ inventory = ./.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory private_key_file = ~/.vagrant.d/insecure_private_key remote_user = vagrant -host_key_checking = False \ No newline at end of file +host_key_checking = False diff --git a/kvm/kvm-generic/machines.yml b/kvm/kvm-generic/machines.yml index 7f3efa6..16e22ed 100644 --- a/kvm/kvm-generic/machines.yml +++ b/kvm/kvm-generic/machines.yml @@ -1,7 +1,11 @@ --- -- name: "kvm-01" - vmw_box: "bento/ubuntu-14.04" +- box: + vmw: "bento/ubuntu-16.04" + lv: "generic/ubuntu1604" + name: "kvm-01" + nics: + - type: "private_network" + ip_addr: "dhcp" ram: "1024" + sync_disabled: true vcpu: "1" - ip_addr: "192.168.100.100" - nested: true diff --git a/kvm/kvm-generic/provision.yml b/kvm/kvm-generic/provision.yml index 44e219f..e069a17 100644 --- a/kvm/kvm-generic/provision.yml +++ b/kvm/kvm-generic/provision.yml @@ -1,9 +1,13 @@ --- - hosts: "all" - sudo: "yes" + become: "yes" remote_user: "vagrant" + gather_facts: false tasks: + - name: "Install Python 2" + raw: "apt-get -y -q install python" + - name: "Install KVM and Libvirt" apt: state: "present"