重要
この記事のサンプル プレイブックを実行するには、Ansible 2.8 (以降) が必要です。
Azure DevTest Labs により、開発者はアプリの VM 環境の作成を自動化できます。 これらの環境をアプリの開発、テスト、およびトレーニング用に構成することができます。
この記事では、次のことについて説明します。
- ラボを作成する
- ラボのポリシーを定義する
- ラボのスケジュールを設定する
- ラボの仮想ネットワークを作成する
- ラボの成果物ソースを定義する
- ラボ内に VM を作成する
- ラボの成果物ソースと成果物を一覧表示する
- 成果物ソースの Azure Resource Manager 情報を取得する
- ラボ環境を作成する
- ラボ イメージを作成する
- ラボを削除する
前提条件
- Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。
- Azure サービス プリンシパル: サービス プリンシパルを作成して、appId、displayName、password、および tenant の値をメモします。
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
は、ユーザーが所有できる VM の数ですuser_owned_lab_premium_vm_count
は、ユーザーが所有できるプレミアム VM の数ですlab_vm_count
は、ラボの VM の最大数ですlab_premium_vm_count
は、ラボのプレミアム VM の最大数ですlab_vm_size
は、許可されているラボの VM のサイズですgallery_image
は、許可されているギャラリー イメージですuser_owned_lab_vm_count_in_subnet
は、サブネット内のユーザーの VM の最大数です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
ラボの成果物ソースを定義する
成果物ソースは、成果物定義と Azure Resource Manager のテンプレートを含む、適切に構造化された GitHub リポジトリです。 すべてのラボには、定義済みのパブリック成果物が付属しています。 次のタスクは、ラボの成果物ソースの作成方法を示します。
- 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 からイメージを作成します。 このイメージを使用すると、同一の 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
サンプル プレイブックを取得する
完全なサンプル プレイブックを取得するには、次の 2 つの方法があります。
- プレイブックをダウンロードして、
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
セクションにより、コマンドの完了時に結果が表示されます。
- プレイブックの