1

I'm trying to pull out the "uuid" from facts returned by ansible's vmware_vm_facts module. There is a solution to a similar question posted on Reddit, but when I try that, it fails with 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'ip_address'

Here is my code:

  - name: Test vmware_vm_facts
    vmware_vm_facts:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      validate_certs: no
    delegate_to: localhost
    register: rc

  - name: Debug...
    debug:
      msg: "IP of {{ item.key }} is {{ item.value.ip_address }}and power is {{ item.value.power_state }}"
    with_dict: "{{ rc.virtual_machines }}"

Anyone have any ideas? Thanks!

richf
  • 21
  • 2

1 Answers1

1

Thanks for your response @Zeitounator. I'm running Ansible 2.8 Since posting this I have received an answer that works for me. I realized that these facts are presented as a list of dicts as noted in the doco, but I was still having trouble for some reason (confused myself badly.) The solution hinges on using with_items instead of with_dict and using an appropriate conditional like so:

  - name: Test vmware_vm_facts
    vmware_vm_facts:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      validate_certs: no
    delegate_to: localhost
    register: rc


  - debug:
      msg: "IP of {{ item.guest_name }} ({{ item.uuid }}) is {{ item.ip_address }} and power is {{ item.power_state }}"
    with_items: "{{ rc.virtual_machines }}"
    when: item.guest_name is search(inventory_hostname)
richf
  • 21
  • 2