教學課程:使用 Ansible 設定 Azure 資源的動態清查

重要

本文中需要 Ansible 2.8 (或更新版本)才能執行範例劇本。

警告

本文參考 CentOS,亦即接近生命週期結束 (EOL) 狀態的 Linux 發行版本。 請據以考慮您的使用和規劃。 如需詳細資訊,請參閱 CentOS 生命週期結束指引

Ansible 動態清查功能可移除維護靜態清查檔案的負擔。

在本教學課程中,您將使用 Azure 的動態清查外掛程式來填入 Ansible 清查。

在本文中,您將學會如何:

  • 設定兩個測試虛擬機。
  • 將標籤新增至 Azure 虛擬機
  • 產生動態清查
  • 使用條件式和索引鍵群組填入群組成員資格
  • 針對動態清查內的群組執行劇本

必要條件

  • Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • Azure 服務主體:建立服務主體,並記下下列值:appIddisplayName密碼租使用者

建立 Azure VM

  1. 登入 Azure 入口網站

  2. 開啟 Cloud Shell

  3. 建立 Azure 資源群組來保存本教學課程的虛擬機。

    重要

    您在此步驟中建立的 Azure 資源群組必須具有完全小寫的名稱。 否則,動態清查的產生將會失敗。

    az group create --resource-group ansible-inventory-test-rg --location eastus
    
  4. 使用下列其中一種技術在 Azure 上建立兩部 Linux 虛擬機:

    • Ansible 劇本 - 使用 Ansible 在 Azure 中建立基本 Linux 虛擬機和使用 Ansible 在 Azure 中建立基本 Windows 虛擬機一文說明如何從 Ansible 劇本建立虛擬機。

    • Azure CLI - 在 Cloud Shell 中發出下列每個命令,以建立這兩部虛擬機:

      az vm create \
      --resource-group ansible-inventory-test-rg \
      --name win-vm \
      --image MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest \
      --admin-username azureuser \
      --admin-password <password>
      
      az vm create \
      --resource-group ansible-inventory-test-rg \
      --name linux-vm \
      --image CentOS85Gen2 \
      --admin-username azureuser \
      --admin-password <password>
      

      <password>取代您的密碼。

新增應用程式角色標籤

標記可用來組織和分類 Azure 資源。 指派應用程式角色的 Azure VM 可讓您使用標籤作為 Azure 動態清查內的組名。

執行下列命令來更新 VM 標籤:

az vm update \
--resource-group ansible-inventory-test-rg \
--name linux-vm \
--set tags.applicationRole='message-broker' 

az vm update \
--resource-group ansible-inventory-test-rg \
--name win-vm \
--set tags.applicationRole='web-server' 

若要深入瞭解 Azure 標記策略,請參閱 定義您的標記策略

產生動態清查

Ansible 提供 Azure 動態清查外掛程式

下列步驟會逐步引導您使用外掛程式:

  1. 建立名為 的動態清查 myazure_rm.yml

    plugin: azure_rm
    include_vm_resource_groups:
      - ansible-inventory-test-rg
    auth_source: auto
    

    關鍵點:

    • Ansible 會使用清查檔名和擴展名來識別要使用的清查外掛程式。 若要使用 Azure 動態清查外掛程式,檔案必須以 azure_rm 結尾,且擴展名 yml 為 或 yaml
  2. 執行下列命令來查詢資源群組內的 VM:

    ansible-inventory -i myazure_rm.yml --graph
    
  3. 當您執行 命令時,您會看到類似下列輸出的結果:

    @all:
      |--@ungrouped:
      |  |--linux-vm_cdb4
      |  |--win-vm_3211
    

這兩部 VM 都屬於群組 ungrouped ,這是 Ansible 清查中群組的 all 子系。

關鍵點

  • 根據預設,Azure 動態清查外掛程式會傳回全域唯一的名稱。 這是 VM 名稱之後額外字元的原因。 您可以藉由新增 plain_host_names: yes 至動態清查來停用。

尋找 Azure VM hostvars

執行下列命令以檢視所有 hostvars

ansible-inventory -i myazure_rm.yml --list
{
    "_meta": {
        "hostvars": {
            "linux-vm_cdb4": {
                "ansible_host": "52.188.118.79",
                "availability_zone": null,
                "computer_name": "linux-vm",
                "default_inventory_hostname": "linux-vm_cdb4",
                "id": "/subscriptions/<subscriptionid>/resourceGroups/ansible-inventory-test-rg/providers/Microsoft.Compute/virtualMachines/linux-vm",
                "image": {
                    "offer": "CentOS",
                    "publisher": "OpenLogic",
                    "sku": "7.7",
                    "version": "latest"
                },
                ...,
                "tags": {
                    "applicationRole": "message-broker"
                },
                ...
            },
            "win-vm_3211": {
                "ansible_host": "52.188.112.110",
                "availability_zone": null,
                "computer_name": "win-vm",
                "default_inventory_hostname": "win-vm_3211",
                "id": "/subscriptions/<subscriptionid>/resourceGroups/ansible-inventory-test-rg/providers/Microsoft.Compute/virtualMachines/win-vm",
                "image": {
                    "offer": "WindowsServer",
                    "publisher": "MicrosoftWindowsServer",
                    "sku": "2019-Datacenter",
                    "version": "latest"
                },
                ...
                "tags": {
                    "applicationRole": "web-server"
                },
                ...
            }
        }
    },
    ...
    }
}

藉由從 Azure 提取資訊,動態清查會填入 hostvars 每個 Azure VM 的 。 然後,這些 hostvars 會決定 Ansible 清查內的 VM 群組成員資格。

使用conditional_groups指派群組成員資格

每個條件式群組是由兩個部分所組成。 將成員新增至群組的組名和條件。

使用 屬性image.offer來建立 linux-vm 的條件式群組成員資格。

myazure_rm.yml開啟動態清查,並新增下列conditional_group專案:

plugin: azure_rm
include_vm_resource_groups:
  - ansible-inventory-test-rg
auth_source: auto
conditional_groups:
  linux: "'CentOS' in image.offer"
  windows: "'WindowsServer' in image.offer"

ansible-inventory使用 選項執行 --graph

ansible-inventory -i myazure_rm.yml --graph
@all:
  |--@linux:
  |  |--linux-vm_cdb4
  |--@ungrouped:
  |--@windows:
  |  |--win-vm_3211

從輸出中,您可以看到 VM 不再與 ungrouped 群組相關聯。 相反地,每個群組都已指派給動態清查所建立的新群組。

關鍵點

  • 條件式群組可讓您在清查內命名特定群組,並使用 hostvars填入它們。

使用keyed_groups指派群組成員資格

索引鍵群組指派群組成員資格的方式與條件式群組相同,但使用索引鍵群組時,組名也會動態填入。

將下列keyed_group新增至 myazure_rm.yml 動態清查:

plugin: azure_rm
include_vm_resource_groups:
  - ansible-inventory-test-rg
auth_source: auto
conditional_groups:
  linux: "'CentOS' in image.offer"
  windows: "'WindowsServer' in image.offer"
keyed_groups:
 - key: tags.applicationRole

ansible-inventory使用 選項執行 --graph

ansible-inventory -i myazure_rm.yml --graph
@all:
  |--@_message_broker:
  |  |--linux-vm_cdb4
  |--@_web_server:
  |  |--win-vm_3211
  |--@linux:
  |  |--linux-vm_cdb4
  |--@ungrouped:
  |--@windows:
  |  |--win-vm_3211

從輸出中,您會看到另外兩個群組 _message_broker_web_server。 使用索引鍵群組,標記 applicationRole 會填入組名和群組成員資格。

關鍵點

  • 根據預設,索引鍵群組會包含分隔符。 若要移除索引鍵屬性底下的分隔符新增 separator: ""

使用組名模式執行劇本

使用動態清查所建立的群組,以子群組為目標。

  1. 使用下列內容建立名為 win_ping.yml 的劇本:

    ---
    - hosts: windows
      gather_facts: false
    
      vars_prompt:
        - name: username
          prompt: "Enter local username"
          private: false
        - name: password
          prompt: "Enter password"
    
      vars:
        ansible_user: "{{ username }}"
        ansible_password: "{{ password }}"
        ansible_connection: winrm
        ansible_winrm_transport: ntlm
        ansible_winrm_server_cert_validation: ignore
    
      tasks:
        - name: run win_ping
          win_ping:
    
  2. win_ping.yml執行劇本。

    ansible-playbook win_ping.yml -i myazure_rm.yml
    

    出現提示時,輸入 username Azure Windows VM 的 和 password

    Enter local username: azureuser
    Enter password:
    
    PLAY [windows] **************************************************************************************************************************************
    
    TASK [run win_ping] *********************************************************************************************************************************
    ok: [win-vm_3211]
    
    PLAY RECAP ******************************************************************************************************************************************
    win-vm_3211                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    重要

    如果您收到錯誤 winrm or requests is not installed: No module named 'winrm',請使用下列命令安裝 pywinrm: pip install "pywinrm>=0.3.0"

  3. 使用下列內容建立名為 ping.yml 的第二個劇本:

    ---
    - hosts: all
      gather_facts: false
    
      vars_prompt:
        - name: username
          prompt: "Enter ssh user"
        - name: password
          prompt: "Enter password for ssh user"
    
      vars:
        ansible_user: "{{ username }}"
        ansible_password: "{{ password }}"
        ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
    
      tasks:
        - name: run ping
          ping:
    
  4. ping.yml執行劇本。

    ansible-playbook ping.yml -i myazure_rm.yml
    

    出現提示時,輸入 usernamepassword 以取得 Azure Linux VM。

    Enter ssh username: azureuser
    Enter password for ssh user:
    
    PLAY [linux] *******************************************************************************************************
    
    TASK [run ping] ****************************************************************************************************
    ok: [linux-vm_cdb4]
    
    PLAY RECAP *********************************************************************************************************
    linux-vm_cdb4              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    

清除資源

  1. 執行 az group delete 以刪除資源群組。 將會刪除資源群組中的所有資源。

    az group delete --name <resource_group>
    
  2. 使用 az group show 確認資源群組已刪除。

    az group show --name <resource_group>
    

下一步