Dmitry Ge
Dmitry Ge

Reputation: 23

How to get formatted facts from Ansible module vmware_vm_facts?

I tried to get virtual machines facts from ESXi with Ansible. My playbook here:

- 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

And i got some results:

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" } } }

But i understand how to filter only name and ip_address? I tried with_item and with_dict but unsuccessful.

Upvotes: 2

Views: 2292

Answers (1)

zigarn
zigarn

Reputation: 11595

To iterate on virtual machines, you have to use instance_vm_facts.virtual_machines. As it's not a list, you have to use with_dict and then access name with item.key and IP with item.value.ip_address, or power state with item.value.power_state, ...

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

Upvotes: 3

Related Questions