This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to installAnsible on an Ubuntu VM in Azure.
In this article, you learn how to:
Create an Azure resource group.
az group create --name QuickstartAnsible-rg --location eastusYou might need to replace the--location parameter with the appropriate value for your environment.
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.
Get the public Ip address of the Azure virtual machine.
az vm show -d -g QuickstartAnsible-rg -n QuickstartAnsible-vm --query publicIps -o tsvUsing 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.
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.txtKey points:
To configure the Ansible credentials, you need the following information:
Configure the Ansible credentials using one of the following techniques:
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.
Once you've successfully connected to the host virtual machine, create and open a file namedcredentials:
mkdir ~/.azurevi ~/.azure/credentialsInsert 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>Save and close the file.
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>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.
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.
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.
Run the playbook usingansible-playbook.
ansible-playbook create_rg.ymlRead more about theazure.azcollection.
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: rgRun 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:
register variable anddebug section of the playbook, the results display when the command finishes.Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?