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
This commit is contained in:
parent
0d4a4a92c7
commit
18dfdd8895
@ -13,75 +13,79 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
- name: Gather facts
|
- name: Gather Facts for vm_hosts
|
||||||
hosts: vm_hosts
|
hosts: vm_hosts
|
||||||
gather_facts: "{{ gather_facts | default(true) }}"
|
gather_facts: yes
|
||||||
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
|
|
||||||
|
|
||||||
|
tasks:
|
||||||
- name: Get info about the virt storage pools
|
- name: Get info about the virt storage pools
|
||||||
virt_pool:
|
virt_pool:
|
||||||
command: info
|
command: info
|
||||||
register: _virt_pools
|
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
|
- name: Stop running VMs
|
||||||
virt:
|
virt:
|
||||||
name: "{{ hostvars[item]['server_hostname'] }}"
|
name: "{{ server_hostname }}"
|
||||||
command: destroy
|
command: destroy
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Delete VM LV
|
- name: Delete VM LV
|
||||||
lvol:
|
lvol:
|
||||||
vg: "{{ default_vm_disk_vg }}"
|
vg: "{{ default_vm_disk_vg }}"
|
||||||
lv: "{{ hostvars[item]['server_hostname'] }}"
|
lv: "{{ server_hostname }}"
|
||||||
state: absent
|
state: absent
|
||||||
force: yes
|
force: yes
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Delete VM Disk Image
|
- name: Delete VM Disk Image
|
||||||
file:
|
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
|
state: absent
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Undefine the VM
|
- name: Undefine the VM
|
||||||
virt:
|
virt:
|
||||||
name: "{{ hostvars[item]['server_hostname'] }}"
|
name: "{{ server_hostname }}"
|
||||||
command: undefine
|
command: undefine
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Create VM LV
|
- name: Create VM LV
|
||||||
lvol:
|
lvol:
|
||||||
vg: "{{ default_vm_disk_vg }}"
|
vg: "{{ default_vm_disk_vg }}"
|
||||||
lv: "{{ hostvars[item]['server_hostname'] }}"
|
lv: "{{ server_hostname }}"
|
||||||
size: "{{ default_vm_storage }}"
|
size: "{{ default_vm_storage }}"
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
- default_vm_disk_mode == "lvm"
|
- 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
|
- name: Setup/clean-up file-based disk images
|
||||||
when:
|
when:
|
||||||
@ -89,9 +93,11 @@
|
|||||||
block:
|
block:
|
||||||
- name: Find existing base image files
|
- name: Find existing base image files
|
||||||
find:
|
find:
|
||||||
paths: "{{ _virt_pools.pools.default.path | default('/data/images') }}"
|
paths: "{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}"
|
||||||
patterns: '*-base.img'
|
patterns: '*-base.img'
|
||||||
register: _base_images
|
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
|
- name: Enable/disable vm_use_snapshot based on whether there are base image files
|
||||||
set_fact:
|
set_fact:
|
||||||
@ -104,19 +110,22 @@
|
|||||||
with_items: "{{ _base_images.files }}"
|
with_items: "{{ _base_images.files }}"
|
||||||
when:
|
when:
|
||||||
- not (vm_use_snapshot | bool)
|
- not (vm_use_snapshot | bool)
|
||||||
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Create VM Disk Image
|
- name: Create VM Disk Image
|
||||||
command: >-
|
command: >-
|
||||||
qemu-img create
|
qemu-img create
|
||||||
-f qcow2
|
-f qcow2
|
||||||
{% if vm_use_snapshot | bool %}
|
{% 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 %}
|
{% 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
|
{{ default_vm_storage }}m
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
# Note (odyssey4me):
|
# Note (odyssey4me):
|
||||||
# This will only work on a host which has
|
# This will only work on a host which has
|
||||||
@ -133,7 +142,7 @@
|
|||||||
virt-sysprep
|
virt-sysprep
|
||||||
--enable customize
|
--enable customize
|
||||||
--ssh-inject root:file:/root/.ssh/id_rsa.pub
|
--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:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- hostvars[item]['server_vm'] | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
with_items: "{{ groups['pxe_servers'] }}"
|
||||||
@ -162,37 +171,61 @@
|
|||||||
until: "'<guest>' in virsh_caps.stdout"
|
until: "'<guest>' in virsh_caps.stdout"
|
||||||
retries: 6
|
retries: 6
|
||||||
delay: 10
|
delay: 10
|
||||||
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Define the VM
|
- name: Define the VM
|
||||||
virt:
|
virt:
|
||||||
name: "{{ hostvars[item]['server_hostname'] }}"
|
name: "{{ server_hostname }}"
|
||||||
command: define
|
command: define
|
||||||
xml: >-
|
xml: >-
|
||||||
{%- if vm_use_snapshot | bool %}
|
{%- 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 %}
|
{%- else %}
|
||||||
{{ lookup('template', 'kvm/kvm-vm.xml.j2') }}
|
{{ lookup('template', 'kvm/kvm-vm.xml.j2') }}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Get the VM xml
|
- name: Get the VM xml
|
||||||
virt:
|
virt:
|
||||||
command: get_xml
|
command: get_xml
|
||||||
name: "{{ hostvars[item]['server_hostname'] }}"
|
name: "{{ server_hostname }}"
|
||||||
register: vm_xml
|
register: vm_xml
|
||||||
when:
|
when:
|
||||||
- hostvars[item]['server_vm'] | default(false) | bool
|
- server_vm | default(false) | bool
|
||||||
with_items: "{{ groups['pxe_servers'] }}"
|
delegate_to: "{{ item }}"
|
||||||
|
with_items: "{{ groups['vm_hosts'] }}"
|
||||||
|
|
||||||
- name: Write the VM xml
|
- name: Write the VM xml
|
||||||
copy:
|
copy:
|
||||||
content: "{{ item.get_xml }}"
|
content: "{{ item.1.get_xml }}"
|
||||||
dest: "/etc/libvirt/qemu/{{ item.item }}.xml"
|
dest: "/etc/libvirt/qemu/{{ item.1.item }}.xml"
|
||||||
with_items: "{{ vm_xml.results }}"
|
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
|
- name: Start the VM
|
||||||
virt:
|
virt:
|
||||||
name: "{{ hostvars[item]['server_hostname'] }}"
|
name: "{{ hostvars[item]['server_hostname'] }}"
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
<domain type='kvm'>
|
<domain type='kvm'>
|
||||||
<name>{{ hostvars[item]['server_hostname'] }}</name>
|
<name>{{ server_hostname }}</name>
|
||||||
<memory unit='MiB'>{{ hostvars[item]['server_vm_ram'] }}</memory>
|
<memory unit='MiB'>{{ server_vm_ram }}</memory>
|
||||||
<currentMemory unit='MiB'>{{ hostvars[item]['server_vm_ram'] }}</currentMemory>
|
<currentMemory unit='MiB'>{{ server_vm_ram }}</currentMemory>
|
||||||
{% set _vcpus_threads = ((hostvars[item]['server_vm_vcpus'] | int) // 2) %}
|
{% set _vcpus_threads = ((server_vm_vcpus | int) // 2) %}
|
||||||
{% set vcpus_threads = (_vcpus_threads | int) == 0 | ternary('1', _vcpus_threads) %}
|
{% 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>
|
<vcpu placement='static' current='{{ vcpus | int }}'>{{ vcpus | int }}</vcpu>
|
||||||
<resource>
|
<resource>
|
||||||
<partition>/machine</partition>
|
<partition>/machine</partition>
|
||||||
</resource>
|
</resource>
|
||||||
<os>
|
<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'/>
|
<bootmenu enable='yes'/>
|
||||||
</os>
|
</os>
|
||||||
<features>
|
<features>
|
||||||
@ -18,7 +18,7 @@
|
|||||||
<apic/>
|
<apic/>
|
||||||
</features>
|
</features>
|
||||||
<cpu mode='host-model'>
|
<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>
|
</cpu>
|
||||||
<clock offset='utc'>
|
<clock offset='utc'>
|
||||||
<timer name='rtc' tickpolicy='catchup'/>
|
<timer name='rtc' tickpolicy='catchup'/>
|
||||||
@ -37,11 +37,11 @@
|
|||||||
{% if default_vm_disk_mode == "lvm" %}
|
{% if default_vm_disk_mode == "lvm" %}
|
||||||
<disk type='block' device='disk'>
|
<disk type='block' device='disk'>
|
||||||
<driver name='qemu' type='raw' cache='none' io='native'/>
|
<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" %}
|
{% elif default_vm_disk_mode == "file" %}
|
||||||
<disk type='file' device='disk'>
|
<disk type='file' device='disk'>
|
||||||
<driver name='qemu' type='qcow2' discard='unmap' cache='none' io='native'/>
|
<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 %}
|
{% endif %}
|
||||||
<target dev='sda' bus='scsi'/>
|
<target dev='sda' bus='scsi'/>
|
||||||
<alias name='scsi0-0-0-0'/>
|
<alias name='scsi0-0-0-0'/>
|
||||||
@ -59,10 +59,10 @@
|
|||||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
|
<address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
|
||||||
</controller>
|
</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'>
|
<interface type='network'>
|
||||||
{% if _key == hostvars[item]['server_vm_primary_network'] %}
|
{% if _key == server_vm_primary_network %}
|
||||||
<mac address='{{ hostvars[item]["server_mac_address"] | lower }}'/>
|
<mac address='{{ server_mac_address | lower }}'/>
|
||||||
<boot order='2'/>
|
<boot order='2'/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<source network='{{ _value.vm_int_iface }}'/>
|
<source network='{{ _value.vm_int_iface }}'/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user