Ansible to Provision Multiple Azure Virtual Machines

In this article I will demonstrate how to provision multiple virtual machines with the help of Ansible automation (Ansible role), this will also include the provision of a standard Azure load balancer with a public IP address.  So the Ansible script should be able to perform the below tasks automatically.

  • Provision of an NSG (Network security group)
  • Provision 2 virtual machines
  • Provision of a standards load balancer
  • Provision of a public IP address for the load balancer
  • Install Nginx package on both machines

In this article, I will not describe Ansible installation or Ansible role, for this please read my earlier post.

https://www.techiescorner.in/ansible-play-book-and-role-tutorial/

Before provision virtual machines a VNET, subnet, and NSG are required, the playbook to provision these resources also included in this tutorial.

First, create a directory named “roles” for Ansible to lookup for the playbooks.  Inside the roles create a role for Nginx servers. I have named the role name “nginx-server-prov”.

Create an Ansible Role

MacBook-Pro:roles $ ansible-galaxy init nginx-server-prov

- Role nginx-server-prov was created successfully

The created ngix-server-prov contains the following folders, this is where we will write our playbooks.

MacBook-Pro:roles $ tree .
.
└── nginx-server-prov
    ├── README.md
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

9 directories, 8 files

Create playbooks for vnet, subnet, and NSG

Create a new playbook to provision a VNET (vnet.yaml) in the tasks folder and add the below code.

---
- name: Create a virtual network
  azure_rm_virtualnetwork:
    resource_group: "{{resource_group_name}}"
    name: "vnet-{{env}}"
    location: "{{resource_group_location}}"
    address_prefixes_cidr:
        - "{{resource_vnet_octate}}.1.0.0/16"
  tags:
        - vnet-stage

The code which is in the {} are variable so declare the variable in the var/main.yaml file as follows.

---
# vars file for nginx-server-prov
resource_group_name: "Techies_Devops"
resource_group_location: "East US"
resource_vnet_octate: "60"
env: "test"

Next. create a playbook (nsg.yaml) to create an NSG in the tasks folder and add the below code.

---
# Create a security group
- name: Configuring nginx NSG Rules
  azure_rm_securitygroup:
      resource_group: "{{resource_group_name}}"
      name: "nsg-{{env}}-{{resource_nsg_string}}"
      purge_rules: yes
      rules:
          - name: 'http'
            protocol: Tcp
            destination_port_range: 80
            access: Allow
            priority: 101
            direction: Inbound

Add the variable to the same var/main.yaml file. I will add a complete variable file at the end.

Next, create a playbook to provision a subnet (subnet.yaml) under the tasks folder and add the below code.

---
- name: Create a subnet for nginx servers nsg
  azure_rm_subnet:
    resource_group: "{{resource_group_name}}"
    virtual_network_name: "vnet-{{env}}"
    name: "subnet-{{env}}-nginx"
    address_prefix_cidr: "{{resource_vnet_octate}}.1.2.0/24"
    security_group:
      name: "nsg-{{env}}-nginx"
      resource_group: "{{resource_group_name}}"
  tags:
          - nginx

Before going to provision the virtual machines we can provision VNET, NSG, and subnet first, for this add the below code in the main.yaml file.

---
# tasks file for nginx-server-prov
- import_tasks: vnet.yaml
- import_tasks: nsg.yaml
- import_tasks: subnet.yaml

To call an ansible role create a site file outside the Roles directory. I have created a file

site_nginx.yaml and included the role as follows.

---
- name: Deploying the resources
  hosts: localhost
  roles:
          - {role: nginx-server-prov}

Now we are good to execute the below command to provision the resources.

MacBook-Pro:Temp$ ansible-playbook site_nginx.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 [Deploying the resources] **************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [nginx-server-prov : Create a virtual network] *****************************************************************************************************************
changed: [localhost]

TASK [nginx-server-prov : Configuring nginx NSG Rules] **************************************************************************************************************
changed: [localhost]

TASK [nginx-server-prov : Create a subnet for nginx servers nsg] ****************************************************************************************************
changed: [localhost]

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

Log in to the Azure portal and make sure that the resource provisioned successfully.

Next, we have to provision virtual machines for the Nginx server.

Leave a Reply

Your email address will not be published.