Whether you manage your vSphere environment manually or use PowerCLI to automate tasks, Ansible simplifies vSphere...
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.
- 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:
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.
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.
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.