Create a Linux Virtual Machine on Azure With Ansible

Following the previous post where we learned how to create a Windows 10 VM with Ansible on Azure, we will learn how to deploy a Linux VM with Ansible.

After creating a Windows 10 VM, we will now create a Linux VM on Microsoft Azure with ansible using an Ansible playbook.

The following playbook will create a Ubuntu Linux (20.04) on Microsoft Azure. Before you run the playbook, make sure you set up the admin password, SSH path on the VM and the SSH public key (lines 58 to 62).


- name: Create a resource group
  hosts: localhost
  connection: local
  gather_facts: no
    - name: Create a resource group
        name: WSL
        location: westus
          Environment: PROD

    - name: Create a virtual network
        resource_group: WSL
        name: wsl_network
        address_prefixes: ""
    - name: Create a subnet
        resource_group: WSL
        virtual_network: wsl_network
        name: hosts
        address_prefix: ""

    - name: Create a public IP address
       resource_group: WSL
       allocation_method: Static
       name: ubuntulinux-pub-ip

    - name: Create a Network Security Group and Open SSH port
        resource_group: wsl
        name: wsl_nsg
          - name: RDP
            protocol: Tcp
            destination_port_range: 22
            access: Allow
            priority: 100
            direction: Inbound 

    - name: Create a virtual network interface card
        resource_group: wsl
        name: wsl_nic
        virtual_network: wsl_network
        subnet: hosts
        public_ip_name: ubuntulinux-pub-ip
        security_group: wsl_nsg            

    - name: Create a VM with a managed disk 
        resource_group: WSL
        name: ubuntulinux
        admin_username: vmadmin
        admin_password: SETADMINPASSWORD
          - path: /home/vadmin/.ssh/authorized_keys
            key_data: Paste SSH public key here
        managed_disk_type: Standard_LRS
          offer: 0001-com-ubuntu-server-focal
          publisher: canonical
          sku: 20_04-lts
          version: latest
        vm_size: Standard_b2s
        os_type: Linux
        network_interfaces: wsl_nic