How to get remote server facts on ansible

ansibledeployment

I have playbook where i setup web-servers.
And in this playbook i want to know local-network ip address of db server.

All this servers i have in inventory file.

How i can do it?

I tried to use hostvars, but they have only local server facts.

Best Answer

Hostvars and remote variables

It's possible to reference variables on remote servers using hostvars. For example, let's say you're running a play on a server in your web-servers group. You can reference variables on your db-server in the playbook like this:

{{ hostvars['db-server.example.com']['ansible_default_ipv4']['address'] }}

Be advised that for this to work you must already have talked to db-server.example.com in the current play or another play higher up in the playbook. Ansible 1.8, (still in development at the time of writing) will remedy this by implementing optional fact caching, which will allow you to save facts between playbook runs.