Menu

Virtual Geek

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

Deploy create Virtual Network vNET in Azure cloud using Ansible

This is ansible code example for deploying creating vNET (Virtual Network) on Microsoft Azure cloud. This is working basic ansible yaml/yaml script.

Below example shows how it is deployed using ansible-playbook command from the linux using ansible-playbook command.

Download this script here or it is also available on github.com/janviudapi.

ubuntu@ansible:/home$ cd /home/ubuntu/Documents/Azure_Ansible/vnet
ubuntu@ansible:~/Documents/Azure_Ansible/vnet$ ls
vnet_var.yml  vnet.yaml
ubuntu@ansible:~/Documents/Azure_Ansible/vnet$ ansible-playbook vnet.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Create Azure vNet (Virtual Network)] *****************************************************************************************************************************************

TASK [Include variable file] *******************************************************************************************************************************************************
ok: [localhost]

TASK [Create a vNet (Virtual Network)] *********************************************************************************************************************************************
changed: [localhost]

TASK [Add subnet to vNet (Virtual Network)] ****************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ubuntu@ansible:~/Documents/Azure_Ansible/vnet$ 

There are two files in the folder.

Read Article: Azure add create a Subnet to existing Virtual Network using PowerShell

microsoft Azure redhat ansible virtual network vnet deployment inventory parsed host list cloud automation IAC infrastructure as code completed.png

The first file "vnet_var.yml" in the folder is declared as variables, in this file modify the information related to deployment names for your organization.

1
2
3
4
5
6
7
---
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

This is second file vnet.yaml. In general there is no need to modify this file (This example works best), unless until you need to add any additional parameters (If you need additional settings) as mentioned on the ansible azure official kb - https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_virtualnetwork_module.html. To add additional subnets copy subnet task and change the names accordingly and update variables file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
---
- name: Create Azure vNet (Virtual Network)
  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

  - name: Create a vNet (Virtual Network)
    azure_rm_virtualnetwork:
      resource_group: "{{ vnet.resource_group }}"
      name: "{{ vnet.vnet_name }}"
      location: "{{ vnet.location }}"
      address_prefixes_cidr: "{{ vnet.vnet_address_prefixes_cidr }}"
        #- "10.23.0.0/16"
        #- "10.24.0.0/16"

  - name: Add subnet to vNet (Virtual Network)
    azure_rm_subnet:
      resource_group: "{{ vnet.resource_group }}"
      name: "{{ vnet.subnet01_name }}"
      address_prefix: "{{ vnet.subnet01_address_prefix }}"
      virtual_network: "{{ vnet.vnet_name }}"

Once the resources are deployed you can verify Virtual Network configuration on Azure Portal. It looks good. Ansible is idempotent so additional subnets you can add later in the code by modifying variables. 

Microsoft Azure Redhat Ansible resource group virtual network idempotent yaml yml vnet bastion address space suffix bgp dns free subscription.png

Few of the properties cannot be changed for existing resources. Example vnet name and location.

Useful Articles
Create a Azure Virtual Network with Subnet using PowerShell
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Create an Azure Automation account on using Portal PowerShell and AzureCLI
Gather Azure Virtual Network inventory information using PowerShell
Get Azure virtual machine backup reports using Powershell
Why is my Azure recovery services vault not getting deleted?
Microsoft Azure PowerShell cmdlet 'this.Client.SubscriptionId' cannot be null
Azure PowerShell Get-AzStorageShare Could not get the storage context

Go Back

Comment

Blog Search

Page Views

8206966

Follow me on Blogarama