在启用 Python 3 的 Linux Azure 虚拟机系统中使用 VM 扩展时遇到的问题Issues using VM extensions in Python 3-enabled Linux Azure Virtual Machines systems

Note

Azure 鼓励用户在系统中采用 Python 3.x,除非他们的工作负载需要 Python 2.x 支持 。Azure encourages users to adopt Python 3.x in their systems unless your workload requires Python 2.x support. 此要求的示例可能包括旧版管理脚本或扩展,如 Azure 磁盘加密和 Azure Monitor 。Examples of this requirement might include legacy administration scripts, or extensions such as Azure Disk Encryption and Azure Monitor.

在生产环境中安装 Python 2.x 之前,请考虑 Python 2.x 的长期支持问题,尤其是它们接收安全更新的能力。Before installing Python 2.x in production, consider the question of long-term support of Python 2.x, particularly their ability to receive security updates. 由于产品(包括上述一些扩展)使用 Python 3.8 支持进行更新,应停止使用 Python 2.x。As products, including some of the extension mentioned, update with Python 3.8 support, you should discontinue use of Python 2.x.

某些 Linux 发行版已转换为 Python 3.8,并完全删除了 Python 的旧版 /usr/bin/python 入口点。Some Linux distributions have transitioned to Python 3.8 and removed the legacy /usr/bin/python entrypoint for Python altogether. 此转换会影响具备以下条件的某些虚拟机 (VM) 扩展的开箱即用、自动部署:This transition impacts the out-of-the-box, automated deployment of certain virtual machine (VM) extensions with the following conditions:

  • 仍在转换为 Python 3.x 支持的扩展Extensions that are still transitioning to Python 3.x support
  • 使用旧版 /usr/bin/python 入口点的扩展Extensions that use the legacy /usr/bin/python entrypoint

已转换为 Python 3.x 的 Linux 发行版用户在尝试将这些扩展部署到自己的 VM 之前,必须确保存在旧版 /usr/bin/python 入口点。Linux distribution users who have transitioned to Python 3.x must ensure the legacy /usr/bin/python entrypoint exists before attempting to deploy those extensions to their VMs. 否则,扩展部署可能会失败。Otherwise, the extension deployment might fail.

  • 受影响的认可 Linux 发行版包括 Ubuntu Server 20.04 LTS 和 Ubuntu Pro 20.04 LTS 。Endorsed Linux distributions that are affected include Ubuntu Server 20.04 LTS and Ubuntu Pro 20.04 LTS.

  • 受影响的 VM 扩展包括 Azure 磁盘加密、Log Analytics、VM 访问(用于密码重置)和来宾诊断(用于其他性能计数器) 。Affected VM Extensions include Azure Disk Encryption, Log Analytics, VM Access (used for Password Reset), and Guest Diagnostics (used for additional performance counters).

就地升级(如从 Ubuntu 18.04 LTS 升级到 Ubuntu 20.04 LTS)应保留 /usr/bin/python 符号链接,并保持不受影响 。In-place upgrades, such as upgrading from Ubuntu 18.04 LTS to Ubuntu 20.04 LTS, should retain the /usr/bin/python symlink, and remain unaffected.

解决方法Resolution

在上文摘要中介绍的已知受影响的场景中部署扩展之前,请考虑以下常规建议:Consider the following general recommendations before deploying extensions in the known-affected scenarios described previously in the Summary:

  1. 部署扩展之前,请使用 Linux 发行版供应商提供的方法恢复 /usr/bin/python 符号链接。Before deploying the extension, reinstate the /usr/bin/python symlink by using the Linux distribution vendor-provided method.

    • 例如,对于 Python 2.7,请使用 sudo apt update && sudo apt install python-is-python2For example, for Python 2.7, use: sudo apt update && sudo apt install python-is-python2
  2. 如果已部署的实例出现此问题,请使用 VM 边栏选项卡中的“运行命令”功能运行上述命令 。If you've already deployed an instance that exhibits this problem, use the Run command functionality in the VM blade to run the commands mentioned above. 运行命令扩展本身不受向 Python 3.8 的转换的影响。The Run command extension itself is not affected by the transition to Python 3.8.

  3. 如果要部署新实例,并需要在预配时设置扩展,请使用 cloud-init 用户数据来安装上述程序包。If you are deploying a new instance, and need to set an extension at provisioning time, use cloud-init user data to install the packages mentioned above.

    例如,对于 Python 2.7:For example, for Python 2.7:

    # create cloud-init config
    cat > cloudinitConfig.json <<EOF
    #cloud-config
    package_update: true
    
    runcmd:
    - sudo apt update
    - sudo apt install python-is-python2 
    EOF
    
    # create VM
    az vm create \
        --resource-group <resourceGroupName> \
        --name <vmName> \
        --image <Ubuntu 20.04 Image URN> \
        --admin-username azadmin \
        --ssh-key-value "<sshPubKey>" \
        --custom-data ./cloudinitConfig.json
    
  4. 如果组织的策略管理员确定不应在 VM 中部署扩展,则可以在预配时禁用扩展支持:If your organization's policy administrators determine that extensions shouldn't be deployed in VMs, you can disable extension support at provisioning time:

    • REST APIREST API

      在可以使用此属性部署 VM 时禁用和启用扩展:To disable and enable extensions when you can deploy a VM with this property:

      "osProfile": {
        "allowExtensionOperations": false
      },
      

后续步骤Next steps

请参阅默认情况下自 18.04 LTS - Python 3 起其他基础的系统更改获取更多信息。Please refer to Other base system changes since 18.04 LTS - Python 3 by default for additional information.