Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to understand workflow sequence for Ansible variable execution

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

????

like image 411
Ravi Bhure Avatar asked May 15 '14 11:05

Ravi Bhure


1 Answers

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.

like image 151
Mxx Avatar answered Oct 04 '22 21:10

Mxx