hostname as a variable in 'groups' magic variable

I’m trying to set a fact on the localhost (the ansible control node) about a remote hosts ip using magic variables.
After searching online someone suggested to do the following

  • name: set my_fact_var fact
    set_fact:
    my_fact_var: “{{ hostvars[item][‘ansible_default_ipv4’][‘address’] }}”
    with_items: “{{ groups[“prefix_” ~ hostname_var ~ “_postfix”] }}”

To clarify

  1. This is my group name “prefix_somename_postfix”.

  2. hostname_var is defined in advance and has the value of “somename”

  3. In the “groups” magic variable I want to use hostname_var instead of the string “somename”.