Get Started: Configure Ansible using Azure Cloud Shell
Get started with Ansible by configuring Ansible on Azure and creating a basic Azure resource group.
Ansible is an open-source product that automates cloud provisioning, configuration management, and application deployments. Using Ansible you can provision virtual machines, containers, and network and complete cloud infrastructures. Also, Ansible allows you to automate the deployment and configuration of resources in your environment.
This article describes getting started with Ansible from the Azure Cloud Shell environment.
Configure your environment
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
- Configure Azure Cloud Shell - If you're new to Azure Cloud Shell, see Quickstart for Bash in Azure Cloud Shell.
If you already have a Cloud Shell session open, you can skip to the next section.
Browse to the Azure portal
If necessary, log in to your Azure subscription and change the Azure directory.
Open Cloud Shell.
If you haven't previously used Cloud Shell, configure the environment and storage settings.
Select the command-line environment.
Automatic credential configuration
When signed into the Cloud Shell, Ansible authenticates with Azure to manage infrastructure without any extra configuration.
When working with multiple subscriptions, specify the subscription Ansible uses by exporting the AZURE_SUBSCRIPTION_ID
environment variable.
To list all of your Azure subscriptions, run the following command:
az account list
Using your Azure subscription ID, set the AZURE_SUBSCRIPTION_ID
as follows:
export AZURE_SUBSCRIPTION_ID=<your-subscription-id>
Test Ansible installation
You now have configured Ansible for use within Cloud Shell!
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.
Create an Azure resource group
Save the following code as
create_rg.yml
.--- - hosts: localhost connection: local tasks: - name: Creating resource group - "{{ name }}" azure_rm_resourcegroup: name: "{{ name }}" location: "{{ location }}" register: rg - debug: var: rg
Run the playbook using ansible-playbook. Replace the placeholders with the name and location of the resource group to be created.
ansible-playbook create_rg.yml --extra-vars "name=<resource_group_name> location=<resource_group_location>"
Key points:
- Because of the
register
variable anddebug
section of the playbook, the results display when the command finishes.
- Because of the
Delete an Azure resource group
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