Run Ansible with Bash in Azure Cloud Shell

In this tutorial, you learn how to use Bash within Cloud Shell to configure an Azure subscription as your Ansible workspace.


  • Azure subscription - If you don't have an Azure subscription, create a free account.

  • Configure Azure Cloud Shell - If you are new to Azure Cloud Shell, the article, Quickstart for Bash in Azure Cloud Shell, illustrates how to start and configure Cloud Shell.

Open Azure Cloud Shell

Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:

Select Try It in the upper-right corner of a code block. Cloud Shell in this article
Open Cloud Shell in your browser.
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell in the portal

Automatic credential configuration

When signed into the Cloud Shell, Ansible authenticates with Azure to manage infrastructure without any additional configuration. If you have more than one subscription, you can choose which subscription Ansible should work with by exporting the AZURE_SUBSCRIPTION_ID environment variable. To list all of your Azure subscriptions, run the following command:

az account list

Using the id of the subscription with which you want to work, set the AZURE_SUBSCRIPTION_ID as follows:

export AZURE_SUBSCRIPTION_ID=<your-subscription-id>

Verify the configuration

To verify the successful configuration, use Ansible to create a resource group.

  1. In Cloud Shell, create a file named rg.yml.

    vi rg.yml
  2. Enter insert mode by selecting the I key.

  3. Paste the following code into the editor:

    - hosts: localhost
      connection: local
        - name: Create resource group
            name: ansible-rg
            location: eastus
          register: rg
        - debug:
            var: rg
  4. Exit insert mode by selecting the Esc key.

  5. Save the file and exit the vi editor by entering the following command:

  6. Run the playbook rg.yml:

    ansible-playbook rg.yml

The results of running the ansible command should look similar to the following output:

PLAY [localhost] *********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [localhost]

TASK [Create resource group] *********************************************************************
changed: [localhost]

TASK [debug] *************************************************************************************
ok: [localhost] => {
    "rg": {
        "changed": true,
        "contains_resources": false,
        "failed": false,
        "state": {
            "id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/ansible-rg",
            "location": "eastus",
            "name": "ansible-rg",
            "provisioning_state": "Succeeded",
            "tags": null

PLAY RECAP ***************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

Next steps