Referencing a fact from a variable (and resolve in template)

Fellow ansiblers,

Is there a way to reference a gathered fact inside a variable ?

Example use case is to get the ip address of a server (coolhost) and use
it in another host var so it can issue http requests to it.

For instance, let's say I'm building firewall rules. I need to get IP
addresses in rules since it's loaded before the device is up

I'd like to be able to do this (excuse the line break) :

Michel Blanc wrote:

Fellow ansiblers,

Is there a way to reference a gathered fact inside a variable ?

Example use case is to get the ip address of a server (coolhost) and use
it in another host var so it can issue http requests to it.

For instance, let's say I'm building firewall rules. I need to get IP
addresses in rules since it's loaded before the device is up

I'd like to be able to do this (excuse the line break) :

---
add_firewall_rules:
    - -I OUTPUT -m tcp -p tcp --dport 80 -d {{ hostvars['coolhost']
['network_interfaces_ipv4']['eth0']['address'] }} -j DROP

Use ${hostvars.{coolhost}.network_interfaces_ipv4.eth0.address} and ansible
will resolve it before Jinja2 ever sees it. Jinja2 doesn't have any way to
do variable-referencing, so you have to it using ansible's means.

Daniel

Oh, I didn't know Ansible was interpolating in vars too.

Very very cool, thanks Daniel !

M