這很重要
需要 Ansible 2.8(或以上版本)才能執行本文中的範例 playbooks。
Azure 開發測試實驗室 可讓開發人員自動為其應用程式建立 VM 環境。 這些環境可以設定為應用程式開發、測試和訓練。
在本文中,您將學會如何:
- 建立實驗室
- 設定實驗室原則
- 設定實驗室排程
- 建立實驗室虛擬網路
- 定義實驗室的成品來源
- 在實驗室內建立 VM
- 列出實驗室的工件來源和工件
- 取得 Azure Resource Manager 的構件來源資訊
- 建立實驗室環境
- 建立實驗室映像
- 刪除實驗室
先決條件
- Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
- Azure 服務主體: 建立服務主體,並記下下列值: appId、 displayName、 密碼和 租戶。
安裝 Ansible:執行下列其中一個選項:
- 在Linux虛擬機器上安裝及設定Ansible
- 設定 Azure Cloud Shell ,如果您無法存取 Linux 虛擬機器,請 使用 Ansible 建立虛擬機器。
建立資源群組
範例劇本程式碼片段會建立 Azure 資源群組。 資源群組是用來部署和管理 Azure 資源的邏輯容器。
- 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: "{{ location }}"
storage_type: standard
premium_data_disks: no
register: output_lab
設定實驗室原則
您可以設定實驗室原則設定。 可以設定下列值:
-
user_owned_lab_vm_count是使用者可以擁有的虛擬機器數目 -
user_owned_lab_premium_vm_count是使用者可以擁有的進階虛擬機器數目 -
lab_vm_count是實驗室虛擬機器數目上限 -
lab_premium_vm_count是實驗室進階虛擬機器的數目上限 -
lab_vm_size是允許的實驗室 VM 大小 () -
gallery_image是被允許的圖庫圖片 -
user_owned_lab_vm_count_in_subnet是子網路中使用者虛擬機器數目上限 -
lab_target_cost是實驗室的目標成本
- 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
設定實驗室排程
本節中的範例工作會設定實驗室排程。
在下列程式碼片段中,此 lab_vms_startup 值可用來指定 VM 啟動時間。 同樣地,設定值 lab_vms_shutdown 會建立實驗室 VM 關機時間。
- 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: "{{ location }}"
description: My lab virtual network
register: output
定義實驗室的工件來源
成品來源是結構正確的 GitHub 存放庫,其中包含成品定義和 Azure Resource Manager 範本。 每個實驗室都帶有預定義的公共工件。 下列工作示範如何為實驗室建立工件來源。
- 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 }}"
在實驗室內建立 VM
在實驗室內建立 VM。
- 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
取得工件來源的 Azure Resource Manager 資訊
若要列出 public environment repository 中的所有 Azure Resource Manager 範本,請參考具有範本的預先定義存放庫:
- 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
下列工作會從存放庫擷取特定 Azure Resource Manager 範本的詳細資料:
- 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"
register: output
建立實驗室映像
執行以下任務以從 VM 建立映像檔。 此映像可讓您建立相同的虛擬機器。
- 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
刪除實驗室
若要刪除實驗室,請使用下列步驟:
- 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
取得範例教戰手冊
有兩種方法可以取得完整的範例教戰手冊:
-
下載教戰手冊 並將其儲存至
devtestlab-create.yml。 - 建立名為的新
devtestlab-create.yml檔案,並將下列內容複製到其中:
---
- 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
執行作業腳本
在本節中,執行腳本來測試本文中顯示的各種功能。
執行操作流程之前,請進行下列變更:
- 在段落
vars中,將{{ resource_group_name }}預留位置取代為您的資源組的名稱。 - 將 GitHub 權杖儲存為名為
GITHUB_ACCESS_TOKEN的環境變數。
使用 ansible-playbook 執行指令集
ansible-playbook devtestlab-create.yml
清理資源
將下列程式碼儲存為
delete_rg.yml。--- - hosts: localhost tasks: - name: Deleting resource group - "{{ name }}" azure_rm_resourcegroup: name: "{{ name }}" state: absent register: rg - debug: var: rg使用 ansible-playbook 命令執行教戰手冊。 將佔位符取代為要刪除的資源組名稱。 資源群組內的所有資源都會被刪除。
ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"重點:
- 由於教戰手冊中的
register變數和debug區段,當命令完成時,結果會顯示。
- 由於教戰手冊中的