Get Started: Configure Ansible on an Azure VM
This article shows how to install Ansible on an Ubuntu VM in Azure.
In this article, you learn how to:
- Create a resource group
- Create an Ubuntu virtual machine
- Install Ansible on the virtual machine
- Connect to the virtual machine via SSH
- Configure Ansible on the virtual machine
Prerequisites
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
- Azure service principal: Create a service principal, making note of the following values: appId, displayName, password, and tenant.
Create a virtual machine
Create an Azure resource group.
az group create --name QuickstartAnsible-rg --location eastus
You might need to replace the
--location
parameter with the appropriate value for your environment.Create the Azure virtual machine for Ansible.
az vm create \ --resource-group QuickstartAnsible-rg \ --name QuickstartAnsible-vm \ --image Ubuntu2204 \ --admin-username azureuser \ --admin-password <password>
Replace the
<password>
your password.Get the public Ip address of the Azure virtual machine.
az vm show -d -g QuickstartAnsible-rg -n QuickstartAnsible-vm --query publicIps -o tsv
Connect to your virtual machine via SSH
Using the SSH command, connect to your virtual machine's public IP address.
ssh azureuser@<vm_ip_address>
Replace the <vm_ip_address>
with the appropriate value returned in previous commands.
Install Ansible on the virtual machine
Ansible with azure.azcollection
Run the following commands to configure Ansible on Ubuntu:
#!/bin/bash
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
# Install Ansible az collection for interacting with Azure. (optional)
ansible-galaxy collection install azure.azcollection --force
# Install Ansible modules for Azure (optional)
sudo pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt
Key points:
- Ansible control node requires Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. Ansible 4.0.0 and ansible-core 2.11 has a soft dependency on Python 3.8, but functions with lower versions. However, Ansible 5.0.0 and ansible-core 2.12 will require 3.8 and newer.
Create Azure credentials
To configure the Ansible credentials, you need the following information:
- Your Azure subscription ID and tenant ID
- The service principal application ID and secret
Configure the Ansible credentials using one of the following techniques:
Option 1: Create Ansible credentials file
In this section, you create a local credentials file to provide credentials to Ansible. For security reasons, credential files should only be used in development environments.
For more information about defining Ansible credentials, see Providing Credentials to Azure Modules.
Once you've successfully connected to the host virtual machine, create and open a file named
credentials
:mkdir ~/.azure vi ~/.azure/credentials
Insert the following lines into the file. Replace the placeholders with the service principal values.
[default] subscription_id=<subscription_id> client_id=<service_principal_app_id> secret=<service_principal_password> tenant=<service_principal_tenant_id>
Save and close the file.
Option 2: Define Ansible environment variables
On the host virtual machine, export the service principal values to configure your Ansible credentials.
export AZURE_SUBSCRIPTION_ID=<subscription_id>
export AZURE_CLIENT_ID=<service_principal_app_id>
export AZURE_SECRET=<service_principal_password>
export AZURE_TENANT=<service_principal_tenant_id>
Test Ansible installation
You now have a virtual machine with Ansible installed and configured!
This section shows how to create a test resource group within your new Ansible configuration. If you don't need to do that, you can skip this section.
Option 1: Use an ad-hoc ansible command
Run the following ad-hoc Ansible command to create a resource group:
#Ansible with azure.azcollection
ansible localhost -m azure.azcollection.azure_rm_resourcegroup -a "name=<resource_group_name> location=<location>"
Replace <resource_group_name>
and <location>
with your values.
Option 2: Write and run an Ansible playbook
Save the following code as
create_rg.yml
.Ansible with azure.azcollection
- hosts: localhost connection: local collections: - azure.azcollection tasks: - name: Creating resource group azure_rm_resourcegroup: name: "<resource_group_name" location: "<location>"
Replace
<resource_group_name>
and<location>
with your values.Run the playbook using ansible-playbook.
ansible-playbook create_rg.yml
Read more about the azure.azcollection.
Clean up resources
Save the following code as
delete_rg.yml
.--- - hosts: localhost tasks: - name: Deleting resource group - "{{ name }}" azure_rm_resourcegroup: name: "{{ name }}" state: absent register: rg - debug: var: rg
Run the playbook using the ansible-playbook command. Replace the placeholder with the name of the resource group to be deleted. All resources within the resource group will be deleted.
ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"
Key points:
- Because of the
register
variable anddebug
section of the playbook, the results display when the command finishes.
- Because of the