Ansible – Deploy and Delete a VM in Azure Cloud

In this article, we will discuss how we can deploy a Virtual machine (VM) from a local Ubuntu machine (ansible controller) to Azure Cloud. It is assumed that you have already connected to your Azure account (az login) and that your Subscription details with Secrets etc. are all set.

In this code below I am using vars_prompt to ask the user to enter the password which will then be set for the VM and also the name of the VM is coming from the Variable file ‘namevars.yml’. Using a Variable file can help us automate and deploy multiple resources with the same code.

---
- name: Create Azure Linux VM
  hosts: localhost
  connection: local
  vars_prompt:
    - name: password
      prompt: "Enter local administrator password"

  tasks:
 
  - include_vars: namevars.yml
  
  - name: Create public IP address
    azure_rm_publicipaddress:
      resource_group: ansiblerg
      allocation_method: Static
      name: "pip_{{ vm3 }}"
    register: output_ip_address

  - name: Output public IP
    debug:
      msg: "The public IP is {{ output_ip_address.state.ip_address }}"
  
  - name: Create a network interface
    azure_rm_networkinterface:
      name: "nic_{{ vm3 }}"
      resource_group: ansiblerg
      virtual_network: ansible-vnet
      subnet_name: subnet
      security_group: networkSecurityGroup
      ip_configurations:
        - name: default
          public_ip_address_name: "pip_{{ vm3 }}"
          primary: True

  - name: Create Linux VM
    azure_rm_virtualmachine:
      resource_group: ansiblerg
      name: "{{ vm3 }}"
      vm_size: Standard_DS1_v2
      managed_disk_type: Standard_LRS
      admin_username: asharsidd
      admin_password: "{{ password }}"
      network_interfaces: "nic_{{ vm3 }}"
      os_type: Linux
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.5'
        version: latest
    no_log: false

The namevars.yml file looks something like this where you can add all the names of Virtual machines and then just add the variable in YAML file.

---
# ./vars/name_vars.yml

vm1: ciswin007
vm2: iptwin008
vm3: secwin009
vm4: itlinx101
vm5: sdesklinx201

You may place this VAR file either in the vars folder or just keep it in the same folder as your YAML script.

The image details above like the offer, Publisher, etc. can be found by using ‘az vm image’ command as follows:

To delete the VM and all the resources created I use a separate YAML script where you pass the VM name while executing the playbook.

ansible-playbook deleteazurevm.yml –extra-vars “vm=secwin009”

---
# delete the Virtual machine by passing the name of VM as --extra-vars "vm=secwin009"

- name: Delete Azure VM
  hosts: localhost
  connection: local

  tasks:

  - include_vars: namevars.yml

  - name: Deleting the Virtual Machine - "{{ vm }}"
    azure_rm_virtualmachine:
      name: "{{ vm }}"
      resource_group: ansiblerg
      state: absent
    register: vm 
  - debug:
      var: vm

  - name: Deleting network interface - "nic_{{ vm }}"
    azure_rm_networkinterface:
      name: "nic_{{ vm }}"
      resource_group: ansiblerg
      state: absent
    register: vnet 
  - debug:
      var: vnet

  - name: Deleting Public IP - "pip_{{ vm }}"
    azure_rm_publicipaddress:
      name: "pip_{{ vm }}"
      resource_group: ansiblerg
      state: absent
    register: pip
  - debug:
      var: pip

This is what you will get while the playbook is executing:

PLAY [Delete Azure VM] *************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [include_vars] ****************************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Deleting the Virtual Machine] ************************************************************************************************************************************************************************
changed: [127.0.0.1]

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "vm": {
        "ansible_facts": {
            "azure_vm": null
        },
        "changed": true,
        "deleted_managed_disk_ids": [
            "/subscriptions/97XXX-XXXX/resourceGroups/ansiblerg/providers/Microsoft.Compute/disks/secwin009"
        ],
        "deleted_network_interfaces": [
            {
                "name": "nic_secwin009",
                "resource_group": "ansiblerg"
            }
        ],
        "deleted_public_ips": [
            {
                "name": "pip_secwin009",
                "resource_group": "ansiblerg"
            }
        ],
        "deleted_vhd_uris": [],
        "failed": false,
        "powerstate_change": null
    }
}

TASK [Deleting network interface - "nic_secwin009"] ********************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "vnet": {
        "changed": false,
        "failed": false,
        "state": null
    }
}

TASK [Deleting Public IP - "pip_secwin009"] ****************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "pip": {
        "changed": false,
        "failed": false,
        "state": {}
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
127.0.0.1                  : ok=8    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

This will remove all the resources from Azure which were created when we deployed the virtual machine.

You can find the code on my Github account : https://github.com/asharsidd/ansible.git

Leave a comment

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