如何从Ansible模块VMware_vm_facts获得格式化的事实



我试图通过Ansible从ESXI获取虚拟机事实。我的剧本在这里:

- name: VM
  local_action:
    module: vmware_vm_facts
    hostname: '{{ vcenter_hostname }}'
    username: root.
    password: '{{ esxi_root_passw }}'
    validate_certs: no
  register: instance_vm_facts
- debug: var=instance_vm_facts

我得到了一些结果:

ok: [localhost -> localhost] => { "changed": false, "invocation": { "module_args": { "hostname": "192.168.210.63", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "username": "root", "validate_certs": false } }, "virtual_machines": { "vmware-test-1": { "guest_fullname": "Red Hat Enterprise Linux 6 (64-bit)", "ip_address": "192.168.108.91", "power_state": "poweredOn" }, "vmware-test-2”: { "guest_fullname": "Red Hat Enterprise Linux 6 (64-bit)", "ip_address": "192.168.109.24", "power_state": "poweredOn" } } }

但我了解如何仅过滤名称和ip_address?我尝试了_item和_dict,但失败了。

要在虚拟机上迭代,您必须使用 instance_vm_facts.virtual_machines。由于它不是列表,因此您必须使用with_dict,然后使用item.key和IP访问item.value.ip_address的名称,或使用item.value.power_state的电源状态,...

- debug:
    msg: "IP of {{ item.key }} is {{ item.value.ip_address }}"
  with_dict: "{{ instance_vm_facts.virtual_machines }}"

最新更新