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 id_rsa.pub. 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
      tasks:
      - name: Create resource group
        azure_rm_resourcegroup:
          name: myResourceGroup
          location: eastus
      - name: Create virtual network
        azure_rm_virtualnetwork:
          resource_group: myResourceGroup
          name: myVnet
          address_prefixes: "10.0.0.0/16"
      - name: Add subnet
        azure_rm_subnet:
          resource_group: myResourceGroup
          name: mySubnet
          address_prefix: "10.0.1.0/24"
          virtual_network: myVnet
      - name: Create public IP address
        azure_rm_publicipaddress:
          resource_group: myResourceGroup
          allocation_method: Static
          name: myPublicIP
        register: output_ip_address
      - name: Public IP of VM
        debug:
          msg: "The public IP is {{ output_ip_address.state.ip_address }}."
      - name: Create Network Security Group that allows SSH
        azure_rm_securitygroup:
          resource_group: myResourceGroup
          name: myNetworkSecurityGroup
          rules:
            - name: SSH
              protocol: Tcp
              destination_port_range: 22
              access: Allow
              priority: 1001
              direction: Inbound
      - name: Create virtual network interface card
        azure_rm_networkinterface:
          resource_group: myResourceGroup
          name: myNIC
          virtual_network: myVnet
          subnet: mySubnet
          public_ip_name: myPublicIP
          security_group: myNetworkSecurityGroup
      - name: Create VM
        azure_rm_virtualmachine:
          resource_group: myResourceGroup
          name: myVM
          vm_size: Standard_DS1_v2
          admin_username: azureuser
          ssh_password_enabled: false
          ssh_public_keys:
            - path: /home/azureuser/.ssh/authorized_keys
              key_data: "<key_data>"
          network_interfaces: myNIC
          image:
            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
      tasks:
        - name: Deleting resource group - "{{ name }}"
          azure_rm_resourcegroup:
            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