*Is this a bug or am I misunderstanding how variables are resolved based on 
precedence? According to http://docs.ansible.com/playbooks_variables.html, *

> * extra vars (-e in the command line) always win
> * then comes connection variables defined in inventory (ansible_ssh_user, etc)
> * then comes "most everything else" (command line switches, vars in play, 
> included vars, role vars, etc)
> * then comes the rest of the variables defined in inventory
> * then comes facts discovered about a system
> * then "role defaults", which are the most "defaulty" and lose in priority to 
> everything.
>
>

*# The setup*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ tree
+-- extravars.yml
+-- inventory
+-- roles
    +-- testextravars
        +-- tasks
        ¦   +-- main.yml
        +-- vars
            +-- extravars.json
            +-- main.yml

*# The playbook*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat extravars.yml
---
- name: Test extra vars
  hosts: local
  gather_facts: false
  connection: local
  roles:
    -  { role: testextravars, sudo: no }

*# The role task*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat 
roles/testextravars/tasks/main.yml
---
- name: Test derived var
  debug:
    msg: "Test {{ derivedvar }}"

*# The derived var*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat 
roles/testextravars/vars/main.yml
---
derivedvar: "{{ basevar }}"

*# The inventory with the default value*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat inventory
[local]
localhost basevar=inventory_value

*# Extra vars - generated based on some system parameters at time of 
execution*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat 
roles/testextravars/vars/extravars.json
{
    "basevar": "override"
}

*# The run and output...*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ ansible-playbook --extra-vars 
"@roles/testextravars/vars/extravars.json" -i inventory extravars.yml

PLAY [Test extra vars] 
********************************************************

TASK: [testextravars | Test derived var] 
**************************************
ok: [localhost] => {
    "msg": "Test *inventory_value*"
}

PLAY RECAP 
********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

I’d have expected the extra-vars file to *override all inventory values, 
including vars derived from other vars* and the output to display “Test 
*override*”. If I comment out the inventory var, it shows correctly as 
“Test override” as shown below.

*# Comment out inventory var*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ cat inventory
[local]
*# localhost basevar=inventory_value*
localhost

*# Rerun play*
robin@hood:~/AnsiblePlaybooks/ExtraVars$ ansible-playbook --extra-vars 
"@roles/testextravars/vars/extravars.json" -i inventory extravars.yml

PLAY [Test extra vars] 
********************************************************

TASK: [testextravars | Test derived var] 
**************************************
ok: [localhost] => {
    "msg": "Test *override*"
}

PLAY RECAP 
********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

So unless I'm missing something, ansible is finding the var in the 
extra-vars file, implying that it's failing to follow the specified 
precedence for extra-vars.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0bdb3e6a-349f-4ffe-b087-cb89ac55d162%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to