# how to pass the value of a variable from one playbook to another.

**URL:** <https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322>\
**Category:** Ansible Project\
**Created:** [April 19, 2018, 8:12am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322 "2018-04-19T08:12:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![radha\_gupta](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@radha\_gupta](https://forum.ansible.com/u/radha_gupta)\
**Post date:** [April 19, 2018, 8:12am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/1 "2018-04-19T08:12:16Z")

</div>

Hello Guys,

Can you please help me on how can we pass the value of a variables form one playbook to another.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 19, 2018, 8:34am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/2 "2018-04-19T08:34:10Z")

</div>

The sort answer is you can't, what you can do is having multiple plays in \_one\_ playbook, a play has access to variables in previous plays.

---

<div class="post-metadata">

**Author:** ![radha\_gupta](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@radha\_gupta](https://forum.ansible.com/u/radha_gupta)\
**Post date:** [April 19, 2018, 8:40am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/3 "2018-04-19T08:40:26Z")

</div>

is it possible with any other way that we can achieve it

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 19, 2018, 9:00am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/4 "2018-04-19T09:00:30Z")

</div>

write them into files that are loaded by other playbooks. That’s basically what inventory is, but you have all sorts of other vars files…

---

<div class="post-metadata">

**Author:** ![radha\_gupta](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@radha\_gupta](https://forum.ansible.com/u/radha_gupta)\
**Post date:** [April 19, 2018, 10:00am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/5 "2018-04-19T10:00:54Z")

</div>

i have created a variable file in which i have create a variable for e.g a =10 which is working fine but i want to update the value of the variable as per the playbook (suppose playbook modifying the value a = 20) and want to use that value in another playbook(as a =20) but when i am trying to do the same variable is giving the old value(a=10) rather than the new value(a=20).

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 19, 2018, 10:13am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/6 "2018-04-19T10:13:42Z")

</div>

Sorry, I’m a beginner and haven’t used the relevant modules, so I can’t give specific advice.

But this page for 2.4 details a bunch of things you may find useful.

Or maybe someone with more experience and a better idea could leap in with a better answer 🙂

Regards, K.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 19, 2018, 2:00pm UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/7 "2018-04-19T14:00:39Z")

</div>

You have 2 main scopes for variables play scope and host scope. A  
variable you define in play A will not be available on play B.

You do have hostvars always available via  
hostvars[\<hostname\>][\<varname\>], so even if both plays target  
different hosts, you can still indirectly reference the var.

---

<div class="post-metadata">

**Author:** ![radha\_gupta](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@radha\_gupta](https://forum.ansible.com/u/radha_gupta)\
**Post date:** [April 20, 2018, 7:29am UTC](https://forum.ansible.com/t/how-to-pass-the-value-of-a-variable-from-one-playbook-to-another/27322/8 "2018-04-20T07:29:17Z")

</div>

Both the files are present on a localhost… but when i am trying to reference value b = hostvars[localhost][a] into the second file into the variable b it was throwing an error “The task includes an option with an undefined variable”…
