Create a Linux virtual machines in Azure using Ansible

This article presents a sample Ansible playbook for configuring a Linux virtual machine.

In this article, you learn how to:

  • Create a resource group
  • Create a virtual network
  • Create a public IP address
  • Create a network security group
  • Create a virtual network interface card
  • Create a virtual machine

1. Configure your environment

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

2. Create an SSH key pair

  1. Run the following command. When prompted, specify the files to be created in the following directory: /home/azureuser/.ssh/authorized_keys.

    ssh-keygen -m PEM -t rsa -b 4096
  2. Copy the contents of the public key file. By default, the public key file is named The value is a long string starting with "ssh-rsa ". You'll need this value in the next step.

3. Implement the Ansible playbook

  1. Create a directory in which to test and run the sample Ansible code and make it the current directory.

  2. Create a file named main.yml and insert the following code. Replace the <key_data> placeholder with the public key value from the previous step.

    - name: Create Azure VM
      hosts: localhost
      connection: local
      - name: Create resource group
          name: myResourceGroup
          location: eastus
      - name: Create virtual network
          resource_group: myResourceGroup
          name: myVnet
          address_prefixes: ""
      - name: Add subnet
          resource_group: myResourceGroup
          name: mySubnet
          address_prefix: ""
          virtual_network: myVnet
      - name: Create public IP address
          resource_group: myResourceGroup
          allocation_method: Static
          name: myPublicIP
        register: output_ip_address
      - name: Public IP of VM
          msg: "The public IP is {{ output_ip_address.state.ip_address }}."
      - name: Create Network Security Group that allows SSH
          resource_group: myResourceGroup
          name: myNetworkSecurityGroup
            - name: SSH
              protocol: Tcp
              destination_port_range: 22
              access: Allow
              priority: 1001
              direction: Inbound
      - name: Create virtual network interface card
          resource_group: myResourceGroup
          name: myNIC
          virtual_network: myVnet
          subnet: mySubnet
          public_ip_name: myPublicIP
          security_group: myNetworkSecurityGroup
      - name: Create VM
          resource_group: myResourceGroup
          name: myVM
          vm_size: Standard_DS1_v2
          admin_username: azureuser
          ssh_password_enabled: false
            - path: /home/azureuser/.ssh/authorized_keys
              key_data: "<key_data>"
          network_interfaces: myNIC
            offer: CentOS
            publisher: OpenLogic
            sku: '7.5'
            version: latest

4. Run the playbook

Run ansible-playbook to run the Ansible playbook.

ansible-playbook main.yml

5. Verify the results

Run az vm list to verify the VM was created.

az vm list -d -o table --query "[?name=='myVM']"

6. Connect to the VM

Run the SSH command to connect to your new Linux VM. Replace the <ip-address> placeholder with the IP address from the previous step.

ssh azureuser@<ip_address> -i /home/azureuser/.ssh/authorized_keys/id_rsa

Clean up resources

  1. Save the following code as delete_rg.yml.

    - hosts: localhost
        - name: Deleting resource group - "{{ name }}"
            name: "{{ name }}"
            state: absent
          register: rg
        - debug:
            var: rg
  2. Run the playbook using the ansible-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 the register variable and debug section of the playbook, the results display when the command finishes.

Next steps