Showing posts with label Infrastructure Automation. Show all posts
Showing posts with label Infrastructure Automation. Show all posts

Tuesday, April 23, 2024

Automate Azure App Service setup using Ansible and Azure DevOps pipeline | How to integrate Ansible with Azure DevOps | How to Create WebApp in Azure Cloud using Ansible

Ansible is an open-source, configuration management tool that automates cloud provisioning, configuration management, and application deployments. 
Ansible Playbooks
Ansible playbooks allow you to direct Ansible to configure your environment. Playbooks are coded using YAML so as to be human-readable. 
Watch steps in YouTube channel:

Automate Azure Web App 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:

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 ansible-azure-sp --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-linux-app-svc.yml

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


- hosts: localhost
connection: local
vars:
resource_group: myResourceGroup
webapp_name: myfirstAwesomeWebApp
plan_name: myAppServicePlan
location: eastus
tasks:
- name: Ensure resource group exists
azure_rm_resourcegroup:
name: myResourceGroup
location: East US
register: rg_result
- debug:
var: rg_result
- name: Create App Service on Linux with Java Runtime
azure_rm_webapp:
resource_group: "{{ resource_group }}"
name: "{{ webapp_name }}"
plan:
resource_group: "{{ resource_group }}"
name: "{{ plan_name }}"
is_linux: true
sku: S1
number_of_workers: 1
frameworks:
- name: "java"
version: "8"
settings:
java_container: tomcat
java_container_version: 9.0

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 Azure App Service
ansible-playbook create-linux-app-svc.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 App Service have been created.

Clean up service principal & Resource Group

az ad sp list --display-name ansible-azure-sp --output table

az ad sp delete --id <pass_the_id>

az group delete --name myResourceGroup --yes --no-wait

Delete Resource group and App Service using Ansible playbook: delete-linux-app-svc.yml
- name: Delete Azure App Service
  hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    webapp_namemyfirstAwesomeWebApp
  tasks:
  - name:
    azure_rm_webapp:
      name: "{{ webapp_name }}"
      resource_group: "{{ resource_group }}"
      state: absent

Tuesday, September 5, 2023

How to Automate Infra setup in Azure Cloud using Terraform and Azure DevOps Pipeline | Automate App Service or Web App creation in Azure cloud using Terraform and Azure DevOps Pipeline and store Terraform state remotely

Automating infrastructure setup in Azure Cloud using Terraform and Azure DevOps is a powerful approach to managing your infrastructure as code (IaC). This allows you to define and manage your Azure resources programmatically, version control your infrastructure configurations, and automate the deployment process. 

We will be creating a Java based WebApp(app service) in Azure cloud using Terraform and automate the process using Azure Devops Pipelines.


Watch Steps in YouTube channel:

Pre-requisites:
Implementation Steps:
  • Create a resource group first in Azure cloud
  • Create storage account, container for Terraform to store state information in Azure cloud remotely.
  • Create pipeline and add Terraform tasks
  • Execute pipeline to deploy resources(App service plan and WebApp) in Azure cloud
  • Verify resources are created in Azure cloud
  • Confirm if the terraform.tfstate file is updated
Create Resource Group in Azure cloud

Login to Azure portal, Create a new RG, or you can skip this step if you already have existing group.

Create Storage Account in Azure cloud
Create a new resource, type storage account


Click on create
select RG, enter unique account name, select standard as performance, select locally redundant storage

Click on create

Create a container inside the storage account
Select the storage account you just created, click on containers under Data storage


enter a name for the container

Create a pipeline in Azure DevOps
Login to Azure Devops, select Pipelines, select use the classic editor to create a pipeline


Configure Pipeline with tasks

Add Terraform installer task to install Terraform on build agent


Add Terraform task 

Configure the task

enter storage account, container, key information as below:


Add terraform tasks to plan, apply
Add Terraform task for plan
change the command to plan from drop down and also select azure subscription from drop down

Add Terraform task for apply
change the command to apply from drop down and also select azure subscription from drop down


Now verify to make sure if we have correct values in each/every task. Now click on Save + Queue.



Click on Save and Run


This confirms that pipeline have successfully created resources in Azure cloud. You can login to Azure portal to see the resources - app service plan and web app.


You can also verify terraform state info which has resources entry for all the resources created
Click on Containers, mytfstatecontainer
Click on terraform.tfstate
Click on Edit to view the content of terraform state file



Clean up resources created in Azure using the pipeline - destroy command

change the command to destroy instead of apply from the drop down


You can either add a new task for destroy or modify to destroy from apply in the existing task.

Now save the pipeline and run the pipeline. check the output of destroy task


Check terraform.tfstate file after destroy.

    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:

      Sunday, December 26, 2021

      Azure DevOps Terraform Integration | How do you integrate Terraform with Azure DevOps | Automate Infrastructure setup using Terraform and Azure DevOps | Remote Store in S3 Bucket

      We will be learning an interesting use case to provision resources in AWS cloud using Terraform and Azure DevOps. We will also learn how to store terraform state info remotely in AWS S3 bucket.

      We will create S3 bucket for storing terraform state info and Dynamo DB table for providing state lock capability. 

      We will try to create an EC2 instance and S3 Bucket using Terraform and Azure DevOps in AWS cloud. Look at the diagram that describes the whole flow. 


      Watch the steps in YouTube channel:
        Pre-requisites:
        • Azure DevOps organization
        • Add Azure pipelines Terraform tasks 
        • Create AWS service connection in Azure DevOps for Terraform to use
        • Create service connection for connecting to GitHub
        • Create S3 bucket for storing TF state
        • Create dynamo DB table for providing lock capability
        • I have provided my public repo as an example which you can use.

        Step # 1 - Create S3 Bucket:
        Login to AWS, S3. Click on create S3 bucket.

        Give unique name to the bucket, name needs to be unique.

        Block all public access, enable bucket versioning as well.

        Enable encryption.


        Step # 2 - Create DynamoDB Table
        Create a new table with LockID as partition Key



        Step # 3 - Create Service connection to connect to AWS from Azure DevOps
        Go to Azure Devops, select your project. Project Settings

        Click Service Connections




        Select AWS for Terraform
        Enter Access Key, Secret Key and region code, enter name for service connection and choose Grant Access to all pipelines.


        Click Save.

        Create Service connection for connecting to GitHub

        Save

        Step 4 - Create a new Release Pipeline
        Click on Releases, New, choose New Release pipeline


        Select empty job. 

        Click on Add Artifacts


        Choose GitHub, select Github service connection, select the repo


        Click on Add tasks, type terraform
        choose the task


        Add task, search for install terraform and select installer task

        It should show something like this:
        Add another terraform task for init

        Search for terraform and add task

        select AWS from drop down, choose init as command and add -reconfigure as additional command arguments. Select AWS service connection and enter bucket name


        Add another task for plan, select right values.
        enter -out dev-plan 


        Add another task by cloning for apply.
        enter "dev-plan" as additional arguments


        Click on Save. 
        Create Release, now job should be running.





        Login to AWS--> S3 Bucket, you should see terraform state info 


        How to destroy all the resources created using Terraform?

        Clone the current infra setup release pipeline.

        Modify the pipeline name.

        Add 
        Modify the apply task to as shown in the diagram
        enter -destroy as additional argument.


        Click on Create Release to make sure all the resources are destroyed.


        🚀 Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp – Sep 2026

        Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp from Coach AK - Sep 2026 Schedule