How access variable from one playbook in the other playbook?

Hello.

I'm trying to set up application with database.

My current (simplified) version looks like this:

site.yml:
- include db.yml
- include app.yml

db.yml
- hosts: db
- roles: foo.install_db

app.yml
- hosts: app
- roles: bar.install_app

In the group_var/db I have settings:
db_address: '{{ansible_eth2.address}}'

And I want to use for app:

group_var/app:
database_address: '{{db_address}}'

But it (non-)obviously does not work.

I've tried to use {{hostvar...}}, but it works with hosts, not with groups.

I feel like I'm doing something strange, but I don't know how to do it right. I need to (in runtime) get ip address of server, based on it group/role, and use it in other role. And this IP can not be hardcoded, because eth2 is not configured by ansible and can be only found in runtime...

Please help, Thanks!

Variable referencing in variable files is not something supported.
Try referencing the var like this (in the task): hostvars[groups[‘db’][0]].db_address
That is for general, cross-groups vars sharing.

But for what you need, you could use hostvars[‘db’].ansible_eth2.ipv4.address and forget about setting that in var files.

Hope this helps,

Dan.