让我们一起解决 Ansible 的致命错误:“Failed to import the required Python library (PyVmomi)”,找到问题的根源,使用 PIP 安装所需的库,并成功运行 Ansible for VMware 的 Playbook 代码。

关于 Ansible 故障排除

当我们在没有安装 VMware vSphere API 所需的 Python SDK 的情况下,尝试对 VMware 基础设施运行代码时,通常会遇到这个错误消息。这种情况通常与 Ansible 控制节点的配置有关,而与 Ansible Playbook 本身无关。

故障排除演示

最好的方法是通过实际演示来展示如何解决“Failed to import the required Python library (PyVmomi)”的错误。在本次演示中,我将在一台演示机上重现该错误,并使用 Python 包管理器 PIP 来修复它。

错误重现

运行以下命令时,可能会出现错误:

$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm demo] ******************************************************
TASK [include_vars] ******************************************************
ok: [localhost]
TASK [get VM info] ******************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'pyVim'
fatal: [localhost]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "msg": "Failed to import the required Python library (PyVmomi) on demo.example.com's Python /usr/libexec/platform-python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
PLAY RECAP **************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

解决方案

1. 安装 PyVmomi

在控制节点上运行以下命令以安装 PyVmomi:

[devops@demo vmware]$ sudo su
[root@demo vmware]# pip3 install PyVmomi
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting PyVmomi
...
Successfully installed PyVmomi-7.0.3

2. 再次运行 Playbook

安装完成后,重新运行 Playbook:

$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm demo] ******************************************************
TASK [include_vars] ******************************************************
ok: [localhost]
TASK [get VM info] ******************************************************
ok: [localhost]
TASK [print VM info] ******************************************************
ok: [localhost] => {
    "detailed_vm_info": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "failed": false,
        "instance": {
            "annotation": "",
            "current_snapshot": null,
            "customvalues": {},
            "guest_consolidation_needed": false,
            "guest_question": null,
            "guest_tools_status": "guestToolsNotRunning",
            "guest_tools_version": "0",
            "hw_cluster": "prod-cluster",
            "hw_cores_per_socket": 1,
            "hw_datastores": [
                "Datastore-1"
            ],
            "hw_esxi_host": "vmware.example.com",
            "hw_eth0": {
                "addresstype": "assigned",
                "ipaddresses": null,
                "label": "Network adapter 1",
                "macaddress": "00:50:56:a5:fd:4a",
                "macaddress_dash": "00-50-56-a5-fd-4a",
                "portgroup_key": null,
                "portgroup_portkey": null,
                "summary": "VM Network"
            },
            "hw_files": [
                "[Datastore-1] myvm/myvm.vmx",
                "[Datastore-1] myvm/myvm.vmsd",
                "[Datastore-1] myvm/myvm.vmdk"
            ],
            "hw_folder": "/vmwaredatacenter/vm/myvm",
            "hw_guest_full_name": null,
            "hw_guest_ha_state": null,
            "hw_guest_id": null,
            "hw_interfaces": [
                "eth0"
            ],
            "hw_is_template": false,
            "hw_memtotal_mb": 1024,
            "hw_name": "myvm",
            "hw_power_status": "poweredOff",
            "hw_processor_count": 1,
            "hw_product_uuid": "422549b9-7e76-fb2b-da34-e9c6c8b071de",
            "hw_version": "vmx-11",
            "instance_uuid": "5025d22d-cea7-4d1c-41f9-5cd80b9603dc",
            "ipv4": null,
            "ipv6": null,
            "module_hw": true,
            "moid": "vm-17923",
            "snapshots": [],
            "vimref": "vim.VirtualMachine:vm-17923",
            "vnc": {}
        }
    }
}
PLAY RECAP **************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

总结

通过上述步骤,您可以解决 Ansible 在运行 VMware 相关任务时出现的“Failed to import the required Python library (PyVmomi)”错误。确保在 Ansible 控制节点上安装了 PyVmomi 库,并使用正确的 Python 解释器运行 Ansible。这样,您就可以顺利运行 Ansible for VMware 的 Playbook 代码。