Wednesday, October 31, 2018

Create Azure VM using Ansible - With Extension

Extensions are similar to user data from AWS EC2 instances on Azure VM.
Below is an example of creating a virtual machine on Azure using Ansible.
Apart from creating a VM, it also demos creating an Extension, I am using a Custom Script as an extension so I can perform the Cloudera Director installation on the VM.

It can be used to run any shell script on the machine.

This play has below steps:

1. Create a public IP
2. Create a NIC
3. Create a Virtual Machine
4. Create an Extension

---
- name: Deploy Azure ARM template.
  hosts: localhost
  connection: local
  gather_facts: false
  vars_files:
    - var.yml

  tasks:
  - name: Create a public ip address
    azure_rm_publicipaddress:
      resource_group: '{{ cd_resource_group }}'
      name: '{{ cd_public_ip }}'
      allocation_method: Static
      domain_name: '{{ cd_vmname }}'
      state: present
    register: output_ip_address

  - name: Dump public IP for VM which will be created
    debug:
      msg: "The public IP is {{ output_ip_address.state.ip_address }}."

  - name: Create NIC
    azure_rm_networkinterface:
      name: '{{ cd_nic_name }}'
      resource_group: '{{ cd_resource_group }}'
      virtual_network_name: '{{ cd_vnet }}'
      subnet_name: '{{ cd_subnet_name }}'
      security_group_name: '{{ cd_security_group }}'
      public_ip_address_name: '{{ cd_public_ip }}'

  - name: Create VM
    azure_rm_virtualmachine:
      resource_group: '{{ cd_resource_group }}'
      name: '{{ cd_vmname }}'
      network_interfaces: '{{ cd_nic_name }}'
      admin_username: centos
      ssh_password_enabled: true
      admin_password: xxxxxxx
      # ssh_public_keys:
      #   - path: /home/centos/.ssh/authorized_keys
      #     key_data: <your-key-data>
      vm_size: Standard_A4_v2
      state: present
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.5'
        version: latest a

  - name: Create VM Extension
    azure_rm_virtualmachine_extension:
       name: '{{ cd_vm_extention }}'
       location: eastus
       resource_group: '{{ cd_resource_group }}'
       virtual_machine_name: '{{ cd_vmname }}'
       publisher: Microsoft.Azure.Extensions
       virtual_machine_extension_type: CustomScript
       type_handler_version: 2.0
       settings: '{
                  "commandToExecute": "./director-install.sh",
                  "fileUris": ["https://raw.githubusercontent.com/samcloudera/random/master/director-install.sh"
                  ]
                }'
       auto_upgrade_minor_version: true
       state: present


Below is the playbook for deleting the Azure virtual machine.

It has below plays:

1. Deleting the extension
2. Kill VM
3. Delete the NIC
4. Delete the Public IP


---
- name: Deploy Azure ARM template.
  hosts: localhost
  connection: local
  gather_facts: false
  vars_files:
    - var.yml

  tasks:
  - name: Delete VM Extension
    azure_rm_virtualmachine_extension:
      name: '{{ cd_vm_extention }}'
      location: eastus
      resource_group: '{{ cd_resource_group }}'
      virtual_machine_name: '{{ cd_vmname }}'
      state: absent

  - name: Kill VM
    azure_rm_virtualmachine:
      resource_group: '{{ cd_resource_group }}'
      name: '{{ cd_vmname }}'
      state: absent
      remove_on_absent:
#          - network_interfaces
          - virtual_storage
#          - public_ips

  - name: Delete network interface
    azure_rm_networkinterface:
      resource_group: '{{ cd_resource_group }}'
      name: '{{ cd_nic_name }}'
      state: absent

  - name: Delete public ip
    azure_rm_publicipaddress:
      resource_group: '{{ cd_resource_group }}'
      name: '{{ cd_public_ip }}'

      state: absent