diff --git a/multi-node-aio/playbooks/deploy-vms.yml b/multi-node-aio/playbooks/deploy-vms.yml index 6b1a8092..bcc882cd 100644 --- a/multi-node-aio/playbooks/deploy-vms.yml +++ b/multi-node-aio/playbooks/deploy-vms.yml @@ -13,75 +13,79 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Gather facts +- name: Gather Facts for vm_hosts hosts: vm_hosts - gather_facts: "{{ gather_facts | default(true) }}" - environment: "{{ deployment_environment_variables | default({}) }}" - tags: - - deploy-vms - tasks: - - name: Gather variables for each operating system - include_vars: "{{ item }}" - with_first_found: - - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" - - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" - - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}.yml" - - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}.yml" - tags: - - always + gather_facts: yes + tasks: - name: Get info about the virt storage pools virt_pool: command: info register: _virt_pools + - name: Set virt_pools host fact + set_fact: + virt_pools: "{{ _virt_pools }}" + +- name: Prepare & Create VMs + hosts: pxe_servers + gather_facts: no + environment: "{{ deployment_environment_variables | default({}) }}" + tags: + - deploy-vms + tasks: + - name: Stop running VMs virt: - name: "{{ hostvars[item]['server_hostname'] }}" + name: "{{ server_hostname }}" command: destroy failed_when: false when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Delete VM LV lvol: vg: "{{ default_vm_disk_vg }}" - lv: "{{ hostvars[item]['server_hostname'] }}" + lv: "{{ server_hostname }}" state: absent force: yes failed_when: false when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Delete VM Disk Image file: - path: "{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img" + path: "{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}.img" state: absent when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Undefine the VM virt: - name: "{{ hostvars[item]['server_hostname'] }}" + name: "{{ server_hostname }}" command: undefine failed_when: false when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Create VM LV lvol: vg: "{{ default_vm_disk_vg }}" - lv: "{{ hostvars[item]['server_hostname'] }}" + lv: "{{ server_hostname }}" size: "{{ default_vm_storage }}" when: - - hostvars[item]['server_vm'] | default(false) | bool + - server_vm | default(false) | bool - default_vm_disk_mode == "lvm" - with_items: "{{ groups['pxe_servers'] }}" + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Setup/clean-up file-based disk images when: @@ -89,9 +93,11 @@ block: - name: Find existing base image files find: - paths: "{{ _virt_pools.pools.default.path | default('/data/images') }}" + paths: "{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}" patterns: '*-base.img' register: _base_images + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Enable/disable vm_use_snapshot based on whether there are base image files set_fact: @@ -104,19 +110,22 @@ with_items: "{{ _base_images.files }}" when: - not (vm_use_snapshot | bool) + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Create VM Disk Image command: >- qemu-img create -f qcow2 {% if vm_use_snapshot | bool %} - -b {{ _virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}-base.img + -b {{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}-base.img {% endif %} - {{ _virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img + {{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}.img {{ default_vm_storage }}m when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" # Note (odyssey4me): # This will only work on a host which has @@ -133,7 +142,7 @@ virt-sysprep --enable customize --ssh-inject root:file:/root/.ssh/id_rsa.pub - --add {{ _virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img + --add {{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img when: - hostvars[item]['server_vm'] | default(false) | bool with_items: "{{ groups['pxe_servers'] }}" @@ -162,37 +171,61 @@ until: "'<guest>' in virsh_caps.stdout" retries: 6 delay: 10 + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Define the VM virt: - name: "{{ hostvars[item]['server_hostname'] }}" + name: "{{ server_hostname }}" command: define xml: >- {%- if vm_use_snapshot | bool %} - {{ lookup('file', _virt_pools.pools.default.path | default('/data/images') ~ '/' ~ hostvars[item]['server_hostname'] ~ '.xml') }} + {{ lookup('file', hostvars[item]['virt_pools'].pools.default.path | default('/data/images') ~ '/' ~ hostvars[item]['server_hostname'] ~ '.xml') }} {%- else %} {{ lookup('template', 'kvm/kvm-vm.xml.j2') }} {%- endif %} failed_when: false when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Get the VM xml virt: command: get_xml - name: "{{ hostvars[item]['server_hostname'] }}" + name: "{{ server_hostname }}" register: vm_xml when: - - hostvars[item]['server_vm'] | default(false) | bool - with_items: "{{ groups['pxe_servers'] }}" + - server_vm | default(false) | bool + delegate_to: "{{ item }}" + with_items: "{{ groups['vm_hosts'] }}" - name: Write the VM xml copy: - content: "{{ item.get_xml }}" - dest: "/etc/libvirt/qemu/{{ item.item }}.xml" - with_items: "{{ vm_xml.results }}" + content: "{{ item.1.get_xml }}" + dest: "/etc/libvirt/qemu/{{ item.1.item }}.xml" + delegate_to: "{{ item.0 }}" + with_nested: + - "{{ groups['vm_hosts'] }}" + - "{{ vm_xml.results }}" +- name: Start VMs + hosts: vm_hosts + gather_facts: "{{ gather_facts | default(true) }}" + environment: "{{ deployment_environment_variables | default({}) }}" + tags: + - deploy-vms + tasks: + - name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}.yml" + tags: + - always - name: Start the VM virt: name: "{{ hostvars[item]['server_hostname'] }}" diff --git a/multi-node-aio/playbooks/kvm/kvm-vm.xml.j2 b/multi-node-aio/playbooks/kvm/kvm-vm.xml.j2 index 722ffeba..ea41efbf 100644 --- a/multi-node-aio/playbooks/kvm/kvm-vm.xml.j2 +++ b/multi-node-aio/playbooks/kvm/kvm-vm.xml.j2 @@ -1,16 +1,16 @@ <domain type='kvm'> - <name>{{ hostvars[item]['server_hostname'] }}</name> - <memory unit='MiB'>{{ hostvars[item]['server_vm_ram'] }}</memory> - <currentMemory unit='MiB'>{{ hostvars[item]['server_vm_ram'] }}</currentMemory> -{% set _vcpus_threads = ((hostvars[item]['server_vm_vcpus'] | int) // 2) %} + <name>{{ server_hostname }}</name> + <memory unit='MiB'>{{ server_vm_ram }}</memory> + <currentMemory unit='MiB'>{{ server_vm_ram }}</currentMemory> +{% set _vcpus_threads = ((server_vm_vcpus | int) // 2) %} {% set vcpus_threads = (_vcpus_threads | int) == 0 | ternary('1', _vcpus_threads) %} -{% set vcpus = (hostvars[item]['server_vm_vcpus'] | int) * (vcpus_threads | int) %} +{% set vcpus = (server_vm_vcpus | int) * (vcpus_threads | int) %} <vcpu placement='static' current='{{ vcpus | int }}'>{{ vcpus | int }}</vcpu> <resource> <partition>/machine</partition> </resource> <os> - <type arch='x86_64' machine='pc-i440fx-{{ ansible_distribution_release | lower }}'>hvm</type> + <type arch='x86_64' machine='pc-i440fx-{{ hostvars[item]["ansible_distribution_release"] | lower }}'>hvm</type> <bootmenu enable='yes'/> </os> <features> @@ -18,7 +18,7 @@ <apic/> </features> <cpu mode='host-model'> - <topology sockets='1' cores='{{ hostvars[item]["server_vm_vcpus"] | int }}' threads='{{ vcpus_threads | int }}'/> + <topology sockets='1' cores='{{ server_vm_vcpus | int }}' threads='{{ vcpus_threads | int }}'/> </cpu> <clock offset='utc'> <timer name='rtc' tickpolicy='catchup'/> @@ -37,11 +37,11 @@ {% if default_vm_disk_mode == "lvm" %} <disk type='block' device='disk'> <driver name='qemu' type='raw' cache='none' io='native'/> - <source dev='/dev/{{ default_vm_disk_vg }}/{{ hostvars[item]["server_hostname"] }}'/> + <source dev='/dev/{{ default_vm_disk_vg }}/{{ server_hostname }}'/> {% elif default_vm_disk_mode == "file" %} <disk type='file' device='disk'> <driver name='qemu' type='qcow2' discard='unmap' cache='none' io='native'/> - <source file='{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]["server_hostname"] }}.img'/> + <source file='{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ server_hostname }}.img'/> {% endif %} <target dev='sda' bus='scsi'/> <alias name='scsi0-0-0-0'/> @@ -59,10 +59,10 @@ <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/> </controller> -{% for _key, _value in hostvars[item]['server_networks'].items()|sort(attribute='1.iface') %} +{% for _key, _value in server_networks.items()|sort(attribute='1.iface') %} <interface type='network'> -{% if _key == hostvars[item]['server_vm_primary_network'] %} - <mac address='{{ hostvars[item]["server_mac_address"] | lower }}'/> +{% if _key == server_vm_primary_network %} + <mac address='{{ server_mac_address | lower }}'/> <boot order='2'/> {% endif %} <source network='{{ _value.vm_int_iface }}'/>