CREATING CENTOS VM ON NUTANIX USING ANSIBLE
INTRODUCTION
Nutanix Prism is a management solution for virtualized data center environments. Prism manages the entire stack from the storage and computes infrastructure up to virtual machines (VMs).
Here, we’ll discuss how to create a CentOS VM on Nutanix using the Ansible playbook. Ansible is a high-level automation engine that automates cloud provisioning, configuration management, and application deployment.
Fig: Stages of creating CentOS VM on Nutanix using Ansible
PREREQUISITES
- Nutanix Prism login credentials
- Nutanix cluster URL, cluster name, subnet name, and disk image name
ANSIBLE FOR NUTANIX VM CREATION
Ansible roles are sets of Ansible defaults, files, tasks, templates, variables, and other Ansible components that work together. ‘nutanix_vm_provisioner’ is a fundamental Ansible role to provision VMs on a Nutanix AHV using APIv3.
Variables Required:
- api_version: # Add API version, it’s best to use v3 (if available)
- cluster_url: # IP address where you would typically log in to PRISM
- prism_user: # An account with permissions to provision on the cluster
- prism_password: # The password to your account
- cluster_name: # Name of the cluster to provision against
- subnet_name: # Name of the Subnet to add VMs to
- image_name: # Name of the disk image or ISO to use
- vm_defs: # The list of dicts that defines the VMs to be created
Ansible Roles Required
A single role, ‘nutanix_vm’ is required for the VM creation.
’nutanix_vm’ role deals with creating a VM with the provided subnet, cluster, and the OS image by authenticating with a session cookie for logging into the Prism. The variables used in the code are placed in the ‘defaults’ folder of the role ‘nutanix_vm’. The ‘defaults/main.yaml’ file used is shown below:
Filename: defaults/main.yaml
The templates used for the playbook are placed in the ‘templates’ folder of the role ‘nutanix_vm’. Each of the template files ( which is passed as Jinja2 file/j2 file ) are listed below:
J2 Filename 1: templates/cloud-init.yml.j2
J2 Filename 2: templates/nutanix.py.j2
Given below is the sample template we used to create the VM:
J2 Filename 3: templates/nutanix.yml.j2
J2 Filename 3: templates/vm-body.yml.j2
Once the above setup and configuration are done, the following tasks (by default, tasks are available in the tasks folder) are executed by Ansible one by one. The ‘tasks’ for the role ‘nutanix_vm’ are explained below:
TASK 1: LOG IN TO NUTANIX PRISM VIA SESSION COOKIE
This step shows how to log in to the Prism by creating a session cookie. A session cookie is used to store information in temporary memory and is deleted after the login is completed. This makes the authentication process more secure. The following task is executed by Ansible to log in to the Prism using a session cookie.
Filename: tasks/session_cookie.yml
TASK 2: FIND THE NUTANIX CLUSTER LISTS AND UNIQUE IDENTIFIERS
The following task shows how to get the list of clusters and their unique identifiers (UUIDs). It also shows you how to find the required cluster name and the corresponding UUID for creating the VM.
Filename: tasks/cluster_uuid.yml
TASK 3: FIND THE NUTANIX SUBNET LISTS AND UNIQUE IDENTIFIERS
This task shows how to get the list of subnets and their unique identifiers (UUIDs). It also shows you how to find the required subnet name and the corresponding UUID for creating the VM.
Filename: tasks/subnet_uuid.yml
TASK 4: FIND THE NUTANIX OS IMAGE FOR THE VM CREATION
This task shows how to get the list of OS images and their unique identifiers (UUIDs). It also shows you how to find the required image name and the corresponding UUID for creating the VM.
Filename: tasks/image_uuid.yml
TASK 5: CONFIGURE THE CLUSTER, IMAGE, AND SUBNET UNIQUE IDENTIFIERS
This step shows you how to configure and use the universally unique identifiers (UUIDs) of the cluster, image, and subnet found from the previous steps for the VM creation.
Filename: tasks/configure_uuids.yml
TASK 6: PROVISION NUTANIX VM WITH CENTOS IMAGE
This task shows how to provision a new VM (uses CentOS image here) in the Nutanix cluster. You can do this by running the python script ‘nutanix.py’ (referred to above in the templates section), after providing the configuration parameters and image template as defined above.
‘nutanix.py’ is a Python script that generates a dynamic inventory that Ansible can understand by making API requests to related Nutanix clusters. To get a better idea about this, please refer: https://github.com/mattkeeler/ansible-nutanix-inventory
Filename: tasks/provision_vm.yml
TASK 7: MAIN TASK TO RUN OTHER TASKS AND CREATE CENTOS VM
This is the main task, which will initiate and execute other tasks defined in the tasks folder (as explained above) to create the CentOS VM.
Filename: tasks/main.yaml
HOW TO CALL THE ROLE
This step shows how to execute the role ‘nutanix_vm’. Create a yaml file ‘call.yaml’ in the location where the ‘nutanix_vm’ role is present. Then, add the code given below in the yaml file.
Execute the role by running the command given below:
When the CentOS creation is completed, the IP address and name of the new CentOS will be printed in the terminal as follows:
CONCLUSION
Ansible is an efficient and relatively simple tool for automating infrastructure (IaC – Infrastructure as Code). In this tutorial, I used the Ansible role ‘nutanix_vm_provisioner’ to create a CentOS VM on the Nutanix cluster. Ansible can also help with tasks like app deployments, configuration management, and workflow orchestration.
REFERENCES
About The Author
Sreedevi J S
Cloud Dev-Ops Engineer | Cloud Control
Cloud DevOps Engineer with more than three years of experience supporting, automating and optimizing deployments to hybrid cloud platforms using DevOps processes, tools, CI/CD, containers, and Kubernetes in both Production and Development environments.