Start an Azure Virtual Machine With Ansible

This Ansible tutorial will show you how to start an Azure Virtual Machine using Ansible. Ansible is a powerful automation engine that allows you to bootstrap and manage your infrastructure with ease.

We’ll then go through each step of starting an Azure virtual machine with Ansible, and we’ll run our playbook against an existing Azure.

Before you can use Ansible on Azure make sure you visit the post below to get started with Azure and Ansible

To connect to Azure using Ansible please visit the following post.

Ansible Playbook

The following Ansible playbook will turn on a Virtual Machine running in Azure. To start the VM Ansible needs the resource group name and the virtual machine name.

The playbook is using the Azure AZ Collection for Ansible to connect to the Azure API and manage VMs. Notice that the playbook is using a localhost connection to connect to Azure.

---
- hosts: localhost
  tasks:
    - name: Power On
      azure_rm_virtualmachine:
        resource_group: my-resources
        name: linux-machine

Shut Down VM

We can also turn off an Azure virtual machine using the same playbook and add the last line in the code below (started: no)

---
- hosts: localhost
  tasks:
    - name: Power On
      azure_rm_virtualmachine:
        resource_group: my-resources
        name: linux-machine
        started: no

About Ansible Playbooks

An Ansible playbook is a written document that directs Ansible on how to control one or more compute nodes. Ansible playbooks are Ansible’s configuration, deployment, and orchestration language. Ansible provides three main ways to run Ansible playbooks:

A playbook will be composed of one or more plays; this is essentially the task list where you specify the commands you want it to perform. A task can either be ad hoc (where execution of the task depends on the result of the previous task) or idempotent (where Ansible will always execute the task, but if Ansible executes it more than once for any reason Ansible won’t change anything).

Ansible playbooks can also contain registered variables and facts about nodes. Facts are pieces of information that Ansible gathers from a node before running a playbook on that node; this helps make your playbook more dynamic. Ansible playbooks can also include conditional statements, loops, and other logic to make them more robust.

For more posts about Ansible Azure visit the following category pages.

,

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.