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

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’] }}”