From d4fe54d43b23749a930247c46c3177a06c44d0ae Mon Sep 17 00:00:00 2001 From: nicoo Date: Tue, 29 Jan 2019 14:16:32 +0100 Subject: [PATCH] Refactor VM definition playbook and template to a separate role --- ansible/roles/vm/define/defaults/main.yml | 4 ++ ansible/roles/vm/define/tasks/main.yml | 51 +++++++++++++ .../vm/define/templates/libvirt-domain.xml.j2 | 76 ++++++++++++++++++++ ansible/roles/vm/install/tasks/main.yml | 70 ++---------------- .../vm/install/templates/libvirt-domain.xml.j2 | 76 -------------------- ansible/vm-update.yml | 26 +------ 6 files changed, 140 insertions(+), 163 deletions(-) create mode 100644 ansible/roles/vm/define/defaults/main.yml create mode 100644 ansible/roles/vm/define/tasks/main.yml create mode 100644 ansible/roles/vm/define/templates/libvirt-domain.xml.j2 delete mode 100644 ansible/roles/vm/install/templates/libvirt-domain.xml.j2 diff --git a/ansible/roles/vm/define/defaults/main.yml b/ansible/roles/vm/define/defaults/main.yml new file mode 100644 index 0000000..302256a --- /dev/null +++ b/ansible/roles/vm/define/defaults/main.yml @@ -0,0 +1,4 @@ +vm_define_autostart: "{{ not vm_define_installer and hostvars[hostname].install_cooked.autostart | default(False) }}" +vm_define_start: yes + +vm_define_installer: no diff --git a/ansible/roles/vm/define/tasks/main.yml b/ansible/roles/vm/define/tasks/main.yml new file mode 100644 index 0000000..5667658 --- /dev/null +++ b/ansible/roles/vm/define/tasks/main.yml @@ -0,0 +1,51 @@ +--- +- name: check if vm already exists + virt: + name: "{{ hostname }}" + command: info + register: vmhost_info + +- when: hostname in vmhost_info + block: + - name: destroy exisiting vm + virt: + name: "{{ hostname }}" + state: destroyed + + - name: wait for vm to be destroyed + wait_for_virt: + name: "{{ hostname }}" + states: shutdown,crashed + timeout: 5 + + - name: undefining exisiting vm + virt: + name: "{{ hostname }}" + command: undefine + + +- name: redefine vm + virt: + name: "{{ hostname }}" + command: define + xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" + +- when: vm_define_start + block: + - name: start vm + virt: + name: "{{ hostname }}" + state: running + + - name: wait for VM to start + wait_for_virt: + name: "{{ hostname }}" + states: running + timeout: 10 + +- name: mark vm as autostarted + when: vm_define_autostart + virt: + name: "{{ hostname }}" + autostart: yes + command: info ## virt module needs either command or state diff --git a/ansible/roles/vm/define/templates/libvirt-domain.xml.j2 b/ansible/roles/vm/define/templates/libvirt-domain.xml.j2 new file mode 100644 index 0000000..152ef51 --- /dev/null +++ b/ansible/roles/vm/define/templates/libvirt-domain.xml.j2 @@ -0,0 +1,76 @@ + + {{ hostname }} + {{ hostvars[hostname].install_cooked.mem * 1024 }} + {{ hostvars[hostname].install_cooked.mem * 1024 }} + {{ hostvars[hostname].install_cooked.numcpu }} + + hvm +{% if vm_define_installer %} + {{ debian_installer_path }}/{{ install_distro }}-{{ install_codename }}/{{ hostvars[hostname].install_cooked.arch | default('amd64') }}/linux + {{ preseed_tmpdir }}/initrd.preseed.gz + console=ttyS0,115200n8 +{% endif %} + + + + + + + + + destroy +{% if vm_define_installer %} + destroy + destroy +{% else %} + restart + restart +{% endif %} + + /usr/bin/kvm + + + + + /dev/urandom + + +{% if 'virtio' in hostvars[hostname].install_cooked.disks %} +{% for device, lv in hostvars[hostname].install_cooked.disks.virtio.items() %} + + + + + +{% endfor %} +{% endif %} + +{% if 'scsi' in hostvars[hostname].install_cooked.disks %} + +{% for device, lv in hostvars[hostname].install_cooked.disks.scsi.items() %} + + + + + +{% endfor %} +{% endif %} + +{% if hostvars[hostname].install_cooked.interfaces %} +{% for if in hostvars[hostname].install_cooked.interfaces %} + + + +
+ +{% endfor %} +{% endif %} + + + + + + + + + diff --git a/ansible/roles/vm/install/tasks/main.yml b/ansible/roles/vm/install/tasks/main.yml index 5043fcc..e669fa3 100644 --- a/ansible/roles/vm/install/tasks/main.yml +++ b/ansible/roles/vm/install/tasks/main.yml @@ -6,30 +6,6 @@ lv: "{{ item.value.lv }}" size: "{{ item.value.size }}" -- name: check if vm already exists - virt: - name: "{{ hostname }}" - command: info - register: vmhost_info - -- block: - - name: destroy exisiting vm - virt: - name: "{{ hostname }}" - state: destroyed - - - name: wait for vm to be destroyed - wait_for_virt: - name: "{{ hostname }}" - states: shutdown,crashed - timeout: 5 - - - name: undefining exisiting vm - virt: - name: "{{ hostname }}" - command: undefine - - when: hostname in vmhost_info - block: - name: create a temporary workdir @@ -52,26 +28,12 @@ etype: user permissions: rx - - name: define new installer vm - virt: - name: "{{ hostname }}" - command: define - xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" + - import_role: + name: vm/define vars: - run_installer: yes + vm_define_installer: yes preseed_tmpdir: "{{ tmpdir.path }}" - - name: start vm - virt: - name: "{{ hostname }}" - state: running - - - name: wait for installer to start - wait_for_virt: - name: "{{ hostname }}" - states: running - timeout: 10 - - debug: msg: "you can check on the status of the installer running this command 'virsh console {{ hostname }}' on host {{ inventory_hostname }}." @@ -83,33 +45,13 @@ register: installer_result failed_when: installer_result.failed or installer_result.state == "crashed" - - name: undefining installer vm - virt: - name: "{{ hostname }}" - command: undefine - always: - name: cleanup temporary workdir file: path: "{{ tmpdir.path }}" state: absent -- name: define new production vm - virt: - name: "{{ hostname }}" - command: define - xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" +- import_role: + name: vm/define vars: - run_installer: no - -- name: start vm - virt: - name: "{{ hostname }}" - state: running - -- name: mark vm as autostarted - virt: - name: "{{ hostname }}" - autostart: "{{ hostvars[hostname].install_cooked.autostart }}" - command: info ## virt module needs either command or state - when: hostvars[hostname].install_cooked.autostart is defined + vm_define_installer: no diff --git a/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 b/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 deleted file mode 100644 index ff2ee60..0000000 --- a/ansible/roles/vm/install/templates/libvirt-domain.xml.j2 +++ /dev/null @@ -1,76 +0,0 @@ - - {{ hostname }} - {{ hostvars[hostname].install_cooked.mem * 1024 }} - {{ hostvars[hostname].install_cooked.mem * 1024 }} - {{ hostvars[hostname].install_cooked.numcpu }} - - hvm -{% if run_installer | default(False) %} - {{ debian_installer_path }}/{{ install_distro }}-{{ install_codename }}/{{ hostvars[hostname].install_cooked.arch | default('amd64') }}/linux - {{ preseed_tmpdir }}/initrd.preseed.gz - console=ttyS0,115200n8 -{% endif %} - - - - - - - - - destroy -{% if run_installer | default(False) %} - destroy - destroy -{% else %} - restart - restart -{% endif %} - - /usr/bin/kvm - - - - - /dev/urandom - - -{% if 'virtio' in hostvars[hostname].install_cooked.disks %} -{% for device, lv in hostvars[hostname].install_cooked.disks.virtio.items() %} - - - - - -{% endfor %} -{% endif %} - -{% if 'scsi' in hostvars[hostname].install_cooked.disks %} - -{% for device, lv in hostvars[hostname].install_cooked.disks.scsi.items() %} - - - - - -{% endfor %} -{% endif %} - -{% if hostvars[hostname].install_cooked.interfaces %} -{% for if in hostvars[hostname].install_cooked.interfaces %} - - - -
- -{% endfor %} -{% endif %} - - - - - - - - - diff --git a/ansible/vm-update.yml b/ansible/vm-update.yml index 871b3c7..6c5eab0 100644 --- a/ansible/vm-update.yml +++ b/ansible/vm-update.yml @@ -31,35 +31,15 @@ - name: Update VM definition hosts: _vmhost_ - tasks: + pre_tasks: - name: Wait for VM to shut down wait_for_virt: name: "{{ hostname }}" states: shutdown timeout: 30 - - - name: Undefine VM - virt: - name: "{{ hostname }}" - command: undefine - - - name: Redefine VM - virt: - name: "{{ hostname }}" - command: define - xml: "{{ lookup('template', 'roles/vm/install/templates/libvirt-domain.xml.j2') }}" - - name: Restart VM - virt: - name: "{{ hostname }}" - state: running - - - name: mark vm as autostarted - when: hostvars[hostname].install_cooked.autostart is defined - virt: - name: "{{ hostname }}" - autostart: "{{ hostvars[hostname].install_cooked.autostart }}" - command: info ## virt module needs either command or state + roles: + - vm/define - name: Apply the base vm/guest role hosts: "{{ hostname }}" -- 1.7.10.4