Skip to content

Commit 33e912e

Browse files
committed
rewrite precedence rules for more clarify
1 parent 40cd7c3 commit 33e912e

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

docs/docsite/rst/playbook_guide/playbooks_variables.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ You can use the `set_fact` module to combine lists into a new `merged_list` vari
199199
- peach
200200
- plum
201201
- pear
202-
202+
203203
tasks:
204204
- name: Combine list1 and list2 into a merged_list var
205205
ansible.builtin.set_fact:
@@ -425,16 +425,17 @@ Ansible does apply variable precedence, and you might have a use for it. Here is
425425

426426
#. command line values (for example, ``-u my_user``, these are not variables)
427427
#. role defaults (as defined in :ref:`Role directory structure <role_directory_structure>`) [1]_
428-
#. inventory file or script group vars [2]_
429-
#. inventory group_vars/all [3]_
430-
#. playbook group_vars/all [3]_
431-
#. inventory group_vars/* [3]_
432-
#. playbook group_vars/* [3]_
433-
#. inventory file or script host vars [2]_
434-
#. inventory host_vars/* [3]_
435-
#. playbook host_vars/* [3]_
428+
#. variables for group "all" defined inside of an inventory file or script [2]_
429+
#. other group vars defined inside of an inventory file or script [2]_
430+
#. inventory group_vars/all (a file in the directory group_vars/ adjacent to the inventory) [3]_
431+
#. playbook group_vars/all (a file in the directory group_vars/ adjacent to the playbook) [3]_
432+
#. inventory group_vars/* (a file in the directory group_vars/ adjacent to the inventory) [3]_
433+
#. playbook group_vars/* (a file in the directory group_vars/ adjacent to the playbook) [3]_
434+
#. host vars defined in an inventory file or script [2]_
435+
#. inventory host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_
436+
#. playbook host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_
436437
#. host facts / cached set_facts [4]_
437-
#. play vars
438+
#. play vars (defined in the section vars for the play)
438439
#. play vars_prompt
439440
#. play vars_files
440441
#. role vars (as defined in :ref:`Role directory structure <role_directory_structure>`)
@@ -446,6 +447,8 @@ Ansible does apply variable precedence, and you might have a use for it. Here is
446447
#. include params
447448
#. extra vars (for example, ``-e "user=my_user"``)(always win precedence)
448449

450+
If inventory file is located in the same directory as a playbook, adjacent group_vars/ are interpreted twice both as inventory group_vars/ and playbook group_vars/, therefore, getting precedence of the playbook group_vars.
451+
449452
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.
450453

451454
Ansible merges different variables set in inventory so that more specific settings override more generic settings. For example, ``ansible_ssh_user`` specified as a group_var is overridden by ``ansible_user`` specified as a host_var. For details about the precedence of variables set in inventory, see :ref:`how_we_merge`.

0 commit comments

Comments
 (0)