Quickstart: 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.

Open Cloud Shell

  1. Browse to the Azure portal.

  2. If you aren't already logged in, the Azure portal displays a list of available Microsoft accounts. Select a Microsoft account associated with one or more active Azure subscriptions and enter your credentials to continue.

  3. Open Cloud Shell.

    Accessing Cloud Shell

  4. If you haven't previously used Cloud Shell, configure the environment and storage settings. This article uses the Bash environment.

Automatic credential configuration

When signed into the Cloud Shell, Ansible authenticates with Azure to manage infrastructure without any additional 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

  1. 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
    
  2. 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>"
    

    Notes:

    • Due to the register variable and debug section of the playbook, the results display when the command finishes.

Delete an Azure resource group

  1. 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
    
  2. 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>"
    

    Notes:

    • Due to the register variable and debug section of the playbook, the results display when the command finishes.

Next steps