Zim's Notes

Just work related notes.

New Ansible Modules to Manage Azure DevTest Lab

The new sample containing tasks to create and manage Azure DevTest Lab is available here:

https://github.com/Azure-Samples/ansible-playbooks/blob/master/devtestlab-create.yml

This sample will:

  • create resource group
  • create DevTest Lab
  • create sample DevTest Lab policy
  • create sample DevTest Lab schedule
  • create sample DevTest Lab Virtual Network
  • create sample DevTest Lab artifact source
  • create instance of DTL Virtual Machine
  • list all artifact sources
  • get artifacts source facts
  • list ARM Template facts
  • get ARM Template facts
  • create instance of DevTest Lab Environment
  • create instance of DevTest Lab Image
  • delete instance of Lab

Running the sample

Variables

1
2
3
4
5
6
7
8
  vars:
    resource_group: "{{ resource_group_name }}"
    lab_name: myLab
    vn_name: myLabVirtualNetwork
    vm_name: myLabVm
    artifacts_name: myArtifacts
    github_token: "{{ lookup('env','GITHUB_ACCESS_TOKEN') }}"
    location: eastus

Following variables can be set in vars section of the playbook:

Variable Name Description Notes
resource_group resource group where resources will be created by default it’s using resource_group_name parameter passed when running the playbook
location location where resources should be created
lab_name name of the lab instance
vn_name lab virtual network name
vm_name name of the vm instance
artifacts_name lab artifacts name
github_token GitHub token to access artifacts sources by default playbook will attempt to get it from GITHUB_ACCESS_TOKEN environment variable

Create Resource Group

This simple task creates a resource group if doesn’t exist yet.

1
2
3
4
- name: Create a resource group
  azure_rm_resourcegroup:
    name: "{{ resource_group }}"
    location: "{{ location }}"

Creating DevTest Lab

This task creates an instance of DevTest Lab.

1
2
3
4
5
6
7
8
- name: Create instance of Lab
  azure_rm_devtestlab:
    resource_group: "{{ resource_group }}"
    name: "{{ lab_name }}"
    location: "{{ location }}"
    storage_type: standard
    premium_data_disks: no
  register: output_lab

DevTest Lab Policies

This task shows how to set up DevTest Lab policy settings.

Following values can be set:

Value Description
user_owned_lab_vm_count count of VMs that can be owned by an user
user_owned_lab_premium_vm_count count of premium VMs that can be owned by an user
lab_vm_count maximum lab VM count
lab_premium_vm_count maximum lab premium VM count
lab_vm_size allowed lab VMs size(s)
gallery_image allowed gallery image(s)
user_owned_lab_vm_count_in_subnet maximum number of user’s VMs in a subnet
lab_target_cost target cost of the lab
1
2
3
4
5
6
7
8
- name: Create instance of DevTest Lab Policy
  azure_rm_devtestlabpolicy:
    resource_group: "{{ resource_group }}"
    lab_name: "{{ lab_name }}"
    policy_set_name: myDtlPolicySet
    name: myDtlPolicy
    fact_name: user_owned_lab_vm_count
    threshold: 5

DevTest Lab Schedules

Sample task setting DevTest Lab VM schedule.

Currently allowed are: |Name|Description| |—-|———–| |lab_vms_startup|Lab VMs startup time| |lab_vms_shutdown|Lab VMs shutdown time|

Time and time zone id must be specified.

1
2
3
4
5
6
7
8
    - name: Create instance of DevTest Lab Schedule
      azure_rm_devtestlabschedule:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        name: lab_vms_shutdown
        time: "1030"
        time_zone_id: "UTC+12"
      register: output

DevTest Lab Virtual Network

This task creates default DevTest Lab virtual network.

1
2
3
4
5
6
7
8
    - name: Create instance of DevTest Labs virtual network
      azure_rm_devtestlabvirtualnetwork:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        name: "{{ vn_name }}"
        location: "{{ location }}"
        description: My DevTest Lab Virtual Network
      register: output

DevTest Lab Artifact Source

This task shows how to create DevTest Lab artifacts source. DevTest Lab artifacts source is properly structured GitHub repository that contains artifact definition and ARM templates. Please note that every lab comes with predefined public artifacts source.

1
2
3
4
5
6
7
8
9
    - name: Create instance of DevTest Labs artifacts source
      azure_rm_devtestlabartifactsource:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        name: "{{ artifacts_name }}"
        uri: https://github.com/Azure/azure_preview_modules.git
        source_type: github
        folder_path: /tasks
        security_token: "{{ github_token }}"

DevTest Lab Virtual Machine

This task shows how to create DevTest Labs virtual machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- name: Create instance of DTL Virtual Machine
  azure_rm_devtestlabvirtualmachine:
    resource_group: "{{ resource_group }}"
    lab_name: "{{ lab_name }}"
    name: "{{ vm_name }}"
    notes: Virtual machine notes, just something....
    os_type: linux
    vm_size: Standard_A2_v2
    user_name: dtladmin
    password: ZSasfovobocu$$21!
    lab_subnet:
      virtual_network_name: "{{ vn_name }}"
      name: "{{ vn_name }}Subnet"
    disallow_public_ip_address: no
    image:
      offer: UbuntuServer
      publisher: Canonical
      sku: 16.04-LTS
      os_type: Linux
      version: latest
    artifacts:
      - source_name: "{{ artifacts_name }}"
        source_path: "/Artifacts/linux-install-mongodb"
    allow_claim: no
    expiration_date: "2029-02-22T01:49:12.117974Z"

Listing All Artifact Sources and Artifacts

This task lists all artifacts sources in the lab. Please use it to see default and custom artifacts sources.

1
2
3
4
5
6
7
    - name: List all artifact sources
      azure_rm_devtestlabartifactsource_facts:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
      register: output
    - debug:
        var: output

Following task lists all the artifact in public repo which is predefined artifact source.

1
2
3
4
5
6
7
8
    - name: Get artifacts source facts
      azure_rm_devtestlabartifact_facts:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        artifact_source_name: public repo
      register: output
    - debug:
        var: output

Getting Information on ARM Templates in Artifact Source

This task lists all the ARM templates in public environment repo that is predefined repo with templates.

1
2
3
4
5
6
7
8
    - name: List ARM Template facts
      azure_rm_devtestlabarmtemplate_facts:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        artifact_source_name: "public environment repo"
      register: output
    - debug:
        var: output

And following task retrieves details of a specific ARM template from the repository:

1
2
3
4
5
6
7
8
9
    - name: Get ARM Template facts
      azure_rm_devtestlabarmtemplate_facts:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        artifact_source_name: "public environment repo"
        name: ServiceFabric-LabCluster
      register: output
    - debug:
        var: output

Creating DevTest Lab Environment

Finally following task creates DevTest Lab environment. As you see it refers one of the templates from public environment repo.

1
2
3
4
5
6
7
8
9
    - name: Create instance of DevTest Lab Environment
      azure_rm_devtestlabenvironment:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        user_name: "@me"
        name: myEnvironment
        location: eastus
        deployment_template: "{{ output_lab.id }}/artifactSources/public environment repo/armTemplates/WebApp"
      register: output

Creating DevTest Lab Image

This is also a very useful task. It creates DevTest Lab image from existing DevTest Lab Virtual Machine. It can be later used to created new DevTest Lab virtual machines.

1
2
3
4
5
6
7
    - name: Create instance of DevTest Lab Image
      azure_rm_devtestlabcustomimage:
        resource_group: "{{ resource_group }}"
        lab_name: "{{ lab_name }}"
        name: myImage
        source_vm: "{{ output_vm.virtualmachines[0]['name'] }}"
        linux_os_state: non_deprovisioned

Deleting the Lab

Final task deletes entire lab.

1
2
3
4
5
6
7
8
9
10
- name: Delete instance of Lab
  azure_rm_devtestlab:
    resource_group: "{{ resource_group }}"
    name: "{{ lab_name }}"
    state: absent
  register: output
- name: Assert the change was correctly reported
  assert:
    that:
      - output.changed