你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Ansible playbook 大规模连接计算机

可以使用 Ansible playbook 大规模地将 Ansible 托管节点加入到已启用 Azure Arc 的服务器。 为此,需要下载、修改并运行相应的 playbook。

在开始之前,请务必查看先决条件,并验证你的订阅和资源是否符合要求。 有关支持的区域和其他相关注意事项的信息,请参阅支持的 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,则可通过在 Windows 或 Linux 服务器连接到 Azure Arc 时向其添加一个名为 ArcSQLServerExtensionDeployment 且值为 Disabled 的标记来选择退出。

有关详细信息,请参阅管理 Azure Arc 启用的 SQL Server 自动连接

生成服务主体并收集 Azure 详细信息

在运行脚本以连接计算机之前,需要执行以下操作:

  1. 按照步骤为大规模加入创建服务主体

    • 将 Azure Connected Machine 加入角色分配给服务主体,并将该角色的范围限定为目标 Azure 订阅或资源组。
    • 请记下服务主体机密和服务主体客户端 ID,因为稍后需要使用这些值。
  2. 收集有关租户 ID、订阅 ID、资源组和区域(将在其中加入已启用 Azure Arc 的资源)的详细信息。

下载 Ansible playbook

如果要将计算机加入已启用 Azure Arc 的服务器,请复制以下 Ansible playbook 模板并将 playbook 另存为 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 playbook

下载 Ansible playbook 后,完成以下步骤:

  1. 在 Ansible playbook 中,使用前面收集的服务主体和 Azure 详细信息修改 vars 部分下的变量:

    • 服务主体 ID
    • 服务主体机密
    • 资源组
    • 租户 ID
    • 订阅 ID
    • 区域
  2. 输入正确的主机字段,捕获要加入到 Azure Arc 的目标服务器。可以使用 Ansible 模式以要加入的混合计算机为目标。

  3. 此模板将服务主体机密作为 Ansible playbook 中的变量传递。 请注意,Ansible 保管库可用于加密此机密,变量可以通过配置文件传递。

运行 Ansible playbook

在 Ansible 控制节点中,通过调用 ansible-playbook 命令运行 Ansible playbook:

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

运行 playbook 后,PLAY RECAP 会指示是否已成功完成所有任务,并显示任务失败的节点。

验证是否与 Azure Arc 连接

成功安装代理并将其配置为连接到已启用 Azure Arc 的服务器后,请转到 Azure 门户,验证是否已成功连接目标主机中的服务器。 在 Azure 门户中查看计算机。

后续步骤