How to Make Deployment with Ansible Azure Modules


Ansible is an open-source product that automates cloud provisioning, configuration management, and application deployments. It includes a suite of modules for interacting with Azure Resource Manager, giving us the tools to easily create and orchestrate infrastructure on Azure.

In order to use Ansible, we need a linux on-prem server or a linux jumpbox vm in azure. Then follow Install and configure Ansible to install ansible. Ansible communicates with Azure using a username and password or a service principal. An Azure service principal is a security identity that we can use with apps, services, and automation tools like Ansible. As we can control and define the permissions as to what operations the service principal can perform in Azure, and also revoke the permission as we want anytime, it provides a better security experience than user name password approach. Please refer to Use portal to create an Azure Active Directory application and service principal that can access resources about how to create the service principal and also Authenticating with Azure about how to authenticate to Azure in Ansible. Just please remember that we also need add the corresponding app as the target resource group or subscription's contributor shown below, otherwise the service principal doesn't have the permission for deployment.

If there is no existing linux server to install Ansible, we can also use Azure Cloud Shell which has just supported Ansible since February which means we don't need install Ansible explicitly in Azure Cloud Shell. Also, if  login to the Cloud Shell as the subscription's admin, there is no need to perform authentication step mentioned in the above either. If you are new to Cloud Shell, please refer to Overview of Azure Cloud Shell firstly.

Now, we can create our playbook and perform the deployment. For example the below configuraiton means we are going to create a CentOS virtual machine with user name and password.

- name: Create Azure VM
  hosts: localhost
  connection: local
  tasks:
  - name: Create VM
    azure_rm_virtualmachine:
      resource_group: myResourceGroup
      name: myVM
      vm_size: Standard_DS1_v2
      admin_username: jacky
      admin_password: changeit
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.3'
        version: latest

The azure_rm_virtualmachine in the above configurtion is just the module to create, update, stop and start a virtual machine. For full list of all the supported modules, please refer to below or Azure Cloud Modules.

Run command "ansible-playbook azure_create_vm.yml" and wait for a while, the vm will be provisioned soon shown below.

Notice that Cloud Shell is backed by Azure Storage's file share, if you have one existing yml playbook and would like to upload it to Cloud Shell, just simply upload the asset in the corresponding storage account shown below. Please refer to Transfer local files to Cloud Shell for more details.

Comments (0)

Skip to main content