diff --git a/defaults/main.yml b/defaults/main.yml index ce8d74b..dc28143 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -253,6 +253,7 @@ kvm_vms: # - disk_driver: 'virtio' # name: 'test_vm.1' # size: '36864' + # image: /tmp/bionic-server-cloudimg-amd64.img # - disk_driver: 'virtio' # name: 'test_vm.2' # size: '51200' diff --git a/library/qemu_img b/library/qemu_img index 37ccc72..794c11e 100755 --- a/library/qemu_img +++ b/library/qemu_img @@ -96,7 +96,7 @@ def main(): module.run_command('%s create -f %s -o %s "%s" %s'%(qemu_img, img_format, opt, dest, size), check_rc=True) changed = True else: - rc, stdout, _ = module.run_command('%s info "%s"'%(qemu_img, dest), check_rc=True) + rc, stdout, _ = module.run_command('%s info -U "%s"'%(qemu_img, dest), check_rc=True) current_size = None for line in stdout.splitlines(): if 'virtual size' in line: diff --git a/playbook.yml b/playbook.yml index 13338e1..7244371 100644 --- a/playbook.yml +++ b/playbook.yml @@ -6,7 +6,15 @@ apt: update_cache: yes cache_valid_time: 3600 - + +- hosts: all + tasks: + - name: download test image + get_url: + url: "https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img" + dest: /tmp/ubuntu.img + +# VM with empty disk - hosts: all become: true vars: @@ -31,3 +39,32 @@ type: 'network' roles: - role: ansible-kvm + +# VM with a defined disk config +- hosts: all + become: true + vars: + kvm_config: true + kvm_manage_vms: true + kvm_disable_apparmor: true + kvm_vms: + - name: 'testhost2' + autostart: true + boot_devices: + - 'hd' + memory: '512' + state: 'running' + vcpu: '1' + disks: + - disk_driver: 'virtio' + name: 'testhost2-disk' + image: '/tmp/ubuntu.img' + size: '8192' +# cdroms: +# - source: /tmp/example.iso + network_interfaces: + - source: 'default' + network_driver: 'virtio' + type: 'network' + roles: + - role: ansible-kvm diff --git a/tasks/config_vms.yml b/tasks/config_vms.yml index 170f4be..7ed6261 100644 --- a/tasks/config_vms.yml +++ b/tasks/config_vms.yml @@ -19,7 +19,7 @@ item['host'] is defined and inventory_hostname == item['host'] -- name: config_vms | Creating VM Disk(s) +- name: config_vms | Creating VM Disk(s), undefined host, empty disk qemu_img: dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" size: "{{ item[1]['size'] }}" @@ -28,9 +28,40 @@ with_subelements: - "{{ kvm_vms }}" - disks - when: item[0]['host'] is not defined + when: > + item[0]['host'] is not defined and + item[1]['image'] is not defined + +- name: config_vms | Creating VM Disk(s), undefined host, primed disk, fetch + copy: + dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" + src: "{{ item[1]['image'] }}" + remote_src: yes + force: no + register: new_image + become: true + with_subelements: + - "{{ kvm_vms }}" + - disks + when: > + item[0]['host'] is not defined and + item[1]['image'] is defined + +- name: config_vms | Creating VM Disk(s), undefined host, primed disk, resize + qemu_img: + dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" + size: "{{ item[1]['size'] }}" + become: true + with_subelements: + - "{{ kvm_vms }}" + - disks + when: > + item[0]['host'] is not defined and + item[1]['image'] is defined and + item[1]['size'] is defined and + new_image.changed -- name: config_vms | Creating VM Disk(s) +- name: config_vms | Creating VM Disk(s), defined host, empty disk qemu_img: dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" size: "{{ item[1]['size'] }}" @@ -43,6 +74,37 @@ item[0]['host'] is defined and inventory_hostname == item[0]['host'] +- name: config_vms | Creating VM Disk(s), defined host, primed disk, fetch + copy: + dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" + src: "{{ item[1]['image'] }}" + remote_src: yes + force: no + register: new_image + become: true + with_subelements: + - "{{ kvm_vms }}" + - disks + when: > + item[0]['host'] is defined and + inventory_hostname == item[0]['host'] and + item[1]['image'] is defined + +- name: config_vms | Creating VM Disk(s), defined host, primed disk, resize + qemu_img: + dest: "{{ kvm_images_path }}/{{ item[1]['name'] }}.{{ kvm_images_format_type }}" + size: "{{ item[1]['size'] }}" + become: true + with_subelements: + - "{{ kvm_vms }}" + - disks + when: > + item[0]['host'] is defined and + inventory_hostname == item[0]['host'] and + item[1]['image'] is defined and + item[1]['size'] is defined and + new_image.changed + - name: config_vms | Setting VM State virt: name: "{{ item['name'] }}" diff --git a/templates/vm-template.xml.j2 b/templates/vm-template.xml.j2 index 8056550..28964c8 100644 --- a/templates/vm-template.xml.j2 +++ b/templates/vm-template.xml.j2 @@ -28,6 +28,13 @@ {% endif %} {% endfor %} +{% for cdrom in item.cdroms|default([]) %} + + + + + +{% endfor %} {% for int in item.network_interfaces %} {% if int['portgroup'] is not defined %}