Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Create multiple Azure vnet (Virtual Network) using ansible

This is a second part of Deploy create Virtual Network vNET in Azure cloud using Ansible, in this part I am creating multiple vNets with single Ansible script. All the info and details related to new vNet added in separate vnet variable yml file in json format as shown below.

Azure Virtual network json ansible yaml mutliple vnets creation resources arm templates resource group address prefixes cidr location vnet name.jpg

Next in below ansible yaml script, I am using foreach loop on the provided json information and create mutliple Virtual Network on the Microsoft Azure. You can use ansible-playbook command to deploy this yaml playbook.

Azure.azcollection vnet variables azure virtual network ansible redhat azure_rm_virtualnetwork subnet address prefixes cidr loop configuration.jpg

Download this mutli-azvnets.zip here or it is also available on github.com/janviudapi.

# multi-azvnets.yaml - Ansible yaml code to create multiple AZ vnets
---
- name: Create Azure vNets (Virtual Networks)
  hosts: localhost
  connection: local
  gather_facts: false

  collections:
    - azure.azcollection

  tasks:
    - name: Include variable file
      ansible.builtin.include_vars:
        file: vnet_var.yml
        name: vnet_var

    - name: Create a vNets (Virtual Networks)
      azure.azcollection.azure_rm_virtualnetwork:
        resource_group: "{{ item.resource_group }}"
        name: "{{ item.name }}"
        location: "{{ item.location }}"
        address_prefixes_cidr: "{{ item.address_prefixes_cidr }}"
          # - "10.23.0.0/16"
          # - "10.24.0.0/16"
      loop: "{{ vnet_var.vnet_resources }}"


# vnet_var.yaml - json variables info of AZ vNet resource
---
# resource_group: vcloud-lab.com
# vnet_name: global_vnet_eastus
# location: eastus
# vnet_address_prefixes_cidr: 10.10.0.0/16
# subnet01_name: prod01-10.10.1.x
# subnet01_address_prefix: 10.10.1.0/24

vnet_resources:
  - {
      resource_group: rg-globalnetwork-prod,
      name: prod-vnet-wus3-01,
      vnet_address_prefixes_cidr: ["10.252.0.0/16"],
      location: West US3
    }
  - {
      resource_group: "rg-globalnetwork-prod",
      name: "prod-vnet-weu-01",
      vnet_address_prefixes_cidr: ["10.253.0.0/16"],
      location: "West Europe"
    }

Useful Articles
How to install and configure Ansible on Ubuntu
Get Started: Configure Ansible for Azure Cloud Infrastructure
Install the Microsoft Azure CLI on Ubuntu Linux

Redhat ansible logo azure vmware vsphere awx tower controller yml yaml configuration automation.pngDeploy create Virtual Network vNET in Azure cloud using Ansible
Quickstart How to authenticate Ansible with Microsoft Azure
Ansible authenticate to Microsoft Azure using credential profiles Part 1
Ansible authenticate to Microsoft Azure using credential profiles Part 2
Ansible authenticate to Microsoft Azure using credential inside yaml file

Go Back

Comment

Blog Search

Page Views

12273418

Follow me on Blogarama