From 18dfdd889524572ca4e1321c115b0b3f25fbddb1 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Fri, 14 Sep 2018 16:08:39 -0500 Subject: [PATCH] Change VM definition in deploy-vm mnaio playbook ... to allow usage of host vars and group vars by looping the part of the play that reifies the VM templates over pxe_servers rather than vm_hosts. Instead of looping over pxe_servers hosts on each task, loop over and delegate to vm_hosts on each task. What this makes possible is the definition of default_vm_storage on a per-host basis. The specific use case I have in mind is to allocate a larger share of VG space to compute nodes without artificially limiting how much space the compute nodes can have by bloating every other VM in proportion. Change-Id: I5f9a14038d59af7740acb64cce7f83fd88e5555a --- multi-node-aio/playbooks/deploy-vms.yml | 127 +++++++++++++-------- multi-node-aio/playbooks/kvm/kvm-vm.xml.j2 | 24 ++-- 2 files changed, 92 insertions(+), 59 deletions(-) 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: "'' 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 @@ - {{ hostvars[item]['server_hostname'] }} - {{ hostvars[item]['server_vm_ram'] }} - {{ hostvars[item]['server_vm_ram'] }} -{% set _vcpus_threads = ((hostvars[item]['server_vm_vcpus'] | int) // 2) %} + {{ server_hostname }} + {{ server_vm_ram }} + {{ server_vm_ram }} +{% 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) %} {{ vcpus | int }} /machine - hvm + hvm @@ -18,7 +18,7 @@ - + @@ -37,11 +37,11 @@ {% if default_vm_disk_mode == "lvm" %} - + {% elif default_vm_disk_mode == "file" %} - + {% endif %} @@ -59,10 +59,10 @@
-{% for _key, _value in hostvars[item]['server_networks'].items()|sort(attribute='1.iface') %} +{% for _key, _value in server_networks.items()|sort(attribute='1.iface') %} -{% if _key == hostvars[item]['server_vm_primary_network'] %} - +{% if _key == server_vm_primary_network %} + {% endif %}