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
-
This is my group name “prefix_somename_postfix”.
-
hostname_var is defined in advance and has the value of “somename”
-
In the “groups” magic variable I want to use hostname_var instead of the string “somename”.