Skip to content

Commit 71f7cf1

Browse files
author
Rodrique Heron
committed
refactoring to support issue #317
1 parent 976392e commit 71f7cf1

File tree

5 files changed

+128
-25
lines changed

5 files changed

+128
-25
lines changed

tasks/build_vm_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- name: BUILD_VM_CONFIG check for presense of libguestfs appliance
66
stat:
77
path: /export/libguestfs/appliance/kernel
8+
get_checksum: no
9+
get_attributes: no
810
register: libguestfs_app
911

1012
- name: BUILD_VM_CONFIG ensure the direct /export/libguestfs exist
@@ -61,6 +63,8 @@
6163
- name: BUILD_VM_CONFIG Check if qcow OS image template exist
6264
stat:
6365
path: "{{ os_qcow_template }}"
66+
get_checksum: no
67+
get_attributes: no
6468
register: cloud_init_image_exist
6569
tags: build_vm
6670

@@ -106,6 +110,8 @@
106110
- name: BUILD_VM_CONFIG check for existing cloud init iso
107111
stat:
108112
path: "{{ cloud_init_iso_image }}"
113+
get_checksum: no
114+
get_attributes: no
109115
register: cloud_init_iso
110116
tags: build_vm
111117

tasks/deploy_vm.yml

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
---
2+
- name: Get OS disk size digits
3+
vars:
4+
match_regex: '^[0-9]*'
5+
set_fact:
6+
os_vm_disk_size: "{{ vm_root_disk_size | regex_search(match_regex) }}"
7+
28
- name: DEPLOY_VM get {{ os_qcow_template }} size
39
shell: >
410
qemu-img info "{{ os_qcow_template }}" | awk -F'[^0-9]*' '/virtual size/ {print $3}'
@@ -7,45 +13,93 @@
713

814
- name: DEPLOY_VM convert disk size to bytes
915
vars:
10-
new_disk_size: "{{ vm_root_disk_size }} GB"
16+
new_disk_size: "{{ os_vm_disk_size }} GB"
1117
disk_size_in_bytes: "{{ new_disk_size|human_to_bytes }}"
1218
set_fact:
1319
qcow_source_disk_size: "{{ qcow_source_size.stdout }}"
1420
wanted_size: "{{ disk_size_in_bytes }}"
21+
os_disk_wanted_size: "{{ os_vm_disk_size }}G"
1522
expand_os_disk: "{{ true if disk_size_in_bytes|int > qcow_source_size.stdout|int else false }}"
1623

1724
- name: DEPLOY_VM check if {{ vm_name }} disk {{ os_disk }} exist
1825
stat:
1926
path: "{{ os_disk }}"
27+
get_checksum: no
28+
get_attributes: no
2029
register: os_disk_created
2130

22-
- name: DEPLOY_VM {{ vm_name }} disk image
31+
- name: DEPLOY_VM - Ensure {{ os_disk }} disk size is {{ os_vm_disk_size }}G
2332
when: vm_name not in all_instances.list_vms and expand_os_disk|bool
2433
block:
34+
- name: DEPLOY_VM - Get existing VM disk size
35+
when: os_disk_created.stat.exists
36+
command: "qemu-img info {{ os_disk }}"
37+
register: _os_sda_disk_size
38+
changed_when: False
39+
become: yes
40+
41+
- name: DEPLOY_VM - Get the size of disk {{ os_disk }}
42+
when: os_disk_created.stat.exists
43+
vars:
44+
match_regex: "[0-9]*G"
45+
set_fact:
46+
existing_os_vm_disk_size: "{{ _os_sda_disk_size.stdout | regex_search(match_regex) }}"
47+
48+
- name: DEPLOY_VM - Disk {{ os_disk }} size is {{ existing_os_vm_disk_size }} - REMOVING
49+
file:
50+
path: "{{ os_disk }}"
51+
state: absent
52+
when: existing_os_vm_disk_size is defined and existing_os_vm_disk_size != os_disk_wanted_size
53+
54+
- name: DEPLOY_VM check if {{ vm_name }} disk {{ os_disk }} exist
55+
stat:
56+
path: "{{ os_disk }}"
57+
get_checksum: no
58+
get_attributes: no
59+
register: os_disk_created
60+
2561
- name: DEPLOY_VM Create operating system disk for {{ vm_name }}
2662
when: not os_disk_created.stat.exists
27-
command: "qemu-img create -f qcow2 -o preallocation={{ qemu_img_allocation }} {{ os_disk }} {{ vm_root_disk_size }}"
63+
command: "qemu-img create -f qcow2 -o preallocation={{ qemu_img_allocation }} {{ os_disk }} {{ os_vm_disk_size }}G"
2864
args:
2965
creates: "{{ os_disk }}"
3066
register: create_os_disk
3167

32-
- name: DEPLOY_VM grow {{ vm_name }} operating system
68+
- name: DEPLOY_VM Get {{ os_disk }} filesystem size
69+
shell: >
70+
virt-filesystems -a {{ os_disk }} -h --filesystems -l| awk 'NR>1 {print $5}'
71+
changed_when: False
72+
register: filesystem_size
73+
become: yes
74+
75+
- name: DEPLOY_VM Declare {{ os_disk }} filesystem size
76+
set_fact:
77+
grow_filesystem_size: "{{ 'yes' if os_disk_wanted_size != filesystem_size.stdout else 'no' }}"
78+
79+
- name: DEPLOY_VM grow {{ vm_name }} disk {{ os_disk }} based on {{ os_qcow_template }}
80+
when: grow_filesystem_size|bool
3381
command: >
34-
sudo virt-resize -v -x --expand /dev/sda1 "{{ os_qcow_template }}" "{{ os_disk }}"
82+
virt-resize -v -x --expand /dev/sda1 "{{ os_qcow_template }}" "{{ os_disk }}"
3583
register: resize_create_os_disk
3684
changed_when: '"Resize operation completed with no errors" in resize_create_os_disk.stdout'
85+
become: yes
3786

38-
- name: DEPLOY_VM Grow rhel root filesystem
39-
command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'xfs_growfs /'
40-
register: grow_create_os_disk
41-
changed_when: '"Finishing off" in grow_create_os_disk.stdout'
42-
when: "'rhel' in os_release"
43-
44-
- name: DEPLOY_VM Grow fedora root filesystem
45-
command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'resize2fs /dev/sda1'
46-
register: grow_create_os_disk
47-
changed_when: '"Finishing off" in grow_create_os_disk.stdout'
48-
when: "'fedora' in os_release"
87+
## TODO: Remove becasuse the virt-resize task does this
88+
# - name: DEPLOY_VM Grow rhel root filesystem
89+
# command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'xfs_growfs /'
90+
# register: grow_create_os_disk
91+
# changed_when: '"Finishing off" in grow_create_os_disk.stdout'
92+
# when: >
93+
# "'rhel' in os_release" and
94+
# grow_filesystem_size|bool
95+
#
96+
# - name: DEPLOY_VM Grow fedora root filesystem
97+
# command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'resize2fs /dev/sda1'
98+
# register: grow_create_os_disk
99+
# changed_when: '"Finishing off" in grow_create_os_disk.stdout'
100+
# when: >
101+
# "'fedora' in os_release" and
102+
# grow_filesystem_size|bool
49103

50104
- name: DEPLOY_VM Copy {{ vm_qcow_image }} to {{ os_disk }}
51105
when: vm_name not in all_instances.list_vms and not expand_os_disk|bool
@@ -95,6 +149,32 @@
95149
when: item.enable
96150
changed_when: '"Disk attached successfully" in attached_disk.stdout'
97151

152+
- name: DEPLOY_VM - Shutdown {{ vm_name }} when extra disk is added
153+
when: attached_disk.changed
154+
shell: >
155+
virsh destroy "{{ vm_name }}"
156+
ignore_errors: yes
157+
register: virsh_shutdown
158+
become: yes
159+
160+
- name: DEPLOY_VM - Ensure {{ vm_name }} is shutdown
161+
when: virsh_shutdown.changed
162+
virt:
163+
command: list_vms
164+
state: shutdown
165+
register: shutdown_vms
166+
until: vm_name in shutdown_vms.list_vms
167+
retries: 120
168+
delay: 30
169+
become: yes
170+
171+
- name: DEPLOY_VM - Start {{ vm_name }}
172+
when: attached_disk.changed
173+
virt:
174+
name: "{{ vm_name }}"
175+
state: running
176+
become: yes
177+
98178
- name: DEPLOY_VM wait for {{ vm_name }} ip address
99179
command: "/usr/local/bin/getvmip -r {{ vm_name }}"
100180
until: dhcp_vm_ip.stdout != ""

tasks/main.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
- name: INCLUDE TASKS from configure_vm.yml to configure the vm
4545
import_tasks: configure_vm.yml
46-
46+
4747
- name: run post configuration
4848
when: not vm_teardown|bool
49-
import_tasks: post_config.yml
50-
become: yes
49+
include_tasks: post_config.yml

tasks/post_config.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
when: update_inventory|bool and vm_ip is defined
99

1010
- name: POST_CONFIG update {{ vm_name }} /etc/resolv.conf
11-
when: ping_result is not failed and static_net|bool and vm_ip != ''
11+
become: yes
1212
template:
1313
src: resolv.conf.j2
1414
dest: /etc/resolv.conf
1515
delegate_to: "{{ vm_ip }}"
16+
when: >
17+
ping_result is not failed and
18+
static_net|bool and
19+
vm_ip is defined and
20+
vm_ip != '' and
21+
not vm_teardown|bool
1622
1723
- name: POST_CONFIG change root password
24+
become: yes
1825
user:
1926
name: root
2027
update_password: always
@@ -23,6 +30,7 @@
2330
delegate_to: "{{ vm_ip }}"
2431

2532
- name: POST_CONFIG ensure cloud-init is uninstalled
33+
become: yes
2634
yum:
2735
name: cloud-init
2836
state: absent
@@ -37,10 +45,18 @@
3745
changed_when: false
3846
become: yes
3947

40-
- name: POST_CONFIG Eject CD-Rom
48+
- name: POST_CONFIG Declare CDROM
49+
set_fact:
50+
cdrom_device: "{{ cdrom_device.stdout.split()[0] }}"
51+
when: >
52+
cdrom_device.stdout.split()[0] is defined and
53+
cdrom_device.stdout.split()[0] != ""
54+
55+
- name: POST_CONFIG Eject CDROM {{ cdrom_device }}
4156
shell: >
42-
virsh change-media {{ vm_name }} {{ cdrom_device.stdout.split()[0] }} --eject
43-
when: cdrom_device.stdout != '' and cdrom_device.stdout.split()[0] != ""
57+
virsh change-media {{ vm_name }} {{ cdrom_device }} --eject
58+
ignore_errors: yes
59+
when: cdrom_device != ""
4460

4561
- name: POST_CONFIG display vm information
4662
debug:
@@ -49,4 +65,4 @@
4965
- " VM Name: {{ vm_name }} "
5066
- " IP Address: {{ vm_ip }} "
5167
- " Username: {{ admin_user }} "
52-
- "***********************************"
68+
- "***********************************"

tasks/teardown-vm.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636

3737
- name: TEAR-DOWN-VM find all disk for the vm
3838
find:
39-
paths: "{{ kvm_vm_pool_dir}}/"
39+
paths: "{{ kvm_vm_pool_dir }}/"
4040
patterns: "{{ vm_name }}_vd*.*"
4141
register: vm_disks_all
4242

4343
- name: TEAR-DOWN-VM check if OS disk exist
4444
stat:
4545
path: "{{ os_disk }}"
46+
get_checksum: no
47+
get_attributes: no
4648
register: os_disk_exist
4749

4850
- name: TEAR-DOWN-VM Shutdown {{ vm_name }}

0 commit comments

Comments
 (0)