Get started Bring yourself up to speed with our introductory content.

Use Ansible in vSphere to automate management tasks

Ansible is a powerful tool to simplify and automate management duties in VMware vSphere environments. Ansible eases VM provisioning, as well as some vMotion tasks.

Whether you manage your vSphere environment manually or use PowerCLI to automate tasks, Ansible simplifies vSphere...

infrastructure management.

Ansible is a configuration management tool that is easy to set up and run. It can help you quickly accomplish and automate common tasks.

You do not need to be an Ansible guru to use the tool, but a solid understanding of vSphere and the various constructs that Ansible uses are necessary.

Defining Ansible variables

One of the most common tasks that Ansible automates in vSphere is provisioning VMs. There is already a template for this in vCenter, but you can provision VMs without an existing template.

The first step of provisioning VMs using Ansible in vSphere is putting together your VM definitions that dictate the tasks and parameters Ansible will use. Definitions include the following:          

  • Name: Defines what the VM will be called.
  • Customization: Allows you to add any guest customizations you would like to perform when you provision the VM.
  • Disks: Enables you to allocate disks -- including the template disk -- to the VM.
  • Networks: Defines which networks to allocate to the VM, as well as whether IP addressing should be static or done via the Dynamic Host Configuration Protocol (DHCP).
  • State: Specifies whether the VM is absent or present. This is extremely important if your intention is to manage the full lifecycle of a VM through code. Defining the state as present ensures that the VM exists. If, for whatever reason, a VM should no longer exist, simply change the state to absent.
  • Template: Tells the program which vSphere template to use for the VM.
  • VM hardware: Defines the amount of memory/CPU to allocate to the VM.

The following YAML defines the various parameters for new VMs:

An Ansible play to provision VMs

Creating a VM provisioning playbook

With this script, you have created a playbook -- a set of Ansible scripts -- to provision your VMs as a single Ansible task. It will be driven by the parameters you define.

Parameters for VM provisioning with Ansible in vSphere

This one task handles provisioning the defined VMs. It accommodates most parameters that you may need to address, including static IPs and DHCP, different templates and states, customizations, and hardware settings.

In this task, when you define a template to use, you also must wait for vSphere to report an IP address back to you via VMware tools within the guest VM. By adding this parameter, you can wait for the VMs to be provisioned and available before you continue on to additional tasks. If you don't wait for an IP address to be available, any further tasks dependent on the VM's availability will fail.

The ability to wait for an IP address is only beneficial when using a template to provision the VMs. Without a template, the VM's OS is not present, so there are no VMware tools to provide the VM's IP address. If you need to spin up VMs without an OS or you rely on another provisioning mechanism to install the OS, you don't need an IP address.

Additional tasks

Provisioning VMs is not the only thing you can do with Ansible in vSphere. You can also use Ansible code to:

  • automate the placement of a host into or out of maintenance mode, which helps you perform various tasks that require VMs to be in a specific state;
  • manage vCenter data centers and clusters by automating their creation, renaming or deletion;
  • add, remove, rename or manage IP-related settings for VMkernel interfaces;
  • add, remove, rename or manipulate vSwitches or the uplinks associated with them;
  • add, remove, rename or move port groups to a different vSwitch and manage the port group settings, such as virtual LANs and security settings;
  • move vMotion VMs between hosts that are part of a vSphere cluster; and
  • backup, restore or reset vSphere host configurations.

Another powerful thing you can do with Ansible in vSphere environments is orchestrate various aspects of the infrastructure.

Assume you have a few vSphere hosts configured for network connectivity and nothing else. You can use Ansible to orchestrate the provisioning of the whole infrastructure, which could include configuring additional host networking and storage; spinning up various core infrastructure VMs on a single host, including vCenter; adding hosts to the cluster; and migrating all of the core VMs into the cluster. Ansible lets you tackle all of these tasks from a single playbook.

This was last published in April 2018

Dig Deeper on Scripting administrative tasks

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

Which tasks do you use Ansible to automate in vSphere?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchVirtualDesktop

SearchDataCenter

SearchCloudComputing

Close