The vars_files is skiped but the variables in vars_files can be used
Scenario:
1. The localhost in the group " application_hosts "
2. In the /group_vars/application_hosts file : ( defined a variable 'Enviroment' )
Environment: stage
3. I made other variables in another file : vault/stage/hello.yml
username: Clark
testuser: clark
4. The Play Book: testplaybook.yml
- hosts: localhost
vars_files:
- "vault/{{ Environment }}/hello.yml" tasks:
- name: test for vars_files debug: var=Environment
- name: hello debug: var=hostvars.localhost.username
- name: hello debug: var=testuser
- name: hello debug: var=username
5. execute with this playbook , and the below are out put:
clark@localhost:~> ansible-playbook testplaybook.yml -vvv Using /home/clark/ansible.cfg as config file
PLAYBOOK: testplaybook.yml ****************************************************** 1 plays in testplaybook.yml skipping vars_file 'vault/{{ Environment }}/hello.yml' due to an undefined variable skipping vars_file 'vault/{{ Environment }}/hello.yml' due to an undefined variable
PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
echo $HOME/.ansible/tmp/ansible-tmp-1470310770.4-132624051729407" && echo ansible-tmp-1470310770.4-132624051729407="echo $HOME/.ansible/tmp/ansible-tmp-1470310770.4-132624051729407" ) && sleep 0'"'"''
TASK [test for vars_files] ***************************************************** task path: /home/clark/testplaybook.yml:37 ok: [localhost] => { "Environment": "stage" }
TASK [hello] ******************************************************************** task path: /home/clark/testplaybook.yml:40 ok: [localhost] => { "hostvars.localhost.username": "VARIABLE IS NOT DEFINED!" }
TASK [hello] ******************************************************************** task path: /home/clark/testplaybook.yml:43 ok: [localhost] => { "testuser": "clark" }
TASK [hello] ******************************************************************** task path: /home/clark/testplaybook.yml:46 ok: [localhost] => { "username": "Clark" }
PLAY RECAP ********************************************************************* localhost : ok=5 changed=0 unreachable=0 failed=0
Question:
skipping vars_file 'vault/{{ Environment }}/hello.yml' due to an undefined variable mean the file hello.yml not loaded. So the variables defined in hello.yml should not work , but in output , the variabels testuser and username both get the values defined in hello.yml file. That's pretty weird !! And the hostvars of localhost don't have the variables named in hello.yml, that's why the "hostvars.localhost.username": "VARIABLE IS NOT DEFINED!" in output. but in the next tast , the variables in hello.yml file can be loaded, this is strange too.
So can someone explain it how does that work in this way ?
Can you provide more information ? how are you loading the Environment variable ? Also according to documentation the {{ Environment }} variable should not be quoted, it is only needed when it is after a :
Take a look here