6

Need help to understand workflow sequence for Ansible variable execution in ansible tasks or playbooks,

Which variable used at the end when we use all of these..

1) vars

2) group_vars

3) host_vars

4) extra_vars

5) var_prompt in playbook

6) set_fact in tasks

7) playbooks vars_files

8) playbooks vars

8) roles included vars directory - roles/foo/vars/main.yml

9) role defined vars - { role: foo, param1: 1000, param2: 2000, tags: [ 'foo', 'bar' ] }

10) tasks included include_vars

11) Inventory based variables

????

1
  • If I am using a single same key in all vars with different values. E.g. name: apache name: nginx name: lighttpd name: tomcat name: jetty
    – Ravi Bhure
    May 15, 2014 at 11:37

1 Answer 1

6

Variable Precedence: Where Should I Put A Variable? section of Ansible's docs states the following:

A lot of folks may ask about how variables override another. Ultimately it’s Ansible’s philosophy that it’s better you know where to put a variable, and then you have to think about it a lot less.

Avoid defining the variable “x” in 47 places and then ask the question “which x gets used”. Why? Because that’s not Ansible’s Zen philosophy of doing things.

There is only one Empire State Building. One Mona Lisa, etc. Figure out where to define a variable, and don’t make it complicated.


If multiple variables of the same name are defined in different places, they win in a certain order, which is:

  • -e variables always win
  • then comes "most everything else"
  • then comes 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.

There are a lot more specific examples in the docs link above.

3
  • 1
    thanks I already have the url you provide, and still my question is stick around 'then comes "most everything else"' ???, what I want to know is the very clear statement only for 'who always win' in 'then comes "most everything else"'
    – Ravi Bhure
    May 19, 2014 at 3:38
  • @RaviBhure I do not have information on priority of all 11+ possibilities. However, you are welcome to create a scenario that will tell you exactly that.
    – Mxx
    May 19, 2014 at 6:50
  • Yes @Mxx , I will do that, since I have single variable key used in multiple stages with different values, this can save time and avoid to use different variable files for my multiple OS?datacenter/region/app environment.
    – Ravi Bhure
    May 19, 2014 at 7:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.