如何从Ansible模块vmware_vm_facts获取格式化的事实?

时间:2017-06-23 12:45:11

标签: ansible

我尝试使用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" } } }

但我明白如何只过滤name和ip_address? 我试过with_item和with_dict,但没有成功。

1 个答案:

答案 0 :(得分:3)

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

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