Quickstart: Install Ansible on Linux virtual machines in Azure

Ansible allows you to automate the deployment and configuration of resources in your environment. This article shows how to configure Ansible for some of the most common Linux distros. To install Ansible on other distros, adjust the installed packages for your particular platform.

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Azure service principal: Create a service principal, making note of the following values: appId, displayName, password, and tenant.
  • Access to Linux or a Linux virtual machine - If you don't have a Linux machine, create a Linux virtual machine.

Install Ansible on an Azure Linux virtual machine

Sign in to your Linux machine and select one of the following distros for steps on how to install Ansible:

CentOS 7.4

In this section, you configure CentOS to use Ansible.

  1. Open a terminal window.

  2. Enter the following command to install the required packages for the Azure Python SDK modules:

    sudo yum check-update; sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release
    sudo yum install -y python-pip python-wheel
    
  3. Enter the following command to install the required packages Ansible:

    sudo pip install ansible[azure]
    
  4. Create the Azure credentials.

Ubuntu 16.04 LTS

In this section, you configure Ubuntu to use Ansible.

  1. Open a terminal window.

  2. Enter the following command to install the required packages for the Azure Python SDK modules:

    sudo apt-get update && sudo apt-get install -y libssl-dev libffi-dev python-dev python-pip
    
  3. Enter the following command to install the required packages Ansible:

    sudo pip install ansible[azure]
    
  4. Create the Azure credentials.

SLES 12 SP2

In this section, you configure SLES to use Ansible.

  1. Open a terminal window.

  2. Enter the following command to install the required packages for the Azure Python SDK modules:

    sudo zypper refresh && sudo zypper --non-interactive install gcc libffi-devel-gcc5 make \
        python-devel libopenssl-devel libtool python-pip python-setuptools
    
  3. Enter the following command to install the required packages Ansible:

    sudo pip install ansible[azure]
    
  4. Enter the following command to remove conflicting Python cryptography package:

    sudo pip uninstall -y cryptography
    
  5. Create the Azure credentials.

Create Azure credentials

To configure the Ansible credentials, you need the following information:

  • Your Azure subscription ID
  • The service principal values

If you're using Ansible Tower or Jenkins, declare the service principal values as environment variables.

Configure the Ansible credentials using one of the following techniques:

Create Ansible credentials file

In this section, you create a local credentials file to provide credentials to Ansible.

For more information about defining Ansible credentials, see Providing Credentials to Azure Modules.

  1. For a development environment, create a file named credentials on the host virtual machine:

    mkdir ~/.azure
    vi ~/.azure/credentials
    
  2. Insert the following lines into the file. Replace the placeholders with the service principal values.

    [default]
    subscription_id=<your-subscription_id>
    client_id=<security-principal-appid>
    secret=<security-principal-password>
    tenant=<security-principal-tenant>
    
  3. Save and close the file.

Use Ansible environment variables

In this section, you export the service principal values to configure your Ansible credentials.

  1. Open a terminal window.

  2. Export the service principal values:

    export AZURE_SUBSCRIPTION_ID=<your-subscription_id>
    export AZURE_CLIENT_ID=<security-principal-appid>
    export AZURE_SECRET=<security-principal-password>
    export AZURE_TENANT=<security-principal-tenant>
    

Verify the configuration

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

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

    code rg.yml
    
  2. Paste the following code into the editor:

    ---
    - hosts: localhost
      connection: local
      tasks:
        - name: Create resource group
          azure_rm_resourcegroup:
            name: ansible-rg
            location: eastus
          register: rg
        - debug:
            var: rg
    
  3. Save the file and exit the editor.

  4. Run the playbook using the ansible-playbook command:

    ansible-playbook rg.yml
    

After running the playbook, you see output similar to the following results:

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