共用方式為


使用 Ansible 劇本大規模連線機器

您可以使用 Ansible 劇本,大規模將 Ansible 受控節點上線至已啟用 Azure Arc 的伺服器。 若要這樣做,您必須下載、修改,然後執行適當的劇本。

開始之前,請務必檢閱必要條件並確認訂用帳戶和資源已符合需求。 如需支援區域和其他相關考量的相關資訊,請參閱支援的 Azure 區域。 另請檢閱大規模規劃指南,以了解設計和部署準則,以及我們的管理和監視建議。

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

SQL Server 的自動連線

當您將 Windows 或 Linux 伺服器連線到同樣已安裝 Microsoft SQL Server 的 Azure Arc 時,SQL Server 執行個體也會自動連線到 Azure Arc。 透過 Azure Arc 啟用的 SQL Server 會為您的 SQL Server 執行個體和資料庫提供詳細清查和其他管理功能。 在連線程序中,擴充功能會部署到已啟用 Azure Arc 的伺服器,並將新角色套用至您的 SQL Server 和資料庫。 如果您不想自動將 SQL Server 連線到 Azure Arc,則可以在連線到 Azure Arc 時,將標籤新增至 Windows 或 Linux 伺服器,並加上名稱和 ArcSQLServerExtensionDeploymentDisabled,以選擇退出。

如需詳細資訊,請參閱管理透過 Azure Arc 啟用的 SQL Server 的自動連線

產生服務主體並收集 Azure 詳細資料

您必須先執行下列動作,才能執行指令碼來連線您的機器:

  1. 遵循步驟以建立服務主體進行大規模上線

    • 將 Azure Connected Machine Onboarding 角色指派給您的服務主體,並將角色的範圍限制為目標 Azure 訂用帳戶或資源群組。
    • 記下服務主體祕密和服務主體用戶端識別碼,因為稍後您將需要這些資訊。
  2. 收集租用戶識別碼、訂用帳戶識別碼、資源群組,以及區域 (其中將上線已啟用 Azure Arc 的資源) 的詳細資料。

下載 Ansible 劇本

如果您要將機器上線到已啟用 Azure Arc 的伺服器,請複製下列 Ansible 劇本範本,並將劇本儲存為 arc-server-onboard-playbook.yml

---
- name: Onboard Linux and Windows Servers to Azure Arc-enabled servers with public endpoint connectivity
  hosts: all
  # vars:
  #   azure:
  #     service_principal_id: 'INSERT-SERVICE-PRINCIPAL-CLIENT-ID'
  #     service_principal_secret: 'INSERT-SERVICE-PRINCIPAL-SECRET'
  #     resource_group: 'INSERT-RESOURCE-GROUP'
  #     tenant_id: 'INSERT-TENANT-ID'
  #     subscription_id: 'INSERT-SUBSCRIPTION-ID'
  #     location: 'INSERT-LOCATION'
  tasks:
  - name: Check if the Connected Machine Agent has already been downloaded on Linux servers
    stat:
      path: /usr/bin/azcmagent
      get_attributes: False
      get_checksum: False
    register: azcmagent_lnx_downloaded
    when: ansible_system == 'Linux'

  - name: Download the Connected Machine Agent on Linux servers
    become: yes
    get_url:
      url: https://aka.ms/azcmagent
      dest: ~/install_linux_azcmagent.sh
      mode: '700'
    when: (ansible_system == 'Linux') and (azcmagent_lnx_downloaded.stat.exists == false)

  - name: Install the Connected Machine Agent on Linux servers
    become: yes
    shell: bash ~/install_linux_azcmagent.sh
    when: (ansible_system == 'Linux') and (not azcmagent_lnx_downloaded.stat.exists)

  - name: Check if the Connected Machine Agent has already been downloaded on Windows servers
    win_stat:
      path: C:\Program Files\AzureConnectedMachineAgent
    register: azcmagent_win_downloaded
    when: ansible_os_family == 'Windows'

  - name: Download the Connected Machine Agent on Windows servers
    win_get_url:
      url: https://aka.ms/AzureConnectedMachineAgent
      dest: C:\AzureConnectedMachineAgent.msi
    when: (ansible_os_family == 'Windows') and (not azcmagent_win_downloaded.stat.exists)

  - name: Install the Connected Machine Agent on Windows servers
    win_package:
      path: C:\AzureConnectedMachineAgent.msi
    when: (ansible_os_family == 'Windows') and (not azcmagent_win_downloaded.stat.exists)

  - name: Check if the Connected Machine Agent has already been connected
    become: true
    command:
     cmd: azcmagent check
    register: azcmagent_lnx_connected
    ignore_errors: yes
    when: ansible_system == 'Linux'
    failed_when: (azcmagent_lnx_connected.rc not in [ 0, 16 ])
    changed_when: False

  - name: Check if the Connected Machine Agent has already been connected on windows
    win_command: azcmagent check
    register: azcmagent_win_connected
    when: ansible_os_family == 'Windows'
    ignore_errors: yes
    failed_when: (azcmagent_win_connected.rc not in [ 0, 16 ])
    changed_when: False

  - name: Connect the Connected Machine Agent on Linux servers to Azure Arc
    become: yes
    shell: azcmagent connect --service-principal-id "{{ azure.service_principal_id }}" --service-principal-secret "{{ azure.service_principal_secret }}" --resource-group "{{ azure.resource_group }}" --tenant-id "{{ azure.tenant_id }}" --location "{{ azure.location }}" --subscription-id "{{ azure.subscription_id }}"
    when:  (ansible_system == 'Linux') and (azcmagent_lnx_connected.rc is defined and azcmagent_lnx_connected.rc != 0)

  - name: Connect the Connected Machine Agent on Windows servers to Azure
    win_shell: '& $env:ProgramFiles\AzureConnectedMachineAgent\azcmagent.exe connect --service-principal-id "{{ azure.service_principal_id }}" --service-principal-secret "{{ azure.service_principal_secret }}" --resource-group "{{ azure.resource_group }}" --tenant-id "{{ azure.tenant_id }}" --location "{{ azure.location }}" --subscription-id "{{ azure.subscription_id }}"'
    when: (ansible_os_family == 'Windows') and (azcmagent_win_connected.rc is defined and azcmagent_win_connected.rc != 0)

修改 Ansible 劇本

在下載 Ansible 劇本之後,請完成下列步驟:

  1. 在 Ansible 劇本內,使用稍早收集的服務主體和 Azure 詳細資料,修改 vars 區段 底下的變數:

    • 服務主體識別碼
    • 服務主體祕密
    • 資源群組
    • 租用戶識別碼
    • 訂用帳戶識別碼
    • 區域
  2. 輸入正確的主機欄位,以擷取目標伺服器來上線至 Azure Arc。您可以採用 Ansible 模式,選擇性地鎖定要上線的混合式機器。

  3. 此範本會將服務主體祕密當作 Ansible 劇本中的變數傳遞。 請注意,Ansible 保存庫可以用來加密此秘密,而且變數可以透過設定檔傳遞。

執行 Ansible 劇本

從 Ansible 控制節點中,叫用 ansible-playbook 命令來執行 Ansible 劇本:

ansible-playbook arc-server-onboard-playbook.yml

在劇本執行之後,PLAY RECAP 會指出所有工作是否已成功完成,並顯示其中工作失敗的任何節點。

驗證與 Azure Arc 的連線

在成功安裝了代理程式並將其設定為連線至已啟用 Azure Arc 的伺服器之後,請移至 Azure 入口網站,確認目標主機中的伺服器已成功連線。 在 Azure 入口網站中檢視您的機器。

下一步