Automating vCenter Infrastructure Provisioning and Configuration with Ansible
=============================================================================
In this post, we will explore how to use Ansible to automate the provisioning and configuration of a vCenter-based infrastructure. We will focus on deploying virtual machines (VMs) from linked-clone operations and managing vCenter and ESXi hosts configuration.
Getting Started with Ansible
——————————
Before we dive into the specifics of vCenter provisioning, let’s first cover some basics about Ansible. Ansible is an open-source tool for automating software deployment, configuration management, and application deployment. It uses SSH or WinRM to connect to remote machines and execute tasks.
To get started with Ansible, you can install it from the GitHub repository using the following command:
“`
git clone https://github.com/ansible/ansible.git
“`
This will download the latest version of Ansible and its modules. We recommend using a Python virtual environment to manage test and stable environments.
Installing the VMware Python SDK
———————————–
To use Ansible with vCenter, we need to install the VMware Python SDK. This can be done using pip:
“`
pip install vmware-python-sdk
“`
This will install the SDK and its dependencies.
Creating an Inventory File
——————————
Next, we need to create an inventory file that defines our virtual machines. We will use a simple INI-like file named `sample-app01.inv` with the following content:
“`ini
[webservers]
web01 ansible_host=192.168.1.100 ansible_port=22
web02 ansible_host=192.168.1.101 ansible_port=22
[frontendservers]
fe01 ansible_host=192.168.1.102 ansible_port=22
fe02 ansible_host=192.168.1.103 ansible_port=22
“`
This inventory file defines two groups of virtual machines: `webservers` and `frontendservers`. Each group contains two VMs with different IP addresses.
Playbooks and Modules
————————-
Ansible playbooks are the configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process. If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts are your raw material.
To manage VMs in vCenter, we will use the `vmware_guest` module, which allows us to check the presence and configuration of VMs and proceed with changes according to the result. We will also use the `vmware_power_state` module to power on or off VMs.
Here is a sample playbook that deploys two frontend servers and two web servers from linked-clone operations:
“`yaml
—
– name: Deploy web and frontend servers
hosts: all
become: true
tasks:
– name: Check if credentials are provided
ask_password:
prompt: “Enter vCenter credentials:”
echo: “>>> “
become: true
– name: Deploy web servers
vmware_guest:
hostname: “web01”
password: “{{ username }}”
state: present
validate_certs: no
module_name: “vmware_guest”
– name: Deploy frontend servers
vmware_guest:
hostname: “fe01”
password: “{{ username }}”
state: present
validate_certs: no
module_name: “vmware_guest”
– name: Set network attachments for frontend servers
vmware_network:
hostname: “fe01”
network: “VM Network”
state: present
validate_certs: no
module_name: “vmware_network”
– name: Set network attachments for web servers
vmware_network:
hostname: “web01”
network: “VM Network”
state: present
validate_certs: no
module_name: “vmware_network”
“`
This playbook will deploy two frontend servers and two web servers from linked-clone operations. It will power on the VMs, set their network attachments, and configure them with the specified hostnames and passwords.
Managing vCenter and ESXi Hosts
——————————-
To manage vCenter and ESXi hosts, we can use the `vmware_host` module. This module allows us to check the presence and configuration of hosts, as well as proceed with changes according to the result.
Here is a sample playbook that updates the hostnames of our frontend and web servers:
“`yaml
—
– name: Update hostnames for frontend and web servers
hosts: all
become: true
tasks:
– name: Check if credentials are provided
ask_password:
prompt: “Enter vCenter credentials:”
echo: “>>> “
become: true
– name: Update hostnames for frontend servers
vmware_host:
hostname: “fe01”
new_hostname: “fe-01.example.com”
state: present
validate_certs: no
module_name: “vmware_host”
– name: Update hostnames for web servers
vmware_host:
hostname: “web01”
new_hostname: “web-01.example.com”
state: present
validate_certs: no
module_name: “vmware_host”
“`
This playbook will update the hostnames of our frontend and web servers to `fe-01.example.com` and `web-01.example.com`, respectively.
Conclusion
———-
In this article, we have covered the basics of using Ansible with vCenter. We have seen how to create an inventory file, write playbooks, and manage VMs and hosts with Ansible modules. With these skills, you can start automating your vCenter provisioning and configuration management tasks.
Remember to always use secure passwords and validate credentials before executing any playbooks or modules. We also recommend using a Python virtual environment to manage test and stable environments.
We hope this article has been helpful in getting started with Ansible and vCenter. If you have any questions or need further assistance, please don’t hesitate to reach out to us. Happy automating!