Skip to content

Commit b1d08ae

Browse files
authored
Updated playbooks for OLVM (#44)
1 parent 1381b11 commit b1d08ae

13 files changed

+462
-139
lines changed

playbooks/OLVM/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# OLVM - Ansible Playbooks for Oracle Linux Virtualization
2+
3+
4+
A collection of Ansible playbooks to use with Oracle Linux Virtualization Manager. Playbooks are tested with Ansible CLI commands on Oracle Linux and with Oracle Linux Automation Manager.
5+
6+
The playbooks uses modules from the [`ovirt.ovirt` Ansible collection](https://docs.ansible.com/ansible/latest/collections/ovirt/ovirt/index.html) which should be downloaded before using the playbooks. Read the collection documentation page for additional explanation or for extending the functionality of the playbooks.
7+
8+
## How to use the playbooks
9+
10+
### Ansible CLI
11+
12+
First step is the configuration of the playbook variables which are mostly configured in ``default_vars.yml`` file. Variables may be used in the command line when not configured in the default variables file. Variables are required to configure your infrastructure settings for the OLVM server, VM configuration and cloud-init. See below table for explanation of the variables.
13+
14+
For example, the playbooks can be used like this (adjust variables to your infrastructure):
15+
16+
$ git clone https://github.com/oracle-samples/ansible-playbooks.git ol-playbooks
17+
$ cd ol-playbooks/playbooks/OLVM
18+
$ ansible-galaxy collection install -f ovirt.ovirt
19+
$ ansible-galaxy collection install -f community.general
20+
$ vi default_vars.yml
21+
$ export "OVIRT_URL=https://olvm-engine.demo.local/ovirt-engine/api"
22+
$ export "OVIRT_USERNAME=admin@internal"
23+
$ export "OVIRT_PASSWORD=CHANGE_ME"
24+
25+
# create a single VM
26+
$ ansible-playbook -i olvm-engine.demo.local, -u opc --key-file ~/.ssh/id_rsa \
27+
-e "vm_name=vm01" -e "vm_ip_address=192.168.1.101" \
28+
olvm_create_one_vm.yml
29+
30+
# create multiple VMs with inventory file, see example hosts.ini file
31+
$ ansible-playbook -i inventory/hosts.ini -u opc --key-file ~/.ssh/id_rsa \
32+
olvm_create_multiple_vms.yml
33+
34+
# delete a VM
35+
$ ansible-playbook -i olvm-engine.demo.local, -u opc --key-file ~/.ssh/id_rsa \
36+
-e "vm_name=vm01" olvm_delete_vm.yml
37+
38+
# live migrate a VM
39+
$ ansible-playbook -i olvm-engine.demo.local, -u opc --key-file ~/.ssh/id_rsa \
40+
-e "vm_name=vm01" -e "dst_kvmhost=KVM2" olvm_migrate_vm.yml
41+
42+
Note 1: using the OLVM server FQDN (in this example olvm-engine.demo.local), appended with a comma, is a quick-way to not use a inventory file.
43+
44+
Note 2: as it includes clear-text password, for better security you may want to encrypt the ``default_vars.yml`` file with the `ansible-vault` command. When running the playbook, Ansible asks for a secret to decrypt the YAML file.
45+
46+
$ ansible-vault encrypt default_vars.yml
47+
$ ansible-playbook -i olvm-engine.demo.local, -u opc --key-file ~/.ssh/id_rsa \
48+
-e "vm_name=oltest" -e "vm_ip_address=192.168.1.100" \
49+
--ask-vault-pass olvm_create_single_vm.yml
50+
51+
### Oracle Linux Automation Manager
52+
53+
#### Project:
54+
In Oracle Linux Automation Manager you can directly import the playbook repository from GitHub as project. The top-level directory of the repository contains the requirements file to download the `ovirt.ovirt` ansible collection.
55+
56+
#### Inventory:
57+
Create an inventory and add one host with the details of your OLVM server, this is the target host were you run the playbook. Make sure you have a Machine credential setup for this host so that ansible can SSH to it (run the ping Module for this host). For the VMs you want to create add an inventory group ``[instances]`` and add the VM names including hostvars for ``vm_name`` and ``vm_ip_address``.
58+
59+
#### Credentials:
60+
Besides the standard SSH credential to access the target host, an additional credential is required to use the ovirt modules in the playbooks. It's based on credential type ``Red Hat Virtualization`` and you need to fill in the OLVM FQDN, username, password and CA File. For example:
61+
62+
Host (Authentication URL): https://olvm-engine.demo.local/ovirt-engine/api
63+
Username: admin@internal
64+
Password: CHANGE_ME
65+
66+
#### Templates:
67+
Create a new job template and provide the following information:
68+
69+
Inventory: Select the inventory containing the OLVM host
70+
Project: Select project from the Github repository
71+
Playbook: Select playbook from Project, for example olvm_create_single_vm.yml
72+
Credentials: Select Machine (SSH) credential and the Virtualization credentials
73+
Variables: Enter the variables as used in the example default_vars.yml file
74+
75+
### Secure API connection
76+
77+
By default the API connection to the OLVM server is insecure, if you want to use a secure API connection then you need to define variable ``olvm_insecure`` and make sure the CA file is available (default location is ``/etc/pki/ovirt-engine/ca.pem``). You may use ``olvm_cafile`` to specify alternative location.
78+
79+
olvm_insecure: false
80+
olvm_cafile: /home/opc/ca.pem
81+
82+
The CA file can be downloaded from the main OLVM web portal or directly from the OLVM server, for example:
83+
84+
$ scp [email protected]:/etc/pki/ovirt-engine/ca.pem /home/opc/ca.pem
85+
86+
## Variables used in the playbooks
87+
88+
| Variable | Example value | Description |
89+
| -------- | ------------- | ----------- |
90+
| OVIRT_URL | https://olvm-fqdn/ovirt-engine/api | The API URL of the OLVM server
91+
| OVIRT_USERNAME | admin@internal | The name of the user, same as used for GUI login
92+
| OVIRT_PASSWORD | CHANGE_ME | The password of the user, same as used for GUI login
93+
| olvm_cluster | Default | Name of the cluster, where VM should be created
94+
| olvm_template | OL9U4_x86_64-olvm-b234 |Name of the template, which should be used to create VM
95+
| vm_name | oltest | Name of the VM, will also be used as hostname
96+
| vm_ip_address | 192.168.1.100 | Static IP address of VM, if DHCP is required cloud-init section in playbook should be changed
97+
| vm_ram | 2048MiB | Amount of memory of the VM
98+
| vm_cpu | 4 | Number of virtual CPUs sockets of the VM
99+
| vm_root_passwd | your_secret_root_pw | Root password of the VM, used bu cloud-init
100+
| vm_dns | 192.168.1.3 | DNS server to be used for VM
101+
| vm_dns_domain | demo.local | DNS domainto to be used for VM
102+
| vm_gateway | 192.168.1.1 | Default gateway to be used for VM
103+
| vm_netmask | 255.255.255.0 | Netmask to be used for VM
104+
| vm_timezone | Europe/Amsterdam | Timezone for VM
105+
| vm_user | opc | Standard user for Oracle provided template, otherwise use your own or root user
106+
| vm_user_sshpubkey | "ssh-rsa AAAA...YOUR KEY HERE...hj8= " | SSH Public key for stndard user
107+
| src_vm | oltest | VM used as source VM for cloning operation
108+
| src_vm_snapshot | base_snapshot | Name of snapshot of source VM, for cloning operation
109+
| dst_vm | oltest_cloned | Name of destination VM for cloning operation
110+
| dst_kvmhost | KVM2 | Name (not hostname) of kvm host in OLVM cluster and destination for live-migration
111+
| vm_id | 76c76c8b-a9ad-414e-8274-181a1ba8948b | VM ID for the VM, used for rename of VM
112+
| vm_newname | oltest | New name for VM with vm_id, used for rename of VM
113+
| olvm_insecure | false | By default ``true``, but define ``false`` in case you need secure API connection
114+
| olvm_cafile | /home/opc/ca.pem | Location of CA file in case you wish alternative location
115+
116+
117+
## Deploying Oracle Linux OLVM VM templates
118+
119+
Two playbooks are provided to deploy new virtual machines in Oracle Linux Virtualization Manager based on a pre-configured template. This may be your own template or templates downloaded from Oracle's website which can be [imported directly in Oracle Linux Virtualization Manager](https://docs.oracle.com/en/virtualization/oracle-linux-virtualization-manager/admin/admin-admin-tasks.html#templates-create):
120+
121+
* [Free Oracle Linux templates](https://yum.oracle.com/oracle-linux-templates.html)
122+
* [Single Instance and Oracle Real Application Clusters (RAC) templates](https://www.oracle.com/database/technologies/rac/vm-db-templates.html)
123+
124+
The Oracle provided templates use cloud-init to automate the initial setup of virtual machines and cloud-init variables are included in the playbooks.
125+

playbooks/OLVM/Readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

playbooks/OLVM/default_vars.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file containes password for the root user for the VM. If you
2+
# add the passwords in clear-text, you may encrypt this file using the
3+
# ansible-vault command to protect the passwords (alternative is to
4+
# use -e "vm_root_passwd=XXX" in CLI:
5+
#
6+
# $ ansible-vault encrypt default_vars.yml
7+
#
8+
# For Oracle Linux Automation Manager GUI:
9+
# configure password in Vault and use Vault in template
10+
11+
# Variables used for VM configuration:
12+
13+
olvm_cluster: Default
14+
olvm_template: OL9U4_x86_64-olvm-b234
15+
vm_ram: 1024MiB
16+
vm_cpu: 2
17+
18+
# Variables used for cloud-init:
19+
20+
vm_root_passwd: CHANGE_ME
21+
vm_dns: 192.168.1.3
22+
vm_dns_domain: demo.local
23+
vm_gateway: 192.168.1.1
24+
vm_netmask: 255.255.255.0
25+
vm_timezone: Europe/Amsterdam
26+
vm_user: opc
27+
vm_user_sshpubkey: "ssh-rsa AAAA...<YOUR KEY HERE>...hj8= "
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[olvm]
3+
olvm-engine.demo.local
4+
5+
[instances]
6+
vm01 vm_name=vm01 vm_ip_address=192.168.1.101
7+
vm02 vm_name=vm02 vm_ip_address=192.168.1.102
8+
vm03 vm_name=vm03 vm_ip_address=192.168.1.103

playbooks/OLVM/olvm_clone_vm.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
3+
# Configure default_vars.yml to setup default infrastrcuture variables
4+
#
5+
# Define following variables as extra-vars:
6+
# --extra-vars "src_vm=XXX"
7+
# --extra-vars "src_vm_snapshot=YYY"
8+
# --extra-vars "dst_vm=ZZZ"
9+
10+
- hosts: all
11+
become: yes
12+
become_method: sudo
13+
gather_facts: no
14+
15+
vars_files:
16+
- default_vars.yml
17+
18+
tasks:
19+
20+
- name: Login to OLVM manager
21+
ovirt_auth:
22+
url: "{{ lookup('env', 'OVIRT_URL') }}"
23+
username: "{{ lookup('env', 'OVIRT_USERNAME') }}"
24+
password: "{{ lookup('env', 'OVIRT_PASSWORD') }}"
25+
ca_file: "{{ olvm_cafile | default('/etc/pki/ovirt-engine/ca.pem') }}"
26+
insecure: "{{ olvm_insecure | default(true) }}"
27+
tags:
28+
- always
29+
30+
- name: Clone Virtual Machine from snapshot
31+
ovirt.ovirt.ovirt_vm:
32+
auth: "{{ ovirt_auth }}"
33+
snapshot_vm: "{{ src_vm }}"
34+
snapshot_name: "{{ src_vm_snapshot }}"
35+
name: "{{ dst_vm }}"
36+
state: present
37+
38+
- name: Cleanup OLVM auth token
39+
ovirt.ovirt.ovirt_auth:
40+
ovirt_auth: "{{ ovirt_auth }}"
41+
state: absent
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
- hosts: olvm
3+
become: yes
4+
become_method: sudo
5+
gather_facts: yes
6+
7+
8+
vars_files:
9+
- default_vars.yml
10+
11+
tasks:
12+
13+
- name: Login to OLVM manager
14+
ovirt_auth:
15+
url: "{{ lookup('env', 'OVIRT_URL') }}"
16+
username: "{{ lookup('env', 'OVIRT_USERNAME') }}"
17+
password: "{{ lookup('env', 'OVIRT_PASSWORD') }}"
18+
ca_file: "{{ olvm_cafile | default('/etc/pki/ovirt-engine/ca.pem') }}"
19+
insecure: "{{ olvm_insecure | default(true) }}"
20+
tags:
21+
- always
22+
23+
- name: Create Virtual Machine
24+
ovirt.ovirt.ovirt_vm:
25+
auth: "{{ ovirt_auth }}"
26+
name: "{{ item }}"
27+
template: "{{ olvm_template }}"
28+
cluster: "{{ olvm_cluster | default('Default') }}"
29+
memory: "{{ hostvars[item]['vm_ram'] | default('1024MiB') }}"
30+
cpu_sockets: "{{ hostvars[item]['vm_cpu'] | default('1') }}"
31+
high_availability: true
32+
state: running
33+
wait: yes
34+
cloud_init:
35+
host_name: "{{ hostvars[item]['vm_name'] + '.' + vm_dns_domain }}"
36+
root_password: "{{ vm_root_passwd }}"
37+
user_name: "{{ vm_user }}"
38+
authorized_ssh_keys: "{{ vm_user_sshpubkey }}"
39+
dns_servers: "{{ vm_dns }}"
40+
dns_search: "{{ vm_dns_domain }}"
41+
nic_name: "{{ vm_nicname | default('eth0') }}"
42+
nic_boot_protocol: static
43+
nic_ip_address: "{{ hostvars[item]['vm_ip_address'] }}"
44+
nic_gateway: "{{ vm_gateway }}"
45+
nic_netmask: "{{ vm_netmask }}"
46+
timezone: "{{ vm_timezone }}"
47+
custom_script: |
48+
runcmd:
49+
- hostnamectl set-hostname {{ hostvars[item]['vm_name'] + '.' + vm_dns_domain }}
50+
- yum -y remove cloud-init
51+
wait: true
52+
loop: "{{ groups['instances'] }}"
53+
54+
- name: Cleanup OLVM auth token
55+
ovirt.ovirt.ovirt_auth:
56+
ovirt_auth: "{{ ovirt_auth }}"
57+
state: absent

playbooks/OLVM/olvm_create_one_vm.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
3+
# Configure default_vars.yml to setup default infrastrcuture variables
4+
#
5+
# Define following variables as extra-vars:
6+
# --extra-vars "vm_name=oltest"
7+
# --extra-vars "vm_ip_address=192.168.1.100"
8+
9+
- hosts: olvm
10+
become: yes
11+
become_method: sudo
12+
gather_facts: yes
13+
14+
vars_files:
15+
- default_vars.yml
16+
17+
tasks:
18+
19+
- name: Login to OLVM manager
20+
ovirt_auth:
21+
url: "{{ lookup('env', 'OVIRT_URL') }}"
22+
username: "{{ lookup('env', 'OVIRT_USERNAME') }}"
23+
password: "{{ lookup('env', 'OVIRT_PASSWORD') }}"
24+
ca_file: "{{ olvm_cafile | default('/etc/pki/ovirt-engine/ca.pem') }}"
25+
insecure: "{{ olvm_insecure | default(true) }}"
26+
tags:
27+
- always
28+
29+
- name: Create and run VM from template
30+
ovirt_vm:
31+
auth: "{{ ovirt_auth }}"
32+
name: "{{ vm_name }}"
33+
template: "{{ olvm_template }}"
34+
cluster: "{{ olvm_cluster }}"
35+
memory: "{{ vm_ram }}"
36+
cpu_sockets: "{{ vm_cpu }}"
37+
high_availability: true
38+
state: running
39+
wait: yes
40+
cloud_init:
41+
host_name: "{{ vm_name + '.' + vm_dns_domain }}"
42+
root_password: "{{ vm_root_passwd }}"
43+
user_name: "{{ vm_user }}"
44+
authorized_ssh_keys: "{{ vm_user_sshpubkey }}"
45+
dns_servers: "{{ vm_dns }}"
46+
dns_search: "{{ vm_dns_domain }}"
47+
nic_name: "{{ vm_nicname | default('eth0') }}"
48+
nic_boot_protocol: static
49+
nic_ip_address: "{{ vm_ip_address }}"
50+
nic_gateway: "{{ vm_gateway }}"
51+
nic_netmask: "{{ vm_netmask }}"
52+
timezone: "{{ vm_timezone }}"
53+
custom_script: |
54+
runcmd:
55+
- hostnamectl set-hostname {{ vm_name + '.' + vm_dns_domain }}
56+
- yum -y remove cloud-init
57+
58+
- name: Cleanup OLVM auth token
59+
ovirt_auth:
60+
ovirt_auth: "{{ ovirt_auth }}"
61+
state: absent

playbooks/OLVM/olvm_delete_vm.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
3+
# Configure default_vars.yml to setup default infrastrcuture variables
4+
#
5+
# Define following variables as extra-vars:
6+
# --extra-vars "vm_name=oltest"
7+
8+
- hosts: olvm
9+
become: yes
10+
11+
vars_files:
12+
- default_vars.yml
13+
14+
tasks:
15+
16+
- name: Login to OLVM manager
17+
ovirt_auth:
18+
url: "{{ lookup('env', 'OVIRT_URL') }}"
19+
username: "{{ lookup('env', 'OVIRT_USERNAME') }}"
20+
password: "{{ lookup('env', 'OVIRT_PASSWORD') }}"
21+
ca_file: "{{ olvm_cafile | default('/etc/pki/ovirt-engine/ca.pem') }}"
22+
insecure: "{{ olvm_insecure | default(true) }}"
23+
tags:
24+
- always
25+
26+
- name: Delete the VM {{ vm_name }}
27+
ovirt_vm:
28+
auth: "{{ ovirt_auth }}"
29+
state: absent
30+
name: "{{ vm_name }}"
31+
32+
- name: Cleanup OLVM auth token
33+
ovirt_auth:
34+
ovirt_auth: "{{ ovirt_auth }}"
35+
state: absent

playbooks/OLVM/olvm_list_vminfo.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
3+
# Configure default_vars.yml to setup default infrastrcuture variables
4+
#
5+
# Define following variables as extra-vars, for example:
6+
# --extra-vars "vm_name=oltest"
7+
8+
- hosts: olvm
9+
become: yes
10+
11+
vars_files:
12+
- default_vars.yml
13+
14+
tasks:
15+
- name: List OLVM VM's {{ vm_name }} by Cluster {{ olvm_cluster }}
16+
ovirt_vm_info:
17+
auth:
18+
url: "{{ lookup('env', 'OVIRT_URL') }}"
19+
username: "{{ lookup('env', 'OVIRT_USERNAME') }}"
20+
password: "{{ lookup('env', 'OVIRT_PASSWORD') }}"
21+
ca_file: "{{ olvm_cafile | default(omit) }}"
22+
insecure: "{{ olvm_insecure | default(true) }}"
23+
pattern: name="{{ vm_name }}" and cluster="{{ olvm_cluster }}"
24+
register: result
25+
26+
- name: Print out {{ vm_name }} VM information
27+
debug:
28+
msg: "{{ result.ovirt_vms }}"

0 commit comments

Comments
 (0)