本文将介绍如何使用 Ansible Playbook 和 vmware_host_config_info 模块自动化收集指定集群(例如“production”)中所有 VMware ESX/ESXi 主机的高级配置信息。
Ansible 收集指定集群中所有 VMware ESX/ESXi 主机的配置信息
- 模块:
community.vmware.vmware_host_config_info - 功能:收集 ESXi 主机的高级配置信息
- 适用集合:
community.vmware - 支持:社区支持
vmware_host_config_info 模块用于收集 vCenter 中 ESXi 主机的高级配置信息。它可以帮助您获取主机的详细配置参数,以便进行管理和分析。
参数
hostname(字符串):vCenter 或 ESXi 主机名port(整数):连接端口,默认为 443username(字符串):用户名password(字符串):密码datacenter(字符串):数据中心名称validate_certs(布尔值):是否验证证书cluster_name(字符串):要查询的集群名称esxi_hostname(字符串):要查询的特定 ESXi 主机名(可选)
这些参数用于收集指定集群中所有 ESX/ESXi 主机的配置信息。首先,需要使用 hostname、username、password、datacenter 和 validate_certs 等参数建立与 vCenter 的连接。连接成功后,可以指定要查询的集群名称(cluster_name),并收集该集群中所有主机的配置信息。
示例代码
Playbook 文件(host_info_cluster.yml)
---
- name: 收集集群中主机信息示例
hosts: localhost
become: false
gather_facts: false
collections:
- community.vmware
pre_tasks:
- include_vars: vars.yml
tasks:
- name: 收集指定集群中所有 ESXi 主机的信息
community.vmware.vmware_host_config_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: "{{ vcenter_validate_certs }}"
cluster_name: "{{ cluster_name }}"
register: cluster_info
- name: 打印集群信息
ansible.builtin.debug:
var: cluster_info
变量文件(vars.yml)
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
cluster_name: "production"
主机清单文件(inventory)
localhost
执行 Playbook
$ ansible-playbook host_info_cluster.yml
PLAY [cluster info demo] ******************************************************
TASK [include_vars] ******************************************************
ok: [localhost]
TASK [收集指定集群中所有 ESXi 主机的信息] ***********************************
ok: [localhost]
TASK [打印集群信息] ******************************************************
ok: [localhost] => {
"cluster_info": {
"changed": false,
"failed": false,
"hosts_info": {
"esxi1.example.com": {
"Annotations.WelcomeMessage": "",
"BufferCache.FlushInterval": 30000,
"Config.HostAgent.log.level": "info",
"Coredump.Net.DumpIp": "0.0.0.0",
"Coredump.Net.Enabled": false,
"Coredump.Net.Port": 6500,
"Coredump.Net.SSLThumbprint": "",
"Coredump.Net.SSLThumbprintWarning": false,
"Coredump.Net.Throttle": 0,
"Coredump.Net.Timeout": 0,
"Coredump.Net.UseDhcp": false,
"Coredump.Net.Vnic": "",
"Coredump.Net.VnicWarning": false,
"Coredump.Net.VnicWarningThreshold": 0,
"Coredump.Net.VnicWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThreshold": 0,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThreshold": 0,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarning": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThreshold": false,
"Coredump.Net.VnicWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWarningThresholdWar