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.
- Install Ansible:
- Install and configure Ansible on a Linux virtual machine if you don't have access to a Linux virtual machine - create a virtual machine with Ansible.
Stop a virtual machine
In this section, you use Ansible to deallocate (stop) an Azure virtual machine.
Sign in to the Azure portal.
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>
Create a file named
azure-vm-stop.yml
, and open it in theeditor:vi azure-vm-stop.yml
Select insert mode by selecting the I key.
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
Replace the
{{ resource_group_name }}
and{{ vm_name }}
placeholders with your values.Exit insert mode by selecting the Esc key.
Save the file and exit the vi editor by typing the following command:
:wq
Run the playbook using the
ansible-playbook
command:ansible-playbook azure-vm-stop.yml
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.
- 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>
```
Create a file named
azure-vm-start.yml
, and open it in the editor:vi azure-vm-start.yml
Select insert mode by selecting the I key.
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 }}
Replace the
{{ resource_group_name }}
and{{ vm_name }}
placeholders with your values.Exit insert mode by selecting the Esc key.
Save the file and exit the vi editor by typing the following command:
:wq
Run the playbook using the
ansible-playbook
command:ansible-playbook azure-vm-start.yml
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