AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Creating an Azure Virtual Machine Scale Set using Ansible

By Luca Berton · Published 2024-01-01 · Category: windows-automation

Effortlessly Deploy and Scale Your Infrastructure with Ansible and Azure Scale Sets.

Creating an Azure Virtual Machine Scale Set using Ansible

In the world of cloud computing, scalability is a critical requirement for many organizations. When it comes to managing virtual machines, Azure Virtual Machine Scale Sets is a powerful solution that provides automatic scaling of VMs. Ansible, the popular configuration management tool, also provides a module for managing VM Scale Sets in Azure. In this article, we will explore how to create an Azure Virtual Machine Scale Set using Ansible.

Overview of Azure Virtual Machine Scale Sets

Before we dive into the details of creating a VM Scale Set, let's first understand what it is and how it works. A VM Scale Set is a group of identical, load-balanced virtual machines that can automatically scale up or down based on demand or a schedule. This makes it easy to deploy and manage a set of VMs, while also providing high availability and scalability.

A VM Scale Set can be created in Azure using either the Azure Portal, Azure CLI, or Azure PowerShell. In this article, we will use Ansible to create the VM Scale Set.

Creating an Azure VM Scale Set with Ansible

The Ansible module for Azure VM Scale Sets is azure_rm_virtualmachinescaleset. This module provides a simple way to create, update, and delete VM Scale Sets in Azure. Let's take a look at the playbook in detail.

The playbook starts with defining the hosts and variables required for creating the VM Scale Set. The variables include vmss_vm_size, vmss_admin_username, vmss_capacity, and vmss_tier. These variables are used to define the size of each VM, the admin username for the VMs, the number of VMs in the Scale Set, and the pricing tier for the Scale Set.

The playbook then uses the azure.azcollection.azure_rm_keyvaultsecret_info module to retrieve the administrator password for the virtual machines from an Azure Key Vault. This module retrieves the password and registers it as a variable admin_password.

The playbook then prints debug information about the administrator password using the ansible.builtin.debug module.

Finally, the azure_rm_virtualmachinescaleset module is used to create the VM Scale Set with the specified configuration. The azure_rm_virtualmachinescaleset module creates the Scale Set with the following configuration: • The virtual machines in the Scale Set are created in the specified virtual network and subnet. • The virtual machines use an UbuntuServer image from the Canonical publisher with version 20.04-LTS. • The virtual machines have a 20 GB read-only data disk attached at LUN 0. • The virtual machines have the specified custom data file injected. • The virtual machines are associated with the specified application gateway. • The virtual machines have their OS disks cached with ReadWrite and use Standard_LRS managed disks.

Linksazure.azcollection.azure_rm_virtualnetworkazure.azcollection.azure_rm_subnetazure.azcollection.azure_rm_publicipaddress

Code

This Ansible playbook creates an Azure Virtual Machine Scale Set with the specified configuration. The playbook defines the following variables: • vmss_vm_size: The size of each virtual machine in the scale set. • vmss_admin_username: The username for the administrator account on each virtual machine in the scale set. • vmss_capacity: The number of virtual machines to create in the scale set. • vmss_tier: The pricing tier for the scale set.

The playbook then performs the following tasks: Uses the azure.azcollection.azure_rm_keyvaultsecret_info module to retrieve the administrator password for the virtual machines from an Azure Key Vault. Prints debug information about the administrator password. Uses the azure_rm_virtualmachinescaleset module to create the scale set with the specified configuration.

The azure_rm_virtualmachinescaleset module creates the scale set with the following configuration: • The virtual machines in the scale set are created in the specified virtual network and subnet. • The virtual machines use an UbuntuServer image from the Canonical publisher with version 20.04-LTS. • The virtual machines have a 20 GB read-only data disk attached at lun 0. • The virtual machines have the specified custom data file injected. • The virtual machines are associated with the specified application gateway. • The virtual machines have their OS disks cached with ReadWrite and use Standard_LRS managed disks.

The full Ansible Playbook looks like the following:

Conclusion

Azure Virtual Machine Scale Sets provide a scalable and highly available solution for managing VMs in the cloud. Using Ansible, we can easily create, update, and delete VM Scale Sets in Azure. The azure_rm_virtualmachinescaleset module provides a simple way to manage VM Scale Sets using Ansible. With this module, we can define the configuration of the Scale Set and create multiple identical VMs with a single command.

Related ArticlesAnsible Vault GuideAnsible Inventory Guide

Category: windows-automation

Browse all Ansible tutorials · AnsiblePilot Home