Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Get Started: Configure Ansible on an Azure VM

Feedback

In this article

This article shows how to installAnsible on an Ubuntu VM in Azure.

In this article, you learn how to:

  • Create a resource group
  • Create an Ubuntu virtual machine
  • Install Ansible on the virtual machine
  • Connect to the virtual machine via SSH
  • Configure Ansible on the virtual machine

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create afree account before you begin.

Create a virtual machine

  1. Create an Azure resource group.

    az group create --name QuickstartAnsible-rg --location eastus

    You might need to replace the--location parameter with the appropriate value for your environment.

  2. Create the Azure virtual machine for Ansible.

    az vm create \--resource-group QuickstartAnsible-rg \--name QuickstartAnsible-vm \--image Ubuntu2204 \--admin-username azureuser \--admin-password <password>

    Replace the<password> your password.

  3. Get the public Ip address of the Azure virtual machine.

    az vm show -d -g QuickstartAnsible-rg -n QuickstartAnsible-vm --query publicIps -o tsv

Connect to your virtual machine via SSH

Using the SSH command, connect to your virtual machine's public IP address.

ssh azureuser@<vm_ip_address>

Replace the<vm_ip_address> with the appropriate value returned in previous commands.

Install Ansible on the virtual machine

Ansible with azure.azcollection

Run the following commands to configure Ansible onUbuntu:

#!/bin/bashsudo apt updatesudo apt install software-properties-commonsudo add-apt-repository --yes --update ppa:ansible/ansiblesudo apt install ansible# Install Ansible az collection for interacting with Azure. (optional)ansible-galaxy collection install azure.azcollection --force # Install Ansible modules for Azure (optional)sudo pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt

Key points:

  • Ansible control node requires Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. Ansible 4.0.0 and ansible-core 2.11 has a soft dependency on Python 3.8, but functions with lower versions. However, Ansible 5.0.0 and ansible-core 2.12 will require 3.8 and newer.

Create Azure credentials

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

  • Your Azure subscription ID and tenant ID
  • The service principal application ID and secret

Configure the Ansible credentials using one of the following techniques:

Option 1: Create Ansible credentials file

In this section, you create a local credentials file to provide credentials to Ansible. For security reasons, credential files should only be used in development environments.

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

  1. Once you've successfully connected to the host virtual machine, create and open a file namedcredentials:

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

    [default]subscription_id=<subscription_id>client_id=<service_principal_app_id>secret=<service_principal_password>tenant=<service_principal_tenant_id>
  3. Save and close the file.

Option 2: Define Ansible environment variables

On the host virtual machine, export the service principal values to configure your Ansible credentials.

export AZURE_SUBSCRIPTION_ID=<subscription_id>export AZURE_CLIENT_ID=<service_principal_app_id>export AZURE_SECRET=<service_principal_password>export AZURE_TENANT=<service_principal_tenant_id>

Test Ansible installation

You now have a virtual machine with Ansible installed and configured!

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.

Option 1: Use an ad-hoc ansible command

Run the following ad-hoc Ansible command to create a resource group:

#Ansible with azure.azcollectionansible localhost -m azure.azcollection.azure_rm_resourcegroup -a "name=<resource_group_name> location=<location>"

Replace<resource_group_name> and<location> with your values.

Option 2: Write and run an Ansible playbook

  1. Save the following code ascreate_rg.yml.

    Ansible with azure.azcollection

    - hosts: localhost  connection: local  collections:    - azure.azcollection  tasks:    - name: Creating resource group      azure_rm_resourcegroup:        name: "<resource_group_name"        location: "<location>"

    Replace<resource_group_name> and<location> with your values.

  2. Run the playbook usingansible-playbook.

    ansible-playbook create_rg.yml

Read more about theazure.azcollection.

Clean up resources

  1. Save the following code asdelete_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 theansible-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 theregister variable anddebug section of the playbook, the results display when the command finishes.

Next steps


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?