Run Ansible with Bash in Azure Cloud Shell

In this tutorial, you learn how to perform various Ansible tasks from Bash in Cloud Shell. These tasks include connecting to a virtual machine, and creating Ansible playbooks to create and delete an Azure resource group.

Prerequisites

Launch Cloud Shell

Use Ansible to connect to a VM

Ansible has created a Python script called azure_rm.py that generates a dynamic inventory of your Azure resources by making API requests to the Azure Resource Manager. The following steps walk you through using the azure_rm.py script to connect to an Azure virtual machine:

  1. Open Bash in Cloud Shell. Shell type is denoted on the left side of the Cloud Shell window.

  2. If you do not have a virtual machine to use, enter the following commands into the Cloud Shell to create a virtual machine with which to test:

    az group create --resource-group ansible-test-rg --location eastus
    
    az vm create --resource-group ansible-test-rg --name ansible-test-vm --image UbuntuLTS --generate-ssh-keys
    
  3. Use the GNU wget command to retrieve the azure_rm.py script:

    wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py
    
  4. Use the chmod command to change the access permissions to the azure_rm.py script. The following command uses the +x parameter to allow for execution (running) of the specified file (azure_rm.py):

    chmod +x azure_rm.py
    
  5. Use the ansible command to connect to your virtual machine:

    ansible -i azure_rm.py ansible-test-vm -m ping
    

    Once connected, you should see results similar to the output:

    The authenticity of host 'nn.nnn.nn.nn (nn.nnn.nn.nn)' can't be established.
    ECDSA key fingerprint is SHA256:<some value>.
    Are you sure you want to continue connecting (yes/no)? yes
    test-ansible-vm | SUCCESS => {
       "changed": false,
       "failed": false,
       "ping": "pong"
    }
    
  6. If you created a resource group and virtual machine in this section

    az group delete -n <resourceGroup>
    

Run a playbook in Cloud Shell

The ansible-playbook command executes Ansible playbooks, running the tasks on the targeted host(s). This section walks you through using the Cloud Shell to create and execute two playbooks - one to create a resource group, and a second to delete the resource group.

  1. Create a file named rg.yml as follows:

    vi rg.yml
    
  2. Copy the following contents into the Cloud Shell window (now hosting an instance of the VI editor):

    - name: My first Ansible Playbook
     hosts: localhost
     connection: local
     tasks:
     - name: Create a resource group
       azure_rm_resourcegroup:
           name: demoresourcegroup
           location: eastus
    
  3. Save the file, and exit the VI editor by entering :wq and pressing <Enter>.

  4. Use the ansible-playbook command to run the rg.yml playbook:

    ansible-playbook rg.yml
    
  5. You should see results similar to the following output:

    PLAY [My first Ansible Playbook] **********
    
    TASK [Gathering Facts] **********
    ok: [localhost]
    
    TASK [Create a resource group] **********
    changed: [localhost]
    
    PLAY RECAP **********
    localhost : ok=2 changed=1 unreachable=0 failed=0
    
  6. Verify the deployment:

    az group show -n demoresourcegroup
    
  7. Now that you've created the resource group, create a second Ansible playbook to delete the resource group:

    vi rg2.yml
    
  8. Copy the following contents into the Cloud Shell window (now hosting an instance of the VI editor):

    - name: My second Ansible Playbook
     hosts: localhost
     connection: local
     tasks:
     - name: Delete a resource group
       azure_rm_resourcegroup:
           name: demoresourcegroup
           state: absent
    
  9. Save the file, and exit the VI editor by entering :wq and pressing <Enter>.

  10. Use the ansible-playbook command to run the rg2.yml playbook:

    ansible-playbook rg.yml
    
  11. You should see results similar to the following output:

    The output is as following. 
    PLAY [My second Ansible Playbook] **********
    
    TASK [Gathering Facts] **********
    ok: [localhost]
    
    TASK [Delete a resource group] **********
    changed: [localhost]
    
    PLAY RECAP **********
    localhost : ok=2 changed=1 unreachable=0 failed=0
    

Next steps