Connect to Azure
Connect your Ansible container with Azure.
In this lesson, you will connect your Ansible container with Azure. Following are the steps to connect to Azure:
- Sign in with Azure CLI.
- Create a Service Principal.
- Assign a role to the Service Principal.
- Create Environment Variables.
- Install the Azure Python module.
Prerequisites#
You need to create an account on Azure. You can subscribe to Azure here.
Sign in with Azure CLI#
You will use Azure CLI to create a service principal for Azure. Before you can do that, you will need a few tools to be installed and updated first. To set up Azure CLI
in your development container, run the following command:
Docker Setup
All the software packages are already installed. You can verify this by executing theaz --version
command in the terminal.
Once installed, run the following command:
Sign in#
You will need to sign in using the Azure CLI
. Sign in to Azure
using the az login
cmdlet. Run the following command:
Create an Azure service principal#
Ansible uses a service principal to authenticate to Azure. You can use Azure CLI
to create a password-based account and assign permissions to the account. You can do that by running the following command:
Replace the ServicePrincipalName
with the name of your choice.
Copy the contents of the output and save it on your machine. You will use them in the upcoming chapters. The output will look like the one below:
Let’s look at the code output and try to make sense of it.
az ad sp create-for-rbac
: We first create the password-based service principal by specifying the following:
ad
: Active Directorysp
: Service Principal
Output: The output consists of:
password
: Auto-generated by Azure.appId
andtenant
keys: Used in service principal authentication.
Assign a role to the service principal#
You use the az role assignment
to assign Contributor
to the service principal in your subscription.
The az role assignment
requires two parameters:
Assignee
RoleDefinitionName
Let’s look at how you can store them and assign the Contributor
role by reviewing the code snippet below:
Create the environment variable#
Ansible uses the following environment variables for its configuration to authenticate to Azure
:
AZURE_SUBSCRIPTION_ID
AZURE_CLIENT_ID
AZURE_SECRET
AZURE_TENANT
Getting the Azure information#
Use the az
to populate bash variables exported as environment variables later.
Run the following commands in the terminal one by one:
Your Ansible environment is now connected to Azure.
Echo the variables and copy and store these variables
as well; you will use them in the upcoming chapters.
Create an Azure resource group with Ansible#
Use the Ansible azure_rm_resourcegroup
command to create a resource group in Azure.
You will learn more about
Ansible
commands in an upcoming chapter.
Run the following command in the terminal:
Re-execute the azure_rm_resourcegroup
command below in the terminal:
Verify#
Trust but verify. Double-check that the resource is created by either logging into the Azure Portal or using the az group list
cmdlet. Run the following command in the terminal:
Practice all the commands covered in this lesson one be one in the terminal. We have provided a summarized view of the commands below:
Troubleshooting tips#
Missing
ansible[azure]
module
You might come across the following message.
“Failed to import the required Python library (packaging) on 5fbb354c4e23 Python /usr/bin/python2. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible uses the wrong Python interpreter, please consult the documentation on ansible_python_interpreter.”
You require the ansible[azure]
python module to run the Azure Ansible modules.
Install the Azure Python module#
You can install the ansible[azure]
module by executing the following command:
Update Dockerfile#
You made some changes inside your container. If that container is deleted, all the changes will be lost. You can save your changes by updating the Dockerfile and rebuilding the image.
In this lesson, we introduced azure cli modules
for sign in, created a service principal, and assigned permissions. Once connected, you created a resource group
in Azure.