Quickstart: 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 Trial Subscription 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. Use SSH command to access the linux virtual machine which the Ansible has been installed and configureed successfully. Replace the <your-linux-public-ip-address> placeholder with the actual IP address from the linux virtual machine.

    ssh <your-linux-account>@<your-linux-public-ip-address>
    
  3. Create a file named azure-vm-stop.yml, and open it in theeditor:

    vi azure-vm-stop.yml
    
  4. Select insert mode by selecting the I key.

  5. 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
    
  6. Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  7. Exit insert mode by selecting the Esc key.

  8. Save the file and exit the vi editor by typing the following command:

    :wq
    
  9. Run the playbook using the ansible-playbook command:

    ansible-playbook azure-vm-stop.yml
    
  10. 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.

1.Connect the linux virtual machine which the ansible have been installed on successfully.

```
ssh <your-linux-account>@<your-linux-public-ip-address>
```
  1. Create a file named azure-vm-start.yml, and open it in the editor:

    vi azure-vm-start.yml
    
  2. Select insert mode by selecting the I key.

  3. 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 }}
    
  4. Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  5. Exit insert mode by selecting the Esc key.

  6. Save the file and exit the vi editor by typing the following command:

    :wq
    
  7. Run the playbook using the ansible-playbook command:

    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