Recently I started to work with Ansible, as part of the learning curve I wanted to automate Virtual Machine creation on my Oracle Linux OLVM lab cluster. As you may know, Oracle Linux Virtualization Manager is a server virtualization management platform for Oracle Linux KVM servers and based on open-source oVirt.
For this lab configuration there are the following pre-requisites:
- Have a running Oracle Linux OLVM manager server connected to one or more Oracle Linux KVM hosts
- Have a Linux-user configured on your OLVM manager with sudo access.
- Have a Template imported in your OLVM cluster with Cloud-init enabled (see later)
- Replace passwords, hostnames, IP-addresses, example.com to your dns domain, etc, etc in below example files for your own network infrastructure
Installation
At the moment of writing the current release of OLVM is 4.3, and I want to use the Ansible ovirt module for oVirt 4.3 to write my playbooks.
The installation is straight forward, just enable the correct repositories. For ansible you enable the Oracle Linux EPEL and for the Ansible ovirt module you need the SDK which is included in the ovirt 4.3 repository.
| $ sudo yum install oracle-epel-release-el7 |
Install Ansible and test if it works with pinging the localhost.
| $ sudo yum install ansible |
Install the ovirt 4.3 SDK, make sure you disable the ovirt 4.2 repositories.
| $ sudo yum install oracle-ovirt-release-el7 |
Ansible is an agentless system and it follows a push approach. There is no need to install an agent on a target host that you intend to manage with Ansible. The only requirement is SSH access at the target host (in my case the OLVM manager) and you need to setup SSH keys to allow access between Ansible host and target hosts.
| $ ssh-keygen |
It’s easy to test the SSH configuration, just try to logon without a password:
| $ ssh user@olvm.example.com |
OLVM Ansible example configuration files
I wanted to work as much as possible with variables/parameters to be flexible in creation of new virtual machines. I created some example files and you need to adjust the variables in the example files for your server-and network infrastructure.
Ansible hosts inventory setup
Ansible works with a list of hosts or groups of hosts in order to know the nodes to manage. This is called inventory and the default inventory file is /etc/ansible/hosts or you can specify a different inventory file (I use hosts.ini).
Besides adding my OLVM manager host to the inventory, I also wanted to add the virtual machines that I want to create with the Ansible playbook. Create an inventory file hosts.ini like the one below and you OLVM server, how to access the OLVM manager server (user authentication) and the VMs you want to create.
| $ vi hosts.ini |
Use ansible to test the connection with a ping or run an ad-hoc command on the OLVM target server
| $ ansible olvm.example.com -u user -m ping $ ansible olvm.example.com -u user -a “cat /etc/hostname” |
VM and cloud-init setup
There are more variables to define, such as the VM template I want to use and also the information for cloud-init to be used by first start-up of the new virtual machine.
In my OLVM cluster I use a little Oracle Linux template that I have build with the Oracle Linux Image Tools. This is a cool project where you automatically build (SLIM) images to be provisioned in cloud infrastructures or OLVM servers.
The variables for the template are configured in a so called group_vars file, Ansible automaticaly looks for variables on startup in a sub-directory group_vars of current working directory.
| $ mkdir group_vars |
Secure passwords file
Last step in the configuration is the creation of a secure password file. It contains the admin password to access the OLVM manager host and the root password that is used in the created virtual machine. Create a plaintext yaml file with the admin password of your OLVM manager and the VM root password and encrypt the file to secure the password.
| $ vi password.yml |
Use Ansible to create OLVM virtual machines
Last step is to create the Ansible playbook, this will be used with our inventory file and the secured password file to access the OLVM manager server. Just copy the below yaml test to file (I used create-vm.yml). There is no need to change the below create-vm.yml playbook, should run out-of-the-box, but feel free to add your own configuration options ! Check the Ansible ovirt_vm documentation, there are plenty of options to add.
| $ vi create-vm.yml — – hosts: olvm become: yes become_method: sudo gather_facts: no vars_files: – password.yml tasks: – name: Login to OLVM manager ovirt_auth: hostname: “{{ olvm_fqdn }}” username: “{{ olvm_user }}” password: “{{ olvm_password }}” ca_file: “{{ olvm_cafile | default(omit) }}” insecure: “{{ olvm_insecure | default(true) }}” tags: – always – name: Create Virtual Machine(s) ovirt_vm: auth: “{{ ovirt_auth }}” cluster: “{{ olvm_cluster | default(‘Default’) }}” template: “{{ olvm_template }}” name: “{{ item }}” state: running memory: “{{ hostvars[item][‘vm_ram‘] | default(‘1GiB’) }}” high_availability: yes cloud_init: host_name: “{{ hostvars[item][‘ansible_host‘] }}” user_name: root root_password: “{{ vm_root_passwd }}” dns_servers: “{{ vm_dns }}” dns_search: “{{ vm_dns_domain }}” nic_name: “{{ vm_nicname | default(‘eth0’) }}” nic_on_boot: true nic_boot_protocol: static nic_ip_address: “{{ hostvars[item][‘ansible_ssh_host‘] }}” nic_gateway: “{{ vm_gateway }}” nic_netmask: “{{ vm_netmask }}” timezone: “{{ vm_timezone }}” custom_script: | runcmd: – hostnamectl set-hostname {{ hostvars[item][‘ansible_host‘] }} – yum -y remove cloud-init wait: true loop: “{{ groups[‘virtualmachines‘] }}” – name: Cleanup OLVM auth token ovirt_auth: ovirt_auth: “{{ ovirt_auth }}” state: absent |
Run below command and when asked for a password enter the password that you provided for the Ansible vault.
| $ ansible-playbook –i hosts.ini -u user –ask-vault-pass create-vm.yml |
Final notes
This is just a simple example on how to use Ansible to automatically generate VMs on your OLVM server. What I specifically like is how to use the template, I highly recommend to investigate the Oracle Linux Image tools for building templates for both OLVM or for Oracle Cloud.
Make sure you check the Ansible oVirt module documentation to see the amount of options you can add to the playbook to create your VM.
Next step is to extend the playbook and write tasks to deploy the applications on your VM !
