Is there a way to access a play's vars section?

Given a play, I would like to reference somehow its vars section. I would like to achieve any of the two:

  • The next play to have access to the vars of the previous play like “{{ prev_play_vars }}”
  • The vars section of each play to be available to the rest of the plays via sth like “{{play_name_here_vars}}”

I tried messing around with a custom callback plugin but that didn’t get me anywhere.

How could I achieve sth like that?

You cannot, play vars are only in scope for that play. The only vars
in a playbook you can reference across all plays are host vars.

Thanks. In that case, could I add a task to a play programmatically? I have around 20 plays and I try to have the vars section of each play to be saved locally to a yaml file. Right now, at the end of each play, I use a ‘copy’ tasks that reads the vars via a yaml alias. I would like to not repeat myself. I tried to place this task in a different play that is run after each one of the above plays , but ansible complains about an undefined alias. Do you see any way to not have to repeat the task in every play?

I forgot to mention that import_tasks or include_tasks also results in the undefined alias error.

To save vars to a yaml file i would just use a template task, unsure
what you are doing or why, but this seems wrong to me.

On the import_tasks, include_tasks .. no clue what 'undefined alias'
is nor can i reproduce it, you need to give a full error with -vvv
(which will include traceback)