本文将介绍如何配置 Ansible 以与 VMware 基础设施(数据中心、集群、主机系统和虚拟机)进行交互。我们将使用 Python 的 PyVmomi 库以及 Ansible 的 community.vmware 集合,并通过 vmware_guest_info 模块进行“Hello World”演示。

Ansible 提供了多种模块来管理 VMware 基础设施,包括数据中心、集群、主机系统和虚拟机。我将逐步展示如何准备 Ansible 控制节点以与 VMware 基础设施进行交互。初始配置有时会成为一些 VMware 用户开始使用 Ansible 的障碍。

配置 Ansible 以管理 VMware

支持的 vSphere 版本

  • vSphere 6.0、5.5、5.1 和 5.0 PyVmomi 支持 Python 2.7.x 和 3.4+。 Ansible 的 community.vmware 集合支持所有现代版本的 VMware vSphere,包括 vSphere 6.0、5.5、5.1 和 5.0。 Ansible 的 VMware 模块基于 PyVmomi 构建,PyVmomi 是 VMware vSphere API 的 Python SDK,允许用户管理 ESX、ESXi 和 vCenter 基础设施。 PyVmomi 支持 Python 2.7.x 和 3.4+。 Ansible 的 community.vmware 集合提供了与虚拟机相关的各种操作管理功能。需要注意的是,该集合仅提供社区支持,不由 Ansible 工程师团队直接维护。

相关链接

如何配置 Ansible 以管理 VMware

1. 安装 PyVmomi

首先,您需要安装 PyVmomi —— VMware vSphere API 的 Python 绑定。

2. 安装 community.vmware 集合

其次,您需要安装 Ansible 的 community.vmware 集合。

3. 配置主机清单和 Playbook

完成节点上的所有操作后,您可以在 Ansible 控制节点上配置 Ansible 主机清单,并运行第一个 Ansible Playbook,使用 vmware_guest_info 模块验证配置是否成功。

常见错误:缺少 Python PyVmomi 库

如果未安装 PyVmomi,运行 Playbook 时可能会出现以下错误:

$ 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

安装 PyVmomi

$ 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

安装 community.vmware 集合

$ ansible-galaxy install -r requirements.yml

示例代码

Playbook 文件(vm_info.yml)

---
- name: 获取虚拟机信息
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: 获取虚拟机信息
      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: 打印虚拟机信息
      ansible.builtin.debug:
        var: detailed_vm_info

变量文件(vars.yml)

---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"

主机清单文件(inventory)

localhost

集合依赖文件(requirements.yml)

---
collections:
  - name: community.vmware

执行 Ansible 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 基础设施进行交互。安装 PyVmomi 和 community.vmware 集合后,您可以使用 Ansible 的 VMware 模块管理虚拟机和其他资源。