title | description | ms.service | keywords | author | manager | ms.author | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|
Tutorial - Configure labs in Azure DevTest Labs using Ansible |
Learn how to configure a lab in Azure DevTest Labs using Ansible |
ansible |
ansible, azure, devops, bash, playbook, devtest labs |
tomarchermsft |
jeconnoc |
tarcher |
tutorial |
04/30/2019 |
[!INCLUDE ansible-28-note.md]
Azure DevTest Labs allows developers to automate the creation of VM environments for their apps. These environments can be configured for app developing, testing, and training.
[!INCLUDE ansible-tutorial-goals.md]
[!div class="checklist"]
- Create a lab
- Set the lab policies
- Set the lab schedules
- Create the lab virtual network
- Define an artifact source for the lab
- Create a VM within the lab
- List the lab's artifact sources and artifacts
- Get Azure Resource Manager information for the artifact sources
- Create the lab environment
- Create the lab image
- Delete the lab
[!INCLUDE open-source-devops-prereqs-azure-subscription.md] [!INCLUDE open-source-devops-prereqs-create-service-principal.md] [!INCLUDE ansible-prereqs-cloudshell-use-or-vm-creation2.md]
The sample playbook snippet creates an Azure resource group. A resource group is a logical container in which Azure resources are deployed and managed.
- name: Create a resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
The next task creates the sample lab.
- name: Create the lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: "{{ location }}"
storage_type: standard
premium_data_disks: no
register: output_lab
You can set up lab policy settings. The following values can be set:
user_owned_lab_vm_count
is the number of VMs a user can ownuser_owned_lab_premium_vm_count
is the number of premium VMs a user can ownlab_vm_count
is the maximum number of lab VMslab_premium_vm_count
is the maximum number of lab premium VMslab_vm_size
is the allowed lab VMs size(s)gallery_image
is the allowed gallery image(s)user_owned_lab_vm_count_in_subnet
is the maximum number of user’s VMs in a subnetlab_target_cost
is the target cost of the lab
- name: Set the lab policies
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
The sample task in this section configures the lab schedule.
In the following code snippet, the lab_vms_startup
value is used to specify the VM startup time. Likewise, setting the lab_vms_shutdown
value establishes the lab VM shutdown time.
- name: Set the 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
This following task creates the default lab virtual network.
- name: Create the lab virtual network
azure_rm_devtestlabvirtualnetwork:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: "{{ vn_name }}"
location: "{{ location }}"
description: My lab virtual network
register: output
An artifacts source is a properly structured GitHub repository that contains artifact definition and Azure Resource Manager templates. Every lab comes with predefined public artifacts. The follow tasks shows you how to create an artifact source for a lab.
- name: Define the lab 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 }}"
Create a VM within the lab.
- name: Create a VM within the lab
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"
To list all default and custom artifacts sources in the lab, use the following task:
- name: List the artifact sources
azure_rm_devtestlabartifactsource_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
register: output
- debug:
var: output
The following task lists all the artifacts:
- name: List the artifact facts
azure_rm_devtestlabartifact_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
artifact_source_name: public repo
register: output
- debug:
var: output
To list all the Azure Resource Manager templates in public environment repository
, the predefined repository with templates:
- name: List the Azure Resource Manager template facts
azure_rm_devtestlabartifactsource_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
register: output
- debug:
var: output
And the following task retrieves details of a specific Azure Resource Manager template from the repository:
- name: Get Azure Resource Manager 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
The following task creates the lab environment based on one of the templates from public environment repository.
- name: Create the 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
The following task creates an image from a VM. The image allows you to create identical VMs.
- name: Create the 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
To delete the lab, use the following task:
- name: Delete the 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
There are two ways to get the complete sample playbook:
- Download the playbook and save it to
devtestlab-create.yml
. - Create a new file named
devtestlab-create.yml
and copy into it the following contents:
---
- hosts: localhost
#roles:
# - azure.azure_preview_modules
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
tasks:
- name: Create a resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
- name: Create the lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: eastus
storage_type: standard
premium_data_disks: no
register: output_lab
- name: Set the lab policies
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
- name: Set the 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
- name: Create the lab virtual network
azure_rm_devtestlabvirtualnetwork:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: "{{ vn_name }}"
location: eastus
description: My lab virtual network
register: output
- name: Define the lab 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 }}"
- name:
set_fact:
artifact_source:
- source_name: "{{ artifacts_name }}"
source_path: "/Artifacts/linux-install-mongodb"
when: "github_token | length > 0"
- name:
set_fact:
artifact_source: null
when: "github_token | length == 0"
- name: Create a VM within the lab
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"
- name: List the artifact sources
azure_rm_devtestlabartifactsource_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
register: output
- debug:
var: output
- name: List the artifact facts
azure_rm_devtestlabartifact_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
artifact_source_name: public repo
register: output
- debug:
var: output
- name: List the Azure Resource Manager 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
- name: Get Azure Resource Manager 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
- name: Create the 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"
- name: Create the lab image
azure_rm_devtestlabcustomimage:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: myImage
source_vm: "{{ vm_name }}"
linux_os_state: non_deprovisioned
- name: Delete the lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
state: absent
In this section, run the playbook to test various features shown in this article.
Before running the playbook, make the following changes:
- In the
vars
section, replace the{{ resource_group_name }}
placeholder with the name of your resource group. - Store the GitHub token as an environment variable named
GITHUB_ACCESS_TOKEN
.
Run the playbook using the ansible-playbook
command:
ansible-playbook devtestlab-create.yml
When no longer needed, delete the resources created in this article.
Save the following code as cleanup.yml
:
- hosts: localhost
vars:
resource_group: myResourceGroup
tasks:
- name: Delete a resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
force_delete_nonempty: yes
state: absent
Run the playbook using the ansible-playbook
command:
ansible-playbook cleanup.yml
[!div class="nextstepaction"] Ansible on Azure