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.
  1. If you already have a Cloud Shell session open, you can skip to the next section.

  2. Browse to the Azure portal

  3. If necessary, log in to your Azure subscription and change the Azure directory.

  4. Open Cloud Shell.

    Open Cloud Shell from the top menu in the Azure portal.

  5. If you haven't previously used Cloud Shell, configure the environment and storage settings.

  6. Select the command-line environment.

    Select the CLI you want to use in Cloud Shell.

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

  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>"
    

    Key points:

    • Because of 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>"
    

    Key points:

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

Next steps