使用 Ansible playbook 大规模将计算机连接到 Azure Arc

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

先决条件

在开始之前,请确保具有以下项:

  • 一份 Azure 订阅。 如果没有订阅,请创建 一个试用订阅
  • 查看 常规先决条件 ,并验证订阅和资源是否符合要求。
  • 熟悉 支持的 Azure 区域 和其他相关注意事项。
  • 查看 大规模规划指南 ,了解设计和部署条件,以及管理和监视建议。
  • 安装并配置了 Ansible 的控制节点,用于管理您的目标设备。
  • 从 Ansible 控制节点到目标计算机和 Azure 终结点的网络连接。

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

运行脚本以连接计算机之前,请完成以下步骤:

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

    • 将 Azure Connected Machine 加入角色分配给服务主体,并将该角色的范围限定为目标 Azure 订阅或资源组。
    • 记下服务主体机密和服务主体客户端 ID 值;稍后需要这些值。
  2. 收集下表中有关已启用 Azure Arc 的资源将载入位置的详细信息。 使用此信息更新接下来的各部分中的蓝本。

    参数 Description playbook 中的变量名称
    服务主体客户端 ID 服务主体的应用程序(客户端)ID service_principal_id
    服务主体机密 服务主体的客户端机密值 service_principal_secret
    租户 ID 你的 Azure Active Directory 租户 ID tenant_id
    订阅 ID 用于创建资源的 Azure 订阅 subscription_id
    资源组 已启用 Arc 的服务器资源组的名称 resource_group
    区域 已启用 Arc 的资源将载入的 Azure 区域 location

下载 Ansible playbook

在 Ansible 控件节点上,复制以下 Ansible 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 (service_principal_id
    • 服务主体机密 (service_principal_secret
    • 资源组 (resource_group
    • 租户 ID (tenant_id
    • 订阅 ID (subscription_id)
    • 区域(location
  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 门户中查看计算机。

后续步骤