Ansible environment#

The Ansible development environment in Azure will comprise the following:

  • Windows Server 2019 virtual machine
  • Linux virtual machine running CentOS

Each virtual machine in Azure requires several Azure resources, and each of these resources is managed by different Ansible Modules.

Ansible Modules#

The following are the required Azure resources and the corresponding Ansible Modules:

Azure Resource Azure Module
Resource Group azure_rm_resourcegroup
Virtual Network azure_rm_virtualnetwork
Subnet azure_rm_subnet
Public IP Address azure_rm_publicipaddress
Network Security Group azure_rm_securitygroup
Network Interface Card azure_rm_networkinterface
Custom Script Extension azure_rm_virtualmachineextension
Virtual Machine azure_rm_virtualmachine

Ansible codifies your infrastructure in YAML files called Ansible playbooks. You will use pre-written Ansible playbooks to deploy the Ansible development environment to Azure.

Resource Dependency
Several of the Azure resources depend on other resources. These dependencies mean that you have to run the playbooks in the right order.

Let’s start exploring the playbooks we will cover in this lesson one by one.

Create a resource group#

First, you need to create a resource group. Review the playbook below:

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
/
azure_create_resource_group.yaml
Create a resource group

Ansible playbook#

Ansible playbooks are written in YAML and have two main sections:

  • Hosts
  • Tasks

Hosts

Hosts determine which hosts are targeted by the playbook. Refer to Line 2-3 for hosts in the above playbook.

Tasks

Tasks define what Ansible will execute sequentially. From Line 5 onwards, you can observe all the tasks that Ansible will perform.

The azure_create_resource_group.yaml playbook has only one task, Create Resource Group. The task uses the azure_rm_resourcegroup Ansible module to deploy a resource group to Azure.

connection: local
Ansible uses SSH as the default connection. Adding connection: local under the hosts' section runs the tasks locally rather than connecting with SSH.

Execute the playbook by clicking on the Run button. The button runs the following command in the terminal:

Execute the playbook

Deploy a Windows virtual machine#

We will require the following resources to deploy a virtual machine on Azure:

  • Virtual network
  • Subnet
  • Public IP address
  • Network Security Group
  • Network interface
  • Azure virtual machine

Review the playbook below and note the Ansible Modules required to deploy an Azure virtual machine:

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
/
azure_create_windows_vm.yaml
Create a Windows virtual machine

Execute the playbook by clicking on the Run button. The button runs the following command in the terminal:

run azure_create_windows_vm.yaml playbook

A prompt will display for the password. You can provide a password of your own choice. You will use this same password later to connect to the virtual machine.

pause Ansible Module
Pauses playbook execution for a set amount of time or until a prompt is acknowledged. Read more about the pause module.

Deploy a Linux virtual machine#

Deploying a Linux virtual machine with Ansible is identical to that of a Windows virtual machine. Review the azure_create_linux_vm.yaml playbook below. It uses the same modules as before and only requires the arguments to be changed:

  • The ports allowed in the Network Security Group.
  • Image selected for the virtual machine.
This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
/
zure_create_linux_vm.yaml
Create a Linux virtual machine

Execute the playbook by clicking on the Run button. Once again, use the password of your own choice when prompted. The following command is executed when you click the Run button:

run azure_create_linux_vm.yaml playbook

Delete the environment#

All the above infrastructure lies in the free tier. You will be using these resources in the upcoming lessons and chapters. In case you are going to visit the next lessons later, you can take down the resources to avoid any unexpected bills.

You can take these resources down by executing the playbook below.

Disclaimer: Run the playbook at your own risk!
It is highly recommended you use a development Azure subscription.

Review the playbook:

This code requires the following environment variables to execute:
AZURE_SUBSCRIPTION_ID
Not Specified...
AZURE_CLIENT_ID
Not Specified...
AZURE_SECRET
Not Specified...
AZURE_TENANT
Not Specified...
/
azure_delete_ansible_env.yaml
Delete the Ansible environment

Execute the playbook by clicking on the Run button. It will execute the following command:

run azure_delete_ansible_env.yaml playbook

In this lesson, you were introduced to the Ansible playbooks and modules to create Linux and Windows virtual machines in Azure.


Download the Source Code
You can download the playbooks for this lesson from the Github repository, become Ansible.

Deploy to AWS
Connect to the Environment
Mark as Completed
Report an Issue