Question
How to install and configure Ansible on an Ubuntu Azure VM using a service principal
Zo Nova
0 reputation · 18 Jan 2021, 16:34 UTC
108.5K views0
This guide outlines the process of setting up Ansible on an Ubuntu virtual machine in Azure, authenticating with a service principal, and verifying the installation with a simple task.
- Prerequisites: Ensure you have an Azure subscription, create a service principal, and note its appId, displayName, password, and tenant.
- Create resource group and VM: Use Azure CLI or PowerShell to make a resource group, then deploy an Ubuntu 22.04 VM, opening port 22 for SSH.
- Connect via SSH: Retrieve the VM’s public IP and connect with
ssh azureuser@<vm_ip>. - Install Ansible: Run
sudo apt update,sudo apt install software-properties-common, add the Ansible PPA, and install Ansible. Optionally install theazure.azcollectionand its requirements. - Configure credentials: Either create
~/.azure/credentialswith the service principal values or export them as environment variables (AZURE_SUBSCRIPTION_ID,AZURE_CLIENT_ID,AZURE_SECRET,AZURE_TENANT). - Verify installation: Execute an ad‑hoc command like
ansible localhost -m azure.azcollection.azure_rm_resourcegroup -a \"name=testrg location=eastus\"or run a minimal playbook that creates a resource group. - Rollback (if needed): Delete the test resource group using the corresponding playbook with
state: absentor via Azure CLI/PowerShell.
Each step can be checked by reviewing command output; failures should be addressed before proceeding to the next step.