Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
---
# This Ad Hoc task will deploy a KVM virtual machine on existing KVM host
# If you define a bridge network, it has to exist first of course on the kvm host, defined by the ansible-role-kvm-host role
# Needed variables (per host or group inheritance):
# kvm_host: The kvm host on which to deploy the guest (string)
# kvm_guest_vcpus: number or cores to add to define the vm (string)
# kvm_guest_memory: available RAM for the guest (string, in MB)
# kvm_guest_disk_size: the qcow2 image size for the guest (string, in GB)
# kvm_guest_luks_encrypted: if we want to encrypt disk (boolean : True|False)
# kvm_guest_luks_passphrase: if we enabled it (see above) : the passphrase to crypt the disk
# Account:
# kvm_guest_root_pass: the root account password to define by default
# Network settings:
# kvm_guest_bridge: (boolean, True|False) : using an existing bridge on the kvm host
# kvm_host_bridge: the bridge name to use if above is enabled (string, like br0)
# kvm_guest_default_net: if kvm_guest_bridge is False, which other network we want to use, like 'default' (string)
# kvm_guest_arch: the architecture for the guest (string, like x86_64, aarch64, ppc64le, etc)
# kvm_guest_ip: ip address for the guest (string)
# kvm_guest_netmask: netmask for the guest (string)
# kvm_guest_gateway: gateway ip address for the guest (string)
# kvm_guest_nameserver: resolver ip address to use for the guest (string)
#
# Operating Systems
#
# rhel_version: if defined at host/group level, use RHEL and that version (string) and not centos at all
# centos_version: which centos os version to deploy (string, like 8, 8-stream, etc)
- hosts: "{{ init_kvm_guests }}"
become: true
gather_facts: false
vars_prompt:
- name: "init_kvm_guests"
prompt: "[WARNING] KVM guests to be deployed with CentOS => "
private: no
tasks:
- name: Checking Virtual Guest exists
virt:
name: "{{ inventory_hostname }}"
state: running
ignore_errors: yes
no_log: True
delegate_to: "{{ kvm_host }}"
register: vm_exists
- name: Creating kickstart to provision VM[s]
template:
src: "../templates/kickstarts/kvm-guest-rhel{{ rhel_version }}-ks.j2"
dest: "/var/lib/libvirt/local-kickstarts/{{ inventory_hostname }}.cfg"
delegate_to: "{{ kvm_host }}"
when: rhel_version is defined
tags:
- ks
- name: Creating kickstart to provision VM[s]
template:
src: "../templates/kickstarts/kvm-guest-c{{ kvm_guest_distro_release }}-ks.j2"
dest: "/var/lib/libvirt/local-kickstarts/{{ inventory_hostname }}.cfg"
delegate_to: "{{ kvm_host }}"
when: rhel_version is not defined
tags:
- ks
- name: Creating temporary virt-install command
template:
src: ../templates/ansible-virt-install.j2
dest: "/var/lib/libvirt/virt-install-{{ inventory_hostname }}"
mode: 0750
owner: root
group: root
delegate_to: "{{ kvm_host }}"
tags:
- ks
- name: Provisining the Virtual Guest[s]
command: "/var/lib/libvirt/virt-install-{{ inventory_hostname }}"
when: vm_exists is failed
delegate_to: "{{ kvm_host }}"
- name: Waiting for sshd to be available on the deployed node
wait_for:
port: 22
host: "{{ kvm_guest_ip }}"
timeout: 1200
delegate_to: "{{ kvm_host }}"
when: vm_exists is failed
- name: Ensuring we remove the templatized virt-install script
file:
path: "/var/lib/libvirt/virt-install-{{ inventory_hostname }}"
state: absent
delegate_to: "{{ kvm_host }}"