Skip to content

Commit

Permalink
refactoring to support issue #317
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrique Heron committed Jan 21, 2021
1 parent 976392e commit 71f7cf1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 25 deletions.
6 changes: 6 additions & 0 deletions tasks/build_vm_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- name: BUILD_VM_CONFIG check for presense of libguestfs appliance
stat:
path: /export/libguestfs/appliance/kernel
get_checksum: no
get_attributes: no
register: libguestfs_app

- name: BUILD_VM_CONFIG ensure the direct /export/libguestfs exist
Expand Down Expand Up @@ -61,6 +63,8 @@
- name: BUILD_VM_CONFIG Check if qcow OS image template exist
stat:
path: "{{ os_qcow_template }}"
get_checksum: no
get_attributes: no
register: cloud_init_image_exist
tags: build_vm

Expand Down Expand Up @@ -106,6 +110,8 @@
- name: BUILD_VM_CONFIG check for existing cloud init iso
stat:
path: "{{ cloud_init_iso_image }}"
get_checksum: no
get_attributes: no
register: cloud_init_iso
tags: build_vm

Expand Down
112 changes: 96 additions & 16 deletions tasks/deploy_vm.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
---
- name: Get OS disk size digits
vars:
match_regex: '^[0-9]*'
set_fact:
os_vm_disk_size: "{{ vm_root_disk_size | regex_search(match_regex) }}"

- name: DEPLOY_VM get {{ os_qcow_template }} size
shell: >
qemu-img info "{{ os_qcow_template }}" | awk -F'[^0-9]*' '/virtual size/ {print $3}'
Expand All @@ -7,45 +13,93 @@

- name: DEPLOY_VM convert disk size to bytes
vars:
new_disk_size: "{{ vm_root_disk_size }} GB"
new_disk_size: "{{ os_vm_disk_size }} GB"
disk_size_in_bytes: "{{ new_disk_size|human_to_bytes }}"
set_fact:
qcow_source_disk_size: "{{ qcow_source_size.stdout }}"
wanted_size: "{{ disk_size_in_bytes }}"
os_disk_wanted_size: "{{ os_vm_disk_size }}G"
expand_os_disk: "{{ true if disk_size_in_bytes|int > qcow_source_size.stdout|int else false }}"

- name: DEPLOY_VM check if {{ vm_name }} disk {{ os_disk }} exist
stat:
path: "{{ os_disk }}"
get_checksum: no
get_attributes: no
register: os_disk_created

- name: DEPLOY_VM {{ vm_name }} disk image
- name: DEPLOY_VM - Ensure {{ os_disk }} disk size is {{ os_vm_disk_size }}G
when: vm_name not in all_instances.list_vms and expand_os_disk|bool
block:
- name: DEPLOY_VM - Get existing VM disk size
when: os_disk_created.stat.exists
command: "qemu-img info {{ os_disk }}"
register: _os_sda_disk_size
changed_when: False
become: yes

- name: DEPLOY_VM - Get the size of disk {{ os_disk }}
when: os_disk_created.stat.exists
vars:
match_regex: "[0-9]*G"
set_fact:
existing_os_vm_disk_size: "{{ _os_sda_disk_size.stdout | regex_search(match_regex) }}"

- name: DEPLOY_VM - Disk {{ os_disk }} size is {{ existing_os_vm_disk_size }} - REMOVING
file:
path: "{{ os_disk }}"
state: absent
when: existing_os_vm_disk_size is defined and existing_os_vm_disk_size != os_disk_wanted_size

- name: DEPLOY_VM check if {{ vm_name }} disk {{ os_disk }} exist
stat:
path: "{{ os_disk }}"
get_checksum: no
get_attributes: no
register: os_disk_created

- name: DEPLOY_VM Create operating system disk for {{ vm_name }}
when: not os_disk_created.stat.exists
command: "qemu-img create -f qcow2 -o preallocation={{ qemu_img_allocation }} {{ os_disk }} {{ vm_root_disk_size }}"
command: "qemu-img create -f qcow2 -o preallocation={{ qemu_img_allocation }} {{ os_disk }} {{ os_vm_disk_size }}G"
args:
creates: "{{ os_disk }}"
register: create_os_disk

- name: DEPLOY_VM grow {{ vm_name }} operating system
- name: DEPLOY_VM Get {{ os_disk }} filesystem size
shell: >
virt-filesystems -a {{ os_disk }} -h --filesystems -l| awk 'NR>1 {print $5}'
changed_when: False
register: filesystem_size
become: yes

- name: DEPLOY_VM Declare {{ os_disk }} filesystem size
set_fact:
grow_filesystem_size: "{{ 'yes' if os_disk_wanted_size != filesystem_size.stdout else 'no' }}"

- name: DEPLOY_VM grow {{ vm_name }} disk {{ os_disk }} based on {{ os_qcow_template }}
when: grow_filesystem_size|bool
command: >
sudo virt-resize -v -x --expand /dev/sda1 "{{ os_qcow_template }}" "{{ os_disk }}"
virt-resize -v -x --expand /dev/sda1 "{{ os_qcow_template }}" "{{ os_disk }}"
register: resize_create_os_disk
changed_when: '"Resize operation completed with no errors" in resize_create_os_disk.stdout'
become: yes

- name: DEPLOY_VM Grow rhel root filesystem
command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'xfs_growfs /'
register: grow_create_os_disk
changed_when: '"Finishing off" in grow_create_os_disk.stdout'
when: "'rhel' in os_release"

- name: DEPLOY_VM Grow fedora root filesystem
command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'resize2fs /dev/sda1'
register: grow_create_os_disk
changed_when: '"Finishing off" in grow_create_os_disk.stdout'
when: "'fedora' in os_release"
## TODO: Remove becasuse the virt-resize task does this
# - name: DEPLOY_VM Grow rhel root filesystem
# command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'xfs_growfs /'
# register: grow_create_os_disk
# changed_when: '"Finishing off" in grow_create_os_disk.stdout'
# when: >
# "'rhel' in os_release" and
# grow_filesystem_size|bool
#
# - name: DEPLOY_VM Grow fedora root filesystem
# command: /usr/local/bin/qubi-virt-customize -a {{ os_disk }} --run-command 'resize2fs /dev/sda1'
# register: grow_create_os_disk
# changed_when: '"Finishing off" in grow_create_os_disk.stdout'
# when: >
# "'fedora' in os_release" and
# grow_filesystem_size|bool

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

- name: DEPLOY_VM - Shutdown {{ vm_name }} when extra disk is added
when: attached_disk.changed
shell: >
virsh destroy "{{ vm_name }}"
ignore_errors: yes
register: virsh_shutdown
become: yes

- name: DEPLOY_VM - Ensure {{ vm_name }} is shutdown
when: virsh_shutdown.changed
virt:
command: list_vms
state: shutdown
register: shutdown_vms
until: vm_name in shutdown_vms.list_vms
retries: 120
delay: 30
become: yes

- name: DEPLOY_VM - Start {{ vm_name }}
when: attached_disk.changed
virt:
name: "{{ vm_name }}"
state: running
become: yes

- name: DEPLOY_VM wait for {{ vm_name }} ip address
command: "/usr/local/bin/getvmip -r {{ vm_name }}"
until: dhcp_vm_ip.stdout != ""
Expand Down
5 changes: 2 additions & 3 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@

- name: INCLUDE TASKS from configure_vm.yml to configure the vm
import_tasks: configure_vm.yml

- name: run post configuration
when: not vm_teardown|bool
import_tasks: post_config.yml
become: yes
include_tasks: post_config.yml
26 changes: 21 additions & 5 deletions tasks/post_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
when: update_inventory|bool and vm_ip is defined

- name: POST_CONFIG update {{ vm_name }} /etc/resolv.conf
when: ping_result is not failed and static_net|bool and vm_ip != ''
become: yes
template:
src: resolv.conf.j2
dest: /etc/resolv.conf
delegate_to: "{{ vm_ip }}"
when: >
ping_result is not failed and
static_net|bool and
vm_ip is defined and
vm_ip != '' and
not vm_teardown|bool
- name: POST_CONFIG change root password
become: yes
user:
name: root
update_password: always
Expand All @@ -23,6 +30,7 @@
delegate_to: "{{ vm_ip }}"

- name: POST_CONFIG ensure cloud-init is uninstalled
become: yes
yum:
name: cloud-init
state: absent
Expand All @@ -37,10 +45,18 @@
changed_when: false
become: yes

- name: POST_CONFIG Eject CD-Rom
- name: POST_CONFIG Declare CDROM
set_fact:
cdrom_device: "{{ cdrom_device.stdout.split()[0] }}"
when: >
cdrom_device.stdout.split()[0] is defined and
cdrom_device.stdout.split()[0] != ""
- name: POST_CONFIG Eject CDROM {{ cdrom_device }}
shell: >
virsh change-media {{ vm_name }} {{ cdrom_device.stdout.split()[0] }} --eject
when: cdrom_device.stdout != '' and cdrom_device.stdout.split()[0] != ""
virsh change-media {{ vm_name }} {{ cdrom_device }} --eject
ignore_errors: yes
when: cdrom_device != ""

- name: POST_CONFIG display vm information
debug:
Expand All @@ -49,4 +65,4 @@
- " VM Name: {{ vm_name }} "
- " IP Address: {{ vm_ip }} "
- " Username: {{ admin_user }} "
- "***********************************"
- "***********************************"
4 changes: 3 additions & 1 deletion tasks/teardown-vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@

- name: TEAR-DOWN-VM find all disk for the vm
find:
paths: "{{ kvm_vm_pool_dir}}/"
paths: "{{ kvm_vm_pool_dir }}/"
patterns: "{{ vm_name }}_vd*.*"
register: vm_disks_all

- name: TEAR-DOWN-VM check if OS disk exist
stat:
path: "{{ os_disk }}"
get_checksum: no
get_attributes: no
register: os_disk_exist

- name: TEAR-DOWN-VM Shutdown {{ vm_name }}
Expand Down

0 comments on commit 71f7cf1

Please sign in to comment.