-
Notifications
You must be signed in to change notification settings - Fork 20
/
adhoc-deploy-kvm-guest.yml
93 lines (84 loc) · 3.58 KB
/
adhoc-deploy-kvm-guest.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
# 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 }}"