Answers: <p>Variables have different priorities, see <a href="http://docs.ansible.com/ansible/latest/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable" rel="nofollow noreferrer">vars precedence</a>.</p>
  
  <p>As of Ansible 2.x:</p>
  
  <blockquote>
    <p>role defaults<br>
    inventory file or script group vars<br>
    inventory group_vars/all<br>
    playbook group_vars/all<br>
    inventory group_vars/*<br>
    playbook group_vars/*<br>
    inventory file or script host vars<br>
    inventory host_vars/*<br>
    playbook host_vars/*<br>
    host facts<br>
    play vars<br>
    play vars_prompt<br>
    play vars_files<br>
    role vars (defined in role/vars/main.yml)<br>
    block vars (only for tasks in block)<br>
    task vars (only for the task)<br>
    role (and include_role) params<br>
    include params<br>
    include_vars<br>
    set_facts / registered vars<br>
    extra vars (always win precedence)  </p>
  </blockquote>
  
  <p>For roles you would generally use <code>myrole/defaults/main.yml</code> for easily overridable vars (e.g. installation path or smth) and use <code>myrole/vars/main.yml</code> for vars that you don't want to be easily overridden (smth like required packages).</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-13T04:51:27.790Z
Answers: 2017-12-13T04:51:27.790Z
Answers: Konstantin Suvorov
Answers: <p>I missed one point which provides the answer: role defaults are the lowest in precedence. So just these vars and file_vars can overwrite in the playbook.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-12T20:10:35.893Z
Answers: 2017-12-12T20:10:35.893Z
Answers: Michael Hoeller
Body: <p>There is quite a simple setup I am sure many of us have come across: </p>
  
  <ol>
  <li>You have lean, slim and dedicate roles</li>
  <li>Every role has its own role vars</li>
  <li>Playbooks include the roles</li>
  </ol>
  
  <p>Let's assume you have a base path for an application, most likely you will need this path in many roles. To keep the roles independent you would define this base path in every role vars where needed.</p>
  
  <p>Now you include some roles in a Playbook and in case of a change on e.g. the base path you need to change that in every role.</p>
  
  <p>One help would be to use extra vars to overrule the role vars, but what if there are many potential candidates for a change?</p>
  
  <p>So I would like to be able to <code>include_vars</code> in a playbook to overrule the variables in the roles. But this is not possible as:</p>
  
  <ol>
  <li><code>include_vars</code> is not allowed in Playbooks</li>
  <li><code>var_files</code> (what would work in Playbooks) will be over written by role vars</li>
  </ol>
  
  <hr>
  
  <p><strong>Question:</strong> how do you do solve this situation?  Maybe I want something odd and there is a much simpler way?</p>
  
Tags: <ansible>
Title: Why to overrule role vars?
UserDisplayName: Michael Hoeller
ThreadText: # Why to overrule role vars?
  
  (Asked by: username_0 on 2017-12-12)
  
  There is quite a simple setup I am sure many of us have come across:
  
  1. You have lean, slim and dedicate roles
  2. Every role has its own role vars
  3. Playbooks include the roles
  
  Let's assume you have a base path for an application, most likely you will need this path in many roles. To keep the roles independent you would define this base path in every role vars where needed.
  
  Now you include some roles in a Playbook and in case of a change on e.g. the base path you need to change that in every role.
  
  One help would be to use extra vars to overrule the role vars, but what if there are many potential candidates for a change?
  
  So I would like to be able to `include_vars` in a playbook to overrule the variables in the roles. But this is not possible as:
  
  1. `include_vars` is not allowed in Playbooks
  2. `var_files` (what would work in Playbooks) will be over written by role vars
  
  ---
  
  **Question:** how do you do solve this situation? Maybe I want something odd and there is a much simpler way?
  
  **Tags:** ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-12-13. Score: 2
  
  Variables have different priorities, see [vars precedence](http://docs.ansible.com/ansible/latest/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable).
  
  As of Ansible 2.x:
  
  > role defaults  
  > inventory file or script group vars  
  > inventory group\_vars/all  
  > playbook group\_vars/all  
  > inventory group\_vars/\*  
  > playbook group\_vars/\*  
  > inventory file or script host vars  
  > inventory host\_vars/\*  
  > playbook host\_vars/\*  
  > host facts  
  > play vars  
  > play vars\_prompt  
  > play vars\_files  
  > role vars (defined in role/vars/main.yml)  
  > block vars (only for tasks in block)  
  > task vars (only for the task)  
  > role (and include\_role) params  
  > include params  
  > include\_vars  
  > set\_facts / registered vars  
  > extra vars (always win precedence)
  
  For roles you would generally use `myrole/defaults/main.yml` for easily overridable vars (e.g. installation path or smth) and use `myrole/vars/main.yml` for vars that you don't want to be easily overridden (smth like required packages).
  
  
  ---
  
  ### Answer by username_0 on 2017-12-12. Score: 0
  
  I missed one point which provides the answer: role defaults are the lowest in precedence. So just these vars and file\_vars can overwrite in the playbook.
  
  
  ---
  
  