Azure DevOps CICD with Ansible Infra Automation

Azure DevOps CICD with Ansible Infra Automation

Azure DevOps CICD with Ansible Infra Automation blog will help you understand how to use Ansible to create an Azure DevOps CICD pipeline. Ansible is an open-source tool that automates cloud provisioning, configuration management, and application deployments. Using Ansible you can provision virtual machines, containers, networks, and complete cloud infrastructures. In addition, Ansible allows you to automate the deployment and configuration of resources in your environment.

Ansible includes a suite of Ansible modules that can be executed directly on remote hosts or via playbooks.

Users can also create their own modules. Modules can be used to control system resources — such as services, packages, or files — or execute system commands.

Ansible with Full CICD Workflow :

Things we are going to achieve in this lab scenarios are given below :

  1. Create an WebApp and the VM using Ansible Infra automation
  2. Provision the Azure DevOps Ansible Team Project with the Azure DevOps Demo Generator tool.
  3. Configure application and database deployment, using Continuous Deployment (CD) in the Azure DevOps
  4. Initiate the complete CICD process.

Steps to Implement the CICD process for AKS :

  1. Login to Azure DevOps Demo Generator using http://azuredevopsdemogenerator.azurewebsites.net/?TemplateId=77372&Name=AKS
  2. From the choose template option, we need to clone the Ansible and then click on Import.
create-new-azure-devops-project-1

3. Once after we import , we will be able to see the Azure DevOps project imported like below.

4. When we go to the Repositories , we will be able to see the code that we have imported to create a Website automation using ansible.

GitHub repositories


Steps to make the Azure Environment ready :

  1. Login to the Azure portal and open the cloud shell to run the commands to create the resources.
  2. Login to the cloud shell, and we need to create certain things that are shown below such as the service principal and VM’s.

az ad sp create-for-rbac — name AnsibleDemo

azure-cloud-shell-3

3. Once after the service principal is created , we can now create a VM for the ansible server .

create-vm-for-ansible-server

4. Click Connect and copy the login command under the SSH tab.

5. Now we must create a directory named .azure in the home directory and a credentials file under it. Type the following commands to create them.

mkdir ~/.azure

vi ~/.azure/credentials

6. Now, we need ssh authentication using Ansible Control Machine private/public key pair. Now let us create a pair of private and public keys. Run the following command to generate a private/public key pair for ssh and to install the public key in the local machine.

ssh-keygen -t rsa

chmod 755 ~/.ssh

touch ~/.ssh/authorized_keys

chmod 644 ~/.ssh/authorized_keys

ssh-copy-id ansibledemo@127.0.0.1

7. We need SSH private key to created SSH endpoint in Azure DevOps service. Run the following command to get the private key. Copy the private key to notepad.

cat ~/.ssh/id_rsa

Steps to make the Azure DevOps CICD ready :

  1. Login to Azure Devops and then we need to go Project Settings –> Service Connections. Select +New service connection and select SSH

2. Next , we need to go to the repos and then change the ansible yaml file to give the details of the Azure resources.

After making these changes, try to commit the file and then start the CI pipeline .

3. Navigate to Pipeline » Releases. Select Ansible-CD and click Edit pipeline.

ansible-cd-3
ansible-cd-3.1

4. Under the Inventory section, select Host list as inventory location and enter pubic ip of your ansible vm in Host list field as shown below.

ansible-cd-4

5. Select the Azure App Service Deploy task. Select the Azure subscription from the drop-down list and click Authorize to configure Azure service connection. And this application requires few app settings to connect to the MySQL database provisioned using Ansible script. That we are updating using the App settings parameter in the task.

ansible-cd-5
ansible-cd-5.1

Once the release is success navigate to your Azure portal. Search for smh360web in App services. Select the app that created with smh360-xxxx and browse to view the application deployed.

ansible-cd-6

We can Login to the site with the following credentials.

Username: me@smarthotel360.com

Password: 1234

ansible-cd-7
ansible-cd-8

Related Courses

VILT & Self-Paced
Azure Developer Certification

AZ-204: Developing Solutions for Microsoft Azure

This self-paced course will help you prepare for the Azure Developer certification exam AZ-204: Developing Solutions for Microsoft Azure.

BUY NOW

Self-Paced
Azure Developer Certification

AZ-900: Microsoft Azure Fundamentals Tutorial

AZ-900: Microsoft Azure Fundamentals Tutorial provides foundational level knowledge on cloud concepts; core Azure services; security, privacy, compliance, and trust; and Azure pricing and support.

BUY NOW

Self-Paced
Azure DevOps Online Course

AZ-400: Designing and Implementing Microsoft DevOps Solutions

This self-paced course will help you prepare for the Azure DevOps certification exam AZ-400: Designing and Implementing Microsoft DevOps Solutions.

BUY NOW


Sharing is Caring

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top