
Setting up an Azure Pipeline for an Ansible playbook with roles involves defining the necessary configuration files and steps to automate the deployment process. Below is a step-by-step guide to help you achieve this. This assumes you have an Azure DevOps account and a repository containing your Ansible playbook and roles, below is the folder structure for ansible framework with Azure cloud

When working with Ansible, the inventory
file, often named hosts.ini
by convention, is crucial. It defines the hosts or groups of hosts on which Ansible will run tasks. Below is an example structure for an hosts.ini
file
[web]
webserver1 ansible_host=13.*.*.*
[database]
dbserver ansible_host=192.*.*.*
In Ansible, the group_vars
directory is used to store variables that are specific to a group of hosts. These variables are automatically loaded by Ansible when it runs a playbook, and they allow you to define common settings for all hosts within a particular group. This makes it easy to manage configurations across multiple hosts, promoting consistency, modularity, and ease of maintenance.

Inside group_vars , define the common vars in the below format

Roles in Ansible provide a way to organize and structure your playbooks, making them more modular, reusable, and maintainable. Roles encapsulate functionality into a directory structure that includes tasks, variables, and handlers, allowing you to manage complex configurations and deployments more efficiently.

Here is the example for the install_ansible role contents

The ansible.cfg
file is a configuration file used by Ansible to define settings and parameters for controlling its behavior. This file plays a crucial role in customizing how Ansible operates, and it is typically located in the project directory or the /etc/ansible/
directory.

Importance of a “master playbook” can vary based on the size and complexity of your Ansible project, your deployment processes, and your organizational requirements. The idea is to have a central point of control and coordination that helps manage the overall execution of your automation tasks

By leveraging Azure Pipelines for Ansible playbooks, you can establish a robust and automated deployment pipeline that enhances collaboration, reduces manual effort, and ensures the reliability and repeatability of your infrastructure configurations.
trigger:
- main
pool:
name: ado-201-pool
# variables:
# - group: Ansible-Azure ##Secret variable group
stages:
# ---------------------------------------------------------------------------------------------------------------------
# Validate stage
# In this stage we are performing ansible validation
# ---------------------------------------------------------------------------------------------------------------------
- stage: validate
jobs:
- job: validateplaybook
steps:
- task: Bash@3
inputs:
targetType: 'inline'
script: |
#!/usr/bin/env bash
#Install module k8s
ansible-galaxy install -r requirements.yml
#check config
ansible --version
#execute ansible command
ansible-playbook -i ./inventory/remote/hosts.ini -b ./master-playbook.yaml --extra-vars "REPO_PATH=$(Build.SourcesDirectory)" --syntax-check
displayName: 'Run ansible validate and check'
# ---------------------------------------------------------------------------------------------------------------------
# Apply stage
#
# In this stage we are performing ansible-playbook.
# ---------------------------------------------------------------------------------------------------------------------
- stage: Apply
dependsOn: validate
condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/main', 'refs/heads/develop'))
jobs:
- job: "waitForValidation"
displayName: "Wait > Wait for manual appoval"
pool: "server"
timeoutInMinutes: "4320" # job times out in 3 days
steps:
- task: ManualValidation@0
timeoutInMinutes: "1440" # task times out in 1 day
inputs:
notifyUsers: [email protected]
instructions: "There are resources being provisioned as part of this deployment, please review the output of Terraform plan before approving."
onTimeout: "reject"
- job: "AnsibleApprove"
displayName: "Ansible > Apply"
dependsOn: "waitForValidation"
steps:
- task: Bash@3
inputs:
targetType: 'inline'
script: |
# Write your commands here
#!/usr/bin/env bash
##Playbook
#check config
ansible --version
#execute ansible command
ansible-playbook -i ./inventory/remote/hosts.ini -b ./master-playbook.yaml --extra-vars "REPO_PATH=$(Build.SourcesDirectory)"
displayName: 'Run ansible apply'
Azure Pipelines enhances the efficiency, reliability, and consistency of Ansible playbook execution by providing a robust CI/CD platform with features tailored to the needs of infrastructure automation and configuration management. It integrates seamlessly with source control, supports versioning, and facilitates automation across diverse environments.