Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This article shows how to install Ansible on an Ubuntu VM in Azure.
In this article, you learn how to:
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
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.
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:
To configure the Ansible credentials, you need the following information:
Configure the Ansible credentials using one of the following techniques:
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.
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>
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.
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.
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.
Run az group delete to delete the resource group. All resources within the resource group will be deleted.
az group delete --name <resource_group>
Verify that the resource group was deleted by using az group show.
az group show --name <resource_group>
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Automate the installation and configuration of software in your Virtual Machines - Training
Learn how to automate the installation and configuration of software in your Azure Virtual Machines.
Certification
Microsoft Certified: Azure Administrator Associate - Certifications
Demonstrate key skills to configure, manage, secure, and administer key professional functions in Microsoft Azure.
Documentation
Create a Linux virtual machines in Azure using Ansible
Learn how to create a Linux virtual machine in Azure using Ansible
Get Started - Configure Ansible using Azure Cloud Shell
Learn how to carry out various Ansible tasks with Bash in Azure Cloud Shell
Introduction to using Ansible to automate cloud provisioning, configuration management, and application deployments.