ansible-examples icon indicating copy to clipboard operation
ansible-examples copied to clipboard

The vars_files is skiped but the variables in vars_files can be used

Open clark0806 opened this issue 9 years ago • 1 comments

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] ******************************************************************* ESTABLISH SSH CONNECTION FOR USER: None SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/clark/.ansible/cp/ansible-ssh-%h-%p-%r localhost '/bin/sh -c '"'"'( umask 77 && mkdir -p "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'"'"'' PUT /tmp/tmpB9bl8k TO /home/clark/.ansible/tmp/ansible-tmp-1470310770.4-132624051729407/setup SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/clark/.ansible/cp/ansible-ssh-%h-%p-%r '[localhost]' ESTABLISH SSH CONNECTION FOR USER: None SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/clark/.ansible/cp/ansible-ssh-%h-%p-%r -tt localhost '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/clark/.ansible/tmp/ansible-tmp-1470310770.4-132624051729407/setup; rm -rf "/home/clark/.ansible/tmp/ansible-tmp-1470310770.4-132624051729407/" > /dev/null 2>&1 && sleep 0'"'"'' ok: [localhost]

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 ?

clark0806 avatar Aug 04 '16 12:08 clark0806

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

userlerueda avatar Feb 12 '17 19:02 userlerueda