'How to filter out a particular line from ansible output

I am facing an issue while running my ansible-playbook to filter out the IP address from the playbook output. Here is my code

- hosts: myhost
  tasks:
    - name: Gather all VMs from a specific folder
      community.vmware.vmware_vm_info:
        hostname: myhost.com
        username: local
        password: mypass
        folder: "/VMFStorage/vm/"
        validate_certs: False
      delegate_to: localhost
      register: vm_info
    - debug:
        var: vm_info


The output of the code

ok: [myhost] => {
    "vm_info": {
        "changed": false,
        "failed": false,
        "virtual_machines": [
            {
                "attributes": {},
                "cluster": "Storage",
                "datacenter": "VMFStorage",
                "esxi_hostname": "161.92.211.24",
                "folder": "/VMFStorage/vm/",
                "guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
                "guest_name": "RHEL Machine",
                "ip_address": "192.168.1.47",
                "mac_address": [
                    "00:50:56:a3:b1:e9"
                ],
                "moid": "vm-22949",
                "power_state": "poweredOn",
                "tags": [],
                "uuid": "4223f3fa-eeae-6a96-6af5-d68dc94199f3",
                "vm_network": {
                    "00:50:56:a3:b1:e9": {
                        "ipv4": [
                            "192.168.1.47"
                        ],
                        "ipv6": [
                            "fe80::250:56ff:fea3:b1e9"
                        ]
                    }
                }
            },
            {
                "attributes": {},
                "cluster": "Storage",
                "datacenter": "VMFStorage",
                "esxi_hostname": "161.92.211.22",
                "folder": "/VMFStorage/vm/",
                "guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
                "guest_name": "Machine2",
                "ip_address": "192.168.1.29",
                "mac_address": [
                    "00:50:56:a3:b3:6d"
                ],
                "moid": "vm-21360",
                "power_state": "poweredOff",
                "tags": [],
                "uuid": "42239cd1-69ec-ff5d-a557-85d0bc8c9edc",
                "vm_network": {
                    "00:50:56:a3:b3:6d": {
                        "ipv4": [],
                        "ipv6": []
                    }
                }
            },

The required output is to catch the IP address of Machine1 guest name and print that output

192.168.1.29

I tried many possibilities but couldn't find out the proper solution Any help would be appreciated


UPDATE

- hosts: myhost
  tasks:
    - name: Gather all VMs from a specific folder
      community.vmware.vmware_vm_info:
        hostname: myhost.com
        username: local
        password: mypass
        folder: "/VMFStorage/vm/"
        validate_certs: False
      delegate_to: localhost
      register: vm_info
    - set_fact:
        ip: "{{ vm_info.virtual_machines|
                selectattr('guest_name', 'eq', 'AlmaLinuxBUILD-Machine')|
                map(attribute='ip_address')|first }}"
    - debug:
        var: ip


Solution 1:[1]

There are many options. For example,

  1. Select the dictionary from the list first and extract the attribute
    - debug:
        var: ip
      vars:
        ip: "{{ vm_info.virtual_machines|
                selectattr('guest_name', 'eq', 'Machine2')|
                map(attribute='ip_address')|first }}"

gives

  ip: 192.168.1.29

  1. If you prefer json_query the task below gives the same result
    - debug:
        var: ip
      vars:
        ip: "{{ vm_info.virtual_machines|
                json_query('[?guest_name==`Machine2`].ip_address')|first }}"

  1. Create a dictionary first if there are many items on the list that should be referenced, e.g.
    - debug:
        var: name_ip
      vars:
        name_ip: "{{ vm_info.virtual_machines|
                     items2dict(key_name='guest_name', 
                                value_name='ip_address') }}"

gives

  name_ip:
    Machine2: 192.168.1.29
    RHEL Machine: 192.168.1.47

Then, use this dictionary to get the IP of a guest. The task below also gives the same result

    - debug:
        var: ip
      vars:
        ip: "{{ name_ip['Machine2'] }}"

Q: "How can I use the ip variable in other places in the same playbook?"

A: There are many options. For example, put it into the set_facts (precedence 19)

    - set_facts:
        ip: "{{ name_ip['Machine2'] }}"
    - debug:
        var: ip

Solution 2:[2]

try this.. if you only want machine1 ip_address.

    - debug:
        msg: "{{ vm_info.virtual_machines[1].ip_address }}"

try this.. if you only want all VMs ip_addresses.

    - debug:
        msg: "{{ vm_info.virtual_machines[*].ip_address }}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2