This is an old revision of the document!
Ansible does apply variable precedence, and you might have a use for it. Here is the order of precedence from least to greatest (the last listed variables override all other variables):
command line values (for example, -u my_user, these are not variables)
role defaults (defined in role/defaults/main.yml) 1
inventory file or script group vars 2
inventory group_vars/all 3
playbook group_vars/all 3
inventory group_vars/* 3
playbook group_vars/* 3
inventory file or script host vars 2
inventory host_vars/* 3
playbook host_vars/* 3
host facts / cached set_facts 4
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)
include_vars
set_facts / registered vars
role (and include_role) params
include params
extra vars (for example, -e "user=my_user")(always win precedence)
In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope. Variables in the defaults folder inside a role are easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in the namespace. Host and/or inventory variables override role defaults, but explicit includes such as the vars directory or an include_vars task override inventory variables.
Ex: yml file
- hosts: webservers
vars:
http_port: 80
base_path: http://ansibile.local/wiki
app_path: {{ base_path }}/{{ http_port }}
Facts are variable collected each time you contact a client. It contains IP adresses, OS type, level, hardware….
You can call them on the ansible server by using the command (xml output): <cli prompt='#'> opensuse:~ # ansible -m setup localhost localhost | SUCCESS ⇒ {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.1.3.22"
],
"ansible_all_ipv6_addresses": [
"fe80::209a:12e0:e8a7:8ea"
],
"ansible_apparmor": {
"status": "enabled"
},
"ansible_architecture": "x86_64",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-4.12.14-lp151.28.91-default",
},
"ansible_date_time": {
"date": "2021-09-13",
"day": "13",
"epoch": "1631520866",
"hour": "10",
"minute": "14",
"month": "09",
"second": "26",
"time": "10:14:26",
"tz": "CEST",
"tz_offset": "+0200",
"weekday": "Monday",
"weekday_number": "1",
"weeknumber": "37",
"year": "2021"
},
…
"ansible_distribution": "openSUSE Leap",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/os-release",
"ansible_distribution_file_variety": "SUSE",
"ansible_distribution_major_version": "15",
"ansible_distribution_release": "1",
"ansible_distribution_version": "15.1",