Samouczek: konfigurowanie laboratoriów w usłudze Azure DevTest Labs przy użyciu rozwiązania Ansible

Ważne

Rozwiązanie Ansible w wersji 2.8 (lub nowszej) jest wymagane do uruchomienia przykładowych podręczników w tym artykule.

Usługa Azure DevTest Labs umożliwia deweloperom automatyzację tworzenia środowisk maszyn wirtualnych dla swoich aplikacji. Te środowiska można skonfigurować pod kątem tworzenia, testowania i trenowania aplikacji.

W tym artykule dowiesz się, jak:

  • Tworzenie laboratorium
  • Ustawianie zasad laboratorium
  • Ustawianie harmonogramów laboratorium
  • Tworzenie sieci wirtualnej laboratorium
  • Definiowanie źródła artefaktu dla laboratorium
  • Tworzenie maszyny wirtualnej w laboratorium
  • Wyświetlanie listy źródeł artefaktów i artefaktów laboratorium
  • Uzyskiwanie informacji o usłudze Azure Resource Manager dla źródeł artefaktów
  • Tworzenie środowiska laboratoryjnego
  • Tworzenie obrazu laboratorium
  • Usuwanie laboratorium

Wymagania wstępne

  • Subskrypcja platformy Azure: jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto.
  • Jednostka usługi platformy Azure: Utwórz jednostkę usługi, zanotuj następujące wartości: appId, displayName, password i tenant.

Utwórz grupę zasobów

Przykładowy fragment podręcznika tworzy grupę zasobów platformy Azure. Grupa zasobów to logiczny kontener, w którym są wdrażane i zarządzane zasoby platformy Azure.

  - name: Create a resource group
    azure_rm_resourcegroup:
      name: "{{ resource_group }}"
      location: "{{ location }}"

Tworzenie laboratorium

Następne zadanie tworzy laboratorium testowe.

- 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

Ustawianie zasad laboratorium

Ustawienia zasad laboratorium można skonfigurować. Można ustawić następujące wartości:

  • user_owned_lab_vm_count to liczba maszyn wirtualnych, których użytkownik może posiadać
  • user_owned_lab_premium_vm_count to liczba maszyn wirtualnych w warstwie Premium, które użytkownik może posiadać
  • lab_vm_count jest maksymalną liczbą maszyn wirtualnych laboratorium
  • lab_premium_vm_count to maksymalna liczba maszyn wirtualnych klasy Premium w laboratorium
  • lab_vm_size to dozwolone rozmiary maszyn wirtualnych laboratorium
  • gallery_image to dozwolone obrazy w galerii
  • user_owned_lab_vm_count_in_subnet to maksymalna liczba maszyn wirtualnych użytkownika w podsieci
  • lab_target_cost to docelowy koszt laboratorium
- 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

Ustawianie harmonogramów laboratorium

Przykładowe zadanie w tej sekcji konfiguruje harmonogram laboratorium.

W poniższym fragmencie lab_vms_startup kodu wartość jest używana do określania czasu uruchamiania maszyny wirtualnej. Podobnie ustawienie lab_vms_shutdown wartości określa czas zamknięcia maszyny wirtualnej laboratorium.

- 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

Tworzenie sieci wirtualnej laboratorium

To poniższe zadanie tworzy domyślną sieć wirtualną laboratorium.

- 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

Definiowanie źródła artefaktu dla laboratorium

Źródło artefaktów to prawidłowo ustrukturyzowane repozytorium GitHub, które zawiera definicję artefaktu i szablony usługi Azure Resource Manager. Każde laboratorium zawiera wstępnie zdefiniowane publiczne artefakty. W poniższych zadaniach pokazano, jak utworzyć źródło artefaktu dla laboratorium.

- 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 }}"

Tworzenie maszyny wirtualnej w laboratorium

Utwórz maszynę wirtualną w laboratorium.

- 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"

Wymień źródła artefaktów laboratorium i same artefakty

Aby wyświetlić listę wszystkich domyślnych i niestandardowych źródeł artefaktów w laboratorium, użyj następującego zadania:

- name: List the artifact sources
  azure_rm_devtestlabartifactsource_facts:
    resource_group: "{{ resource_group }}"
    lab_name: "{{ lab_name }}"
  register: output
- debug:
    var: output

Następujące zadanie zawiera listę wszystkich artefaktów:

- 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

Uzyskiwanie informacji o usłudze Azure Resource Manager dla źródeł artefaktów

Aby wyświetlić listę wszystkich szablonów menedżera zasobów Azure w public environment repository, wstępnie zdefiniowanym repozytorium z szablonami:

- 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

Następujące zadanie pobiera szczegóły określonego szablonu usługi Azure Resource Manager z repozytorium:

- 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

Tworzenie środowiska laboratoryjnego

Następujące zadanie tworzy środowisko laboratoryjne na podstawie jednego z szablonów z repozytorium środowiska publicznego.

- 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

Tworzenie obrazu laboratorium

Następujące zadanie tworzy obraz z maszyny wirtualnej. Obraz umożliwia tworzenie identycznych maszyn wirtualnych.

- 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

Usuwanie laboratorium

Aby usunąć laboratorium, użyj następującego zadania:

- 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

Pobieranie przykładowego podręcznika

Istnieją dwa sposoby uzyskania kompletnego przykładowego podręcznika:

  • Pobierz podręcznik i zapisz go w pliku devtestlab-create.yml.
  • Utwórz nowy plik o nazwie devtestlab-create.yml i skopiuj do niego następującą zawartość:
---
- 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

Uruchamianie podręcznika

W tej sekcji uruchom podręcznik, aby przetestować różne funkcje przedstawione w tym artykule.

Przed uruchomieniem podręcznika wprowadź następujące zmiany:

  • W sekcji vars zastąp symbol zastępczy {{ resource_group_name }} nazwą grupy zasobów.
  • Zapisz token usługi GitHub jako zmienną środowiskową o nazwie GITHUB_ACCESS_TOKEN.

Uruchom podręcznik przy użyciu ansible-playbook

ansible-playbook devtestlab-create.yml

Uprzątnij zasoby

  1. Zapisz następujący kod jako delete_rg.yml.

    ---
    - hosts: localhost
      tasks:
        - name: Deleting resource group - "{{ name }}"
          azure_rm_resourcegroup:
            name: "{{ name }}"
            state: absent
          register: rg
        - debug:
            var: rg
    
  2. Uruchom podręcznik przy użyciu polecenia ansible-playbook . Zastąp symbol zastępczy nazwą grupy zasobów, która ma zostać usunięta. Wszystkie zasoby w grupie zasobów zostaną usunięte.

    ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"
    

    Kluczowe punkty:

    • Ze względu na zmienną register i sekcję debug podręcznika, wyniki są wyświetlane po zakończeniu polecenia.

Dalsze kroki