From 33e912e2fc313dd717a71db352cbe688c51c2530 Mon Sep 17 00:00:00 2001 From: George Shuklin <george.shuklin@gmail.com> Date: Wed, 6 Nov 2024 17:23:49 +0200 Subject: [PATCH 1/2] rewrite precedence rules for more clarify --- .../playbook_guide/playbooks_variables.rst | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/docsite/rst/playbook_guide/playbooks_variables.rst b/docs/docsite/rst/playbook_guide/playbooks_variables.rst index 127fc38ec8c..ef7d254914c 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_variables.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_variables.rst @@ -199,7 +199,7 @@ You can use the `set_fact` module to combine lists into a new `merged_list` vari - peach - plum - pear - + tasks: - name: Combine list1 and list2 into a merged_list var ansible.builtin.set_fact: @@ -425,16 +425,17 @@ Ansible does apply variable precedence, and you might have a use for it. Here is #. command line values (for example, ``-u my_user``, these are not variables) #. role defaults (as defined in :ref:`Role directory structure <role_directory_structure>`) [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]_ + #. variables for group "all" defined inside of an inventory file or script [2]_ + #. other group vars defined inside of an inventory file or script [2]_ + #. inventory group_vars/all (a file in the directory group_vars/ adjacent to the inventory) [3]_ + #. playbook group_vars/all (a file in the directory group_vars/ adjacent to the playbook) [3]_ + #. inventory group_vars/* (a file in the directory group_vars/ adjacent to the inventory) [3]_ + #. playbook group_vars/* (a file in the directory group_vars/ adjacent to the playbook) [3]_ + #. host vars defined in an inventory file or script [2]_ + #. inventory host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_ + #. playbook host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_ #. host facts / cached set_facts [4]_ - #. play vars + #. play vars (defined in the section vars for the play) #. play vars_prompt #. play vars_files #. 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 #. include params #. extra vars (for example, ``-e "user=my_user"``)(always win precedence) +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. + 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. 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`. From 43c60dee28cc125481972acebef734ddd66b9510 Mon Sep 17 00:00:00 2001 From: George Shuklin <george.shuklin@gmail.com> Date: Fri, 6 Dec 2024 10:12:53 +0200 Subject: [PATCH 2/2] Update docs/docsite/rst/playbook_guide/playbooks_variables.rst Co-authored-by: Don Naro <dnaro@redhat.com> --- .../rst/playbook_guide/playbooks_variables.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/docsite/rst/playbook_guide/playbooks_variables.rst b/docs/docsite/rst/playbook_guide/playbooks_variables.rst index ef7d254914c..6defb01f67b 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_variables.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_variables.rst @@ -427,13 +427,13 @@ Ansible does apply variable precedence, and you might have a use for it. Here is #. role defaults (as defined in :ref:`Role directory structure <role_directory_structure>`) [1]_ #. variables for group "all" defined inside of an inventory file or script [2]_ #. other group vars defined inside of an inventory file or script [2]_ - #. inventory group_vars/all (a file in the directory group_vars/ adjacent to the inventory) [3]_ - #. playbook group_vars/all (a file in the directory group_vars/ adjacent to the playbook) [3]_ - #. inventory group_vars/* (a file in the directory group_vars/ adjacent to the inventory) [3]_ - #. playbook group_vars/* (a file in the directory group_vars/ adjacent to the playbook) [3]_ + #. inventory ``group_vars/all`` (a file in the ``group_vars/`` directory that is adjacent to the inventory) [3]_ + #. playbook ``group_vars/all`` (a file in the ``group_vars/`` directory that is adjacent to the playbook) [3]_ + #. inventory ``group_vars/*`` (a file in the ``group_vars/`` directory that is adjacent to the inventory) [3]_ + #. playbook ``group_vars/*`` (a file in the ``group_vars/`` directory that is adjacent to the playbook) [3]_ #. host vars defined in an inventory file or script [2]_ - #. inventory host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_ - #. playbook host_vars/* (a file in the directory host_vars/ adjacent to the inventory) [3]_ + #. inventory ``host_vars/*`` (a file in the ``host_vars/`` directory that is adjacent to the inventory) [3]_ + #. playbook ``host_vars/*`` (a file in the ``host_vars/`` directory that is adjacent to the inventory) [3]_ #. host facts / cached set_facts [4]_ #. play vars (defined in the section vars for the play) #. play vars_prompt