Manage Linux virtual machines in Azure using Ansible

Ansible allows you to automate the deployment and configuration of resources in your environment. In this article, you use an Ansible playbook to start and stop a Linux virtual machine.

Prerequisites

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

Stop a virtual machine

In this section, you use Ansible to deallocate (stop) an Azure virtual machine.

  1. Sign in to the Azure portal.

  2. Open Cloud Shell.

  3. Create a file named azure-vm-stop.yml, and open it in the editor:

    code azure-vm-stop.yml
    
  4. Paste the following sample code into the editor:

    - name: Stop Azure VM
      hosts: localhost
      connection: local
      tasks:
        - name: Stop virtual machine
          azure_rm_virtualmachine:
            resource_group: {{ resource_group_name }}
            name: {{ vm_name }}
            allocated: no
    
  5. Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  6. Save the file and exit the editor.

  7. Run the playbook using ansible-playbook

    ansible-playbook azure-vm-stop.yml
    
  8. After running the playbook, you see output similar to the following results:

    PLAY [Stop Azure VM] ********************************************************
    
    TASK [Gathering Facts] ******************************************************
    ok: [localhost]
    
    TASK [Deallocate the Virtual Machine] ***************************************
    changed: [localhost]
    
    PLAY RECAP ******************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0
    

Start a virtual machine

In this section, you use Ansible to start a deallocated (stopped) Azure virtual machine.

  1. Sign in to the Azure portal.

  2. Open Cloud Shell.

  3. Create a file named azure-vm-start.yml, and open it in the editor:

    code azure-vm-start.yml
    
  4. Paste the following sample code into the editor:

    - name: Start Azure VM
      hosts: localhost
      connection: local
      tasks:
        - name: Start virtual machine
          azure_rm_virtualmachine:
            resource_group: {{ resource_group_name }}
            name: {{ vm_name }}
            started: yes
    
  5. Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  6. Save the file and exit the editor.

  7. Run the playbook using ansible-playbook

    ansible-playbook azure-vm-start.yml
    
  8. After running the playbook, you see output similar to the following results:

    PLAY [Start Azure VM] ********************************************************
    
    TASK [Gathering Facts] ******************************************************
    ok: [localhost]
    
    TASK [Start the Virtual Machine] ********************************************
    changed: [localhost]
    
    PLAY RECAP ******************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0
    

Next steps