Unable to load variable from include_vars into my playbook

I have a playbook where I load a variable file

`
cat vars/BASE.yaml

EXCLUDE_front: value1
EXCLUDE_back: value2
`

I run my test.yml playbook using this command:

`
ansible-playbook test.yml -e Layer=“back” -e base_folder=“/tmp”

  • name: Load a variable file"
    include_vars:
    file: “vars/BASE.yaml”

  • name: Collect data
    shell: “cd {{ base_folder }} && find . -type d \( -name ‘BACKUP’ -o -name ‘vars[EXCLUDE_{{ Layer }}]’ \) -prune -o -type f -exec cksum {} + | awk ‘{$1=""}1’”
    register: outputfiles

`

I also tried

shell: "cd {{ base_folder }} && find . -type d \\( -name 'BACKUP' -o -name 'vars[EXCLUDE_ + Layer ]' \\) -prune -o -type f -exec cksum {} + | awk '{$1=\"\"}1'"

and

shell: "cd {{ base_folder }} && find . -type d \\( -name 'BACKUP' -o -name 'vars[EXCLUDE_ ~ Layer ]' \\) -prune -o -type f -exec cksum {} + | awk '{$1=\"\"}1'"

however, instead of loading value1 or value2, I get the below nature of error:

"The task includes an option with an undefined variable. The error was: ‘EXCLUDE_’ is undefined\n\nT


Can you please suggest what is wrong with my playbook? 

You can’t resolve variables with names that contain variables themselves.

Instead, use a lookup:

https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html

Also try to use the args: chdir option instead of doing &&, see
https://docs.ansible.com/ansible/latest/modules/shell_module.html

You can’t resolve variables with names that contain variables themselves.
Instead, use a lookup:

https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html

Also try to use the args: chdir option instead of doing &&, see
https://docs.ansible.com/ansible/latest/modules/shell_module.html

"{{ vars['EXCLUDE_' + Layer] }}" should work though.

Regards
         Racke

It’s works. Thank you Stefen