4

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.

Falcon
  • 213
  • 3
  • 7

1 Answers1

3

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.

Philip Wilson
  • 606
  • 5
  • 6
  • But if i doesn't interact i can't do it? I thought that ansible has command to go to the remote server from repository and take it facts. Ok. Thanks – Falcon Sep 14 '14 at 13:47
  • And about fact caching. It isn't i good idea(if i understood this correctly). Fact caching it is just cache for improve speed! This cache can be expired. For example, i setup my db severs, setup web-servers using fact cache. It's ok. But if i need to update my web-servers after 1 week, it will fall because cache is empty. – Falcon Sep 14 '14 at 13:52
  • All you need to do is make sure that Ansible has already talked to the db-server before you try to run a play on another server that references a fact on the db server. You could for example have an earlier play that runs and uses Ansible's [setup module](http://docs.ansible.com/setup_module.html) to gather facts on the db-server. – Philip Wilson Sep 14 '14 at 15:26