# hostname as a variable in 'groups' magic variable

**URL:** <https://forum.ansible.com/t/hostname-as-a-variable-in-groups-magic-variable/33434>\
**Category:** Ansible Project\
**Created:** [July 20, 2020, 10:26am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-groups-magic-variable/33434 "2020-07-20T10:26:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_A](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@David\_A](https://forum.ansible.com/u/David_A)\
**Post date:** [July 20, 2020, 10:26am UTC](https://forum.ansible.com/t/hostname-as-a-variable-in-groups-magic-variable/33434/1 "2020-07-20T10:26:45Z")

</div>

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”.
