Wednesday, August 23, 2023

Automate Azure cloud infrastructure setup using Ansible and Azure DevOps pipeline | How to integrate Ansible with Azure DevOps | Integrating Ansible with Azure DevOps Pipelines |

Ansible is an open-source configuration management tool that automates cloud provisioning, configuration management, and application deployments. Using Ansible you can provision virtual machines, containers, network, and complete cloud infrastructures. 

Automate Azure cloud infrastructure setup using Ansible and Azure pipeline



Integrate Ansible with Azure Cloud
Integrating Ansible with Microsoft Azure allows you to automate and manage your Azure infrastructure using Ansible playbooks and modules. Ansible provides a collection of Azure-specific modules that enable you to provision and configure resources in Azure.


To configure Azure credentials, you need the following information:

  • Your Azure subscription ID and tenant ID
  • The service principal application ID and secret

Pre-requisites:

  • Azure account subscription, click here if you don't have one.
  • Azure CLI needs to be installed.
  • Service principal to create any resources in Azure cloud using Azure cloud shell or Azure CLI

Login to Azure

az login

Enter Microsoft credentials

Create Azure Service Principal

Run the following commands to create an Azure Service Principal:

az ad sp create-for-rbac --name <service-principal-name> \ 
--role Contributor \ 
--scopes /subscriptions/<subscription_id>
Save the above output in a file as you will not be able retrieve later.
Create an Ansible playbook

Create a simple playbook to create a resource group in Azure. Make sure you modify the name of the resource group and location below.

---

- hosts: localhost

  connection: local

  tasks:

    - name: Creating resource group

      azure_rm_resourcegroup:

        name: "my-rg12"

        location: "eastus"


Create Azure YAML build pipeline:

Login to Azure Devops --> https://dev.azure.com

Select project dashboard.

Go to Pipelines -> New pipeline --> Click on Azure Repos Git or any SCM where you have playbooks stored. Select repo, click on Starter pipeline.

Add below four pipeline variables with value received from service principal creation.

AZURE_SUBSCRIPTION_ID
AZURE_CLIENT_ID
AZURE_SECRET
AZURE_TENANT
Add below tasks:
  • Install Ansible on build agent
  • Install Ansible rm module on build agent
  • Execute Ansible playbook for creating resource group in Azure cloud.
trigger:
- main
pr: none # Disable PR triggers, can be adjusted as needed
pool:
vmImage: 'ubuntu-latest'
steps:
- script: |
# Install Ansible
pip3 install "ansible==2.9.17"
displayName: 'Install Ansible'
- script: |
# Install Ansible rm module
pip3 install ansible[azure]
displayName: 'Install Ansible rm module'
- script: |
# Run Ansible playbook to create resource group
ansible-playbook create-rg.yml
displayName: 'Run Ansible Playbook'
env:
AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
AZURE_CLIENT_ID: $(AZURE_CLIENT_ID)
AZURE_SECRET: $(AZURE_SECRET)
AZURE_TENANT: $(AZURE_TENANT)

Save the pipeline and run it.


Now Login to Azure cloud to see if the resource group have been created.


Watch Steps in YouTube channel:

    5 comments:

    1. I have followed these instructions, at first it was working fine, but now it's throwing a lot of errors related ansible-glaxy, python-version, and resource group creation.

      ReplyDelete
    2. I have tried a lot of solutions but none of it worked, can you please let us know if there's anything changed?

      ReplyDelete
    3. i am trying to create resource group in azcloud, but PAN ID option those things not enabled for me to upgrade /subscription

      ReplyDelete
    4. I found this post useful because it brings together two powerful automation tools—Ansible and Azure DevOps—to simplify cloud infrastructure provisioning. The explanation of using Ansible playbooks alongside Azure pipelines highlights how repetitive deployment tasks can be standardized and executed consistently across environments. For teams managing Azure resources at scale, this kind of automation can significantly reduce manual effort and configuration errors. Readers interested in similar cloud automation concepts can also explore Cloud Computing Projects to learn more about infrastructure management and cloud-based solutions.

      ReplyDelete
    5. What stood out to me was the focus on service principals, Azure credentials, and integrating infrastructure provisioning directly into the CI/CD workflow. Combining configuration management with pipeline automation creates a more reliable and repeatable deployment process, which is essential for modern DevOps practices. Since Ansible relies heavily on scripting and automation, developers looking to strengthen their automation skills can also check out Python Training Courses, as Python knowledge often

      ReplyDelete

    Complete DevSecOps Learning Roadmap for 2026 to become a DevSecOps Engineer | Top DevSecOps Skills for 2026 | Skills required to become a DevSecOps engineer | DevSecOps Learning RoadMap for 2026

    Complete DevSecOps Roadmap We all know how DevOps is trending right now. And we know where it is going. Let's get to know what skills wi...