# set\_fact across plays in playbook

**URL:** <https://forum.ansible.com/t/set-fact-across-plays-in-playbook/24499>\
**Category:** Ansible Project\
**Created:** [May 16, 2017, 9:17pm UTC](https://forum.ansible.com/t/set-fact-across-plays-in-playbook/24499 "2017-05-16T21:17:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [May 16, 2017, 9:17pm UTC](https://forum.ansible.com/t/set-fact-across-plays-in-playbook/24499/1 "2017-05-16T21:17:42Z")

</div>

Used to work and it stopped working. No idea why

I have a playbook with multiple plays

- name: Configuring zookeeper  
hosts: zookeeperServers  
tasks:  
#- debug: msg=“{{ inventory\_hostname }}”  
#- debug: msg=“{{ play\_hosts.index(inventory\_hostname) }}”

- set\_fact:  
tempdir: “{{ ansible\_date\_time.date }}{{ ansible\_date\_time.time[0:2] }}{{ ansib  
le\_date\_time.time[3:5] }}{{ ansible\_date\_time.time[6:8] }}”

- name: getting list of zookeeper hostnames  
local\_action: ‘lineinfile create=yes dest=/tmp/{{ tempdir }}/zookHostnames.yaml  
line=“zkhost{{ play\_hosts.index(inventory\_hostname) }}: {{ inven  
tory\_hostname }}”’

- name: get list of kafka hostnames  
hosts: kafkaServers  
tasks:  
#- debug: msg=“{{ inventory\_hostname }}”  
#- debug: msg=“{{ play\_hosts.index(inventory\_hostname) }}”

- local\_action: ‘lineinfile create=yes dest=/tmp/{{ tempdir }}/kafkaHostnames.yaml  
line=“kfkahost{{ play\_hosts.index(inventory\_hostname) }}: {{ inv  
entory\_hostname }}”’  
#- include\_vars:

# dir: ‘../group\_vars’

# depth: 1

- name: include vars  
hosts: localhost  
connection: local  
tasks:

- debug: msg=“{{ tempdir }}”

- include\_vars:  
dir: ‘/tmp/{{ tempdir }}’  
depth: 1

- name: list topic  
hosts: localhost  
tasks:

# - name: include vars

# - include\_vars: dir=“../group\_vars” depth=“1”

#dir: ‘../group\_vars’  
#depth: 1

- name:  
shell: /app/bin/kafka/bin/kafka-topics.sh --zookeeper {{ zkhost0 }}:2181 --list  
register: topic  
delegate\_to: “{{ kfkahost0 }}”
- local\_action: copy content={{ topic.stdout }} dest=./topic-list.txt

the tempdir seems to persist on the second play where i run against the kafkaServers but it errors out at the next play where i have local host.

It says tempdir is invalid

fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: "the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘tempdir’ is undefined

this worked before and i have output files to prove it. Not sure why it is not working now.

---

<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:** [May 17, 2017, 5:28pm UTC](https://forum.ansible.com/t/set-fact-across-plays-in-playbook/24499/2 "2017-05-17T17:28:17Z")

</div>

You only run set\_facts on host in zookeeperServers.  
It fails when you try to user {{ tempdir }} in the play for the localhost.  
Since localhost is not part of zookeeperServers it will not have tmpdir set and will fail as you see.
