# How to pass variable of in memory inventory to next plays using hostvars

**URL:** <https://forum.ansible.com/t/how-to-pass-variable-of-in-memory-inventory-to-next-plays-using-hostvars/29668>\
**Category:** Ansible Project\
**Created:** [February 13, 2019, 2:46pm UTC](https://forum.ansible.com/t/how-to-pass-variable-of-in-memory-inventory-to-next-plays-using-hostvars/29668 "2019-02-13T14:46:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [February 13, 2019, 2:46pm UTC](https://forum.ansible.com/t/how-to-pass-variable-of-in-memory-inventory-to-next-plays-using-hostvars/29668/1 "2019-02-13T14:46:18Z")

</div>

How to pass variable of in memory inventory to following next plays using hostvars.  
“nfs\_server” variable to be passed from play 2 to play 3 and these plays are using in-memory inventories.

Play:1

- add\_host:  
name: “{{ group1\_host }}”  
groups: group1

Play 2:

- name:  
hosts: group1  
become: yes  
tasks:

- name:  
set\_fact:  
nfs\_server: “{{ nfs\_server }}”

- shell: /usr/sbin/exportfs -av 2\> /dev/null | awk {‘print $2’} | cut -d ‘:’ -f1 | grep -v ‘\*’  
register: nfs\_clients\_out

- add\_host:  
name: “{{ item }}”  
group: “group 2”  
with\_items: “{{ nfs\_clients\_out.stdout\_lines }}”

Play 3:

- name:  
hosts: group2  
become: yes  
tasks:
- name: Fetch Variable from Previous Play  
set\_fact:  
nfs\_server: “{{ hostvars[‘item’][‘nfs\_server’] }}”  
with\_items: “{{ groups[‘group1’] }}”
