ansible_play_name not available in playbook vars_files

Hi all

I know I have a custom setup, but I’ve discovered funny thing, not sure if it’s a bug or not, but maybe to see a second thought here.

  • name: webservers
    hosts: webservers #it’s a group in hosts file

vars:
my_custom_variable: webservers
/-- output omitted --/

#vars_files: “{{ playbook_dir }}/{{ my_custom_variable }}_vault.yml”
#vars_files: “{{ playbook_dir }}/{{ansible_play_name}}_vault.yml”

become: yes
roles:

  • common
    /-- output omitted --/

So, thing is with vars_files.
IF I use “my_custom_variable” then vault.yml file is located and used.
IF I use “ansible_play_name” then it’s not and I get error saying

ERROR! vars file {{ playbook_dir }}/{{ansible_play_name}}_vault.yml was not found
Could not find file on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option

But when I enter debug and print out those variables inside playbook task, then I can see that they are correct and point to correct names/directories.

What am I missing here? Why I cannot use ansible_play_name inside vars_files inside playbook. When I use the same variable inside template or in same playbook tasks section, everything is correct and “webservers” is used.

While I’m not an Ansible developer, I can tell you static re-use items are processed before the play starts so they can be ‘imported’ at the start of the play. Include_* is dynamic while Import_* is static (vars_files is an import).

Imports: static re-use
Importing roles, tasks, or playbooks adds them to a playbook statically. Ansible pre-processes imported files and roles before it runs any tasks in a playbook, so imported content is never affected by other tasks within the top-level playbook.