Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook


I have a playbook that runs different roles on different hosts.
Is it possible to pass a variable from one role running on one host to another role on another host running within the same playbook run? Or any workaround ?

The task in role1 that sets teh variable db looks like this:

UPDATE: On the first host the values are dynamic, it’s like a configuration file that is always updated. After I store the values in variables on host1 with the role1 I then move to the host2, run the role2 and do stuff with those values from variables stored by host1.

I tried with hostvars:

and I get error:

LBL exists in hosts as on it I run the first role

I set a variable on one host and I want that variable to be available to the other host. All this within a single playbook. Can it be done ?

hostvars is not working using it like this:




The problem is in your inventory.

This message:

is because LBL is a group and not a host. Group LBL has one host in it:

Do one of the following:

1. Change your inventory (/etc/ansible/hosts) to:

2. or if you really know what you’re doing and LBL is a group and you wanna keep it that way then access the variable with:

Again LBL is a group not a host. More info.

Leave a Reply