Ansible Pilot

Creating an Azure Virtual Network with Ansible Playbook

Learn how to use Ansible’s Azure modules to automate the creation of a virtual network with subnets and a public IP address in Microsoft Azure Cloud.

April 30, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Creating an Azure Virtual Network with Ansible Playbook

As organizations continue to adopt cloud technologies, the need for automated infrastructure provisioning and configuration management becomes more important than ever. Ansible, a popular open-source automation tool, has several modules for working with Microsoft Azure Cloud. In this article, we will explore how to use Ansible’s Azure modules to automate the creation of a virtual network with subnets and a public IP address in Microsoft Azure Cloud.

Prerequisites

To follow along with this tutorial, you will need the following:

ansible-galaxy collection install azure.azcollection

Creating an Azure Virtual Network with Ansible Let’s start by creating an Ansible playbook to create a virtual network with subnets and a public IP address in Azure.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

Code

  1. Define Variables First. We need to define the variables that will be used throughout the playbook. Open a new file and add the following code:
---
- name: Azure Virtual Network
  hosts: all
  vars:
    resource_group_name: "aznetwork-rg"
    location: westeurope
    tags:
      environment: dev
      department: infrastructure
    network:
      name: "aznetwork-vnet"
      address: "10.210.0.0/16"
      subnets:
        - name: "aznetwork-vnet-frontend"
          address: "10.210.1.0/24"
        - name: "aznetwork-vnet-backend"
          address: "10.210.2.0/24"
      publicip:
        name: "aznetwork-public-ip"
        domain: aznetwork-labs

In this code, we define the following variables:

  1. Create Virtual Network Next.

We will use the azure_rm_virtualnetwork module to create the virtual network. Add the following task to the playbook:

- name: VNet - Create virtual network
  azure.azcollection.azure_rm_virtualnetwork:
    resource_group: "{{ resource_group_name }}"
    name: "{{ network.name }}"
    address_prefixes:
      - "{{ network.address }}"
    tags:
      environment: "{{ tags.environment }}"
      department: "{{ tags.department }}"
  register: output_vmet

In this code, we use the azure_rm_virtualnetwork module to create the virtual network with the following parameters:

The register keyword allows us to save the output of the task in a variable called output_vmet.

  1. Add Subnets. Now, let’s use the azure_rm_subnet module to add subnets to the virtual network. Add the following tasks to the playbook:
- name: VNet - Add frontend subnet
  azure.azcollection.azure_rm_subnet:
    resource_group: "{{ resource_group_name }}"
    name: "{{ network.subnets[0].name }}"
    address_prefix: "{{ network.subnets[0].address }}"
    virtual_network: "{{ network.name }}"
  register: output_vnet_frontend

This is an Ansible playbook written in YAML format for creating an Azure Virtual Network. It defines variables such as the resource group name, location, and tags, as well as the network, subnet, and public IP address configurations. The playbook executes tasks such as creating the virtual network, adding frontend and backend subnets, and creating a public IP address using the Azure modules provided by the Azure Collection for Ansible. The full Ansible Playbook looks like the following:

---
- name: Azure Virtual Network
  hosts: all
  vars:
    resource_group_name: "aznetwork-rg"
    location: westeurope
    tags:
      environment: dev
      department: infrastructure
    network:
      name: "aznetwork-vnet"
      address: "10.210.0.0/16"
      subnets:
        - name: "aznetwork-vnet-frontend"
          address: "10.210.1.0/24"
        - name: "aznetwork-vnet-backend"
          address: "10.210.2.0/24"
      publicip:
        name: "aznetwork-public-ip"
        domain: aznetwork-labs
    tasks:
      - name: VNet - Create virtual network
        azure.azcollection.azure_rm_virtualnetwork:
          resource_group: "{{ resource_group_name }}"
          name: "{{ network.name }}"
          address_prefixes:
            - "{{ network.address }}"
          tags:
            environment: "{{ tags.environment }}"
            department: "{{ tags.department }}"
        register: output_vmet

      - name: VNet - Add frontend subnet
        azure.azcollection.azure_rm_subnet:
          resource_group: "{{ resource_group_name }}"
          name: "{{ network.subnets[0].name }}"
          address_prefix: "{{ network.subnets[0].address }}"
          virtual_network: "{{ network.name }}"
        register: output_vnet_frontend

      - name: VNet - Add backend subnet
        azure.azcollection.azure_rm_subnet:
          resource_group: "{{ resource_group_name }}"
          name: "{{ network.subnets[1].name }}"
          address_prefix: "{{ network.subnets[1].address }}"
          virtual_network: "{{ network.name }}"
        register: output_vnet_backend

      - name: Create a public IP address
        azure.azcollection.azure_rm_publicipaddress:
          resource_group: "{{ resource_group_name }}"
          allocation_method: Static
          name: "{{ network.publicip.name }}"
          domain_name_label: "{{ network.publicip.domain }}"
          sku: standard
        register: output_publicip

Recap

This Ansible playbook uses the Azure Collection to create an Azure Virtual Network. The playbook includes variables for the resource group name, location, tags, network, subnet, and public IP address configurations. The tasks in the playbook include creating the virtual network, adding frontend and backend subnets, and creating a public IP address.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases