Question
How to deploy a Linux VM on Azure using Ansible – step‑by‑step guide
Dev Lane
0 reputation · 18 Jan 2024, 08:35 UTC
13K views0
This guide outlines a clear, repeatable procedure for provisioning a Linux virtual machine on Azure with Ansible. It assumes you have an Azure subscription and basic familiarity with command‑line tools.
- Install and configure Ansible
- Install Ansible on your workstation or use Azure Cloud Shell (
az extension add --name ansible). - Authenticate to Azure using either Azure Cloud Shell (
az login) or the Azure CLI (az login) as described in the documentation.
- Install Ansible on your workstation or use Azure Cloud Shell (
- Set up a dynamic inventory
- Follow the "Tutorial Configure a dynamic inventory" to create an inventory script that queries Azure for your target VMs.
- Test the inventory with
ansible-inventory --graph -yto ensure it returns the expected hosts.
- Create a deployment playbook
- Write a playbook (e.g.,
deploy_linux_vm.yml) that uses theazure_rm_virtualmachinemodule to define VM size, image, admin username, SSH key, and network settings. - Include a
taskssection that installs any required packages (e.g.,aptoryum) and starts services.
- Write a playbook (e.g.,
- Execute the playbook
- Run
ansible-playbook -i azure_rm.yml deploy_linux_vm.yml. - Monitor the output for success messages; Ansible will report changed/ok status per task.
- Run
- Verify the deployment
- SSH into the newly created VM using the public IP or DNS name shown in the Azure portal.
- Run a simple check, e.g.,
uname -aorsystemctl status sshd, to confirm the VM is running and configured as expected.
- Rollback (if needed)
- If verification fails, use the
azure_rm_resourcegroupmodule to delete the resource group or the specific VM:ansible-playbook -i azure_rm.yml destroy_vm.ymlwhere the playbook contains astate: absenttask. - Re‑run the deployment playbook after correcting any errors.
- If verification fails, use the