在使用 Ansible 管理 VMware 基础设施时,可能会遇到以下错误信息:“Unknown error while connecting to vCenter or ESXi API, [Errno -2] Name or service not known”。这通常表明 Ansible 控制节点无法连接到 VMware 基础设施。问题的根源可能是:

  1. 主机名拼写错误:Ansible Playbook 中的主机名可能拼写错误。
  2. 网络连接问题:可能需要通过安全的 VPN 连接来访问目标主机。
  3. 防火墙配置问题:目标主机上的防火墙配置可能阻止了连接。

示例:故障重现与修复

以下是一个示例,展示如何重现和解决该问题。

Playbook 文件(vm_info.yml)

---
- name: info vm Playbook
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: get VM info
      vmware_guest_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        name: "{{ vm_name }}"
      register: detailed_vm_info

    - name: print VM info
      ansible.builtin.debug:
        var: detailed_vm_info

变量文件(vars.yml)

---
vcenter_hostname: "vm-ware.example.com"  # 错误的主机名
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"

执行 Playbook 运行 Playbook 时,可能会出现以下错误:

$ ansible-playbook vm_info.yml
PLAY [info vm Playbook] ******************************************************
TASK [include_vars] ******************************************************
ok: [localhost]
TASK [get VM info] ******************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unknown error while connecting to vCenter or ESXi API at vm-ware.example.com:443 : [Errno -2] Name or service not known"}
PLAY RECAP **************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

解决方法

修正主机名:确保 vars.yml 文件中的 vcenter_hostname 是正确的。

vcenter_hostname: "vmware.example.com"

验证网络配置:确保 Ansible 控制节点可以访问目标主机(例如,通过 VPN)。

重新运行 Playbook:修正后,再次运行 Playbook:

$ ansible-playbook -i inventory vm_info.yml

修正后的输出

PLAY [info vm Playbook] ******************************************************
TASK [include_vars] ******************************************************
ok: [localhost]
TASK [get VM info] ******************************************************
ok: [localhost]
TASK [print VM info] ******************************************************
ok: [localhost] => {
    "detailed_vm_info": {
        "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 基础设施时出现的“Unknown error while connecting to vCenter or ESXi API”错误。