set_fact across plays in playbook

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.

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.