Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Commit

Permalink
Added disk image: parameter.
Browse files Browse the repository at this point in the history
Added cdroms for VMs.
Fixed problems with write lokcing in qemu_img.
  • Loading branch information
David Johnson authored and David Johnson committed Jan 10, 2019
1 parent 20a5bf5 commit c018f03
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion library/qemu_img
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 38 additions & 1 deletion playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
68 changes: 65 additions & 3 deletions tasks/config_vms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }}"
Expand All @@ -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'] }}"
Expand All @@ -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'] }}"
Expand Down
7 changes: 7 additions & 0 deletions templates/vm-template.xml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
{% endif %}
</disk>
{% endfor %}
{% for cdrom in item.cdroms|default([]) %}
<disk type='file' device='cdrom'>
<source file='{{ cdrom.source }}' />
<target dev='hda' bus='ide'/>
<readonly/>
</disk>
{% endfor %}
{% for int in item.network_interfaces %}
<interface type='{{ int.type }}'>
{% if int['portgroup'] is not defined %}
Expand Down

0 comments on commit c018f03

Please sign in to comment.