Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite precedence rules for more clarify #2112

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from

Conversation

amarao
Copy link

@amarao amarao commented Nov 6, 2024

I found existing explanation of precedence rules a bit confusing and not giving enough information.

  1. I added one more layer of precedence, 'all' vars looses to 'group-specific' variables within inventory file or script.
- #. inventory file or script group vars [2]_
+ #. 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]_

This is true and it wasn't reflected in the documentation.

Proof: For an inventory inv.yaml:

---
testgroup:
  hosts:
    test:
  vars:
    foo: testgroup

all:
  vars:
    foo: allgroup

Result is:

ansible -i inv.yaml -c local -m debug -a var=foo all
test | SUCCESS => {
    "foo": "testgroup"
}
  1. Clarify that inventory group_vars/all is about files (notation group_vars/all is ambiguous and can be interpreted as group_vars.all within file, also). It also allow to understand better difference between playbook and inventory group vars.
-  #. inventory group_vars/all [3]_
-  #. playbook group_vars/all [3]_
-  #. inventory group_vars/* [3]_
-  #. playbook group_vars/* [3]_
+  #. 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]_
  1. Clarification for hostvars inside an inventory file.
-  #. inventory file or script host vars [2]_
+  #. host vars defined in an inventory file or script [2]_
  1. Clarification for host_vars (same as for 2)
-  #. inventory host_vars/* [3]_
-  #. playbook host_vars/* [3]_
+  #. 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]_
  1. Explain 'play vars'
-  #. play vars
+  #. play vars (defined in the section vars for the play)
  1. An additional clarification for the special (but very popular) case, when host_group_vars plugin scan the same group_vars/ twice, this happens when an inventory and a playbook are in the same directory, and there is group_vars/ directory there.
+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.
+

There is also change for example yaml, removing trailing white spaces, done by my editor, but I think, it's correct:


    vars:
      list1:
      - apple
      - banana
      - fig
      list2:
      - peach
      - plum
      - pear
-
+    
    tasks:
    - name: Combine list1 and list2 into a merged_list var
      ansible.builtin.set_fact:

@ansible-documentation-bot ansible-documentation-bot bot added the new_contributor This PR is the first contribution by a new community member. label Nov 6, 2024
@ansible-documentation-bot
Copy link
Contributor

Thanks for your Ansible docs contribution! We talk about Ansible documentation on Matrix at #docs:ansible.im if you ever want to join us and chat about the docs! We meet on Matrix every Tuesday. See the Ansible calendar for meeting details. We welcome additions to our weekly agenda items too. You can add the dawgs-meeting tag to a forum topic to bring it up at the next meeting.

@amarao amarao force-pushed the clarify_precedence_rules branch from b76fee7 to 33e912e Compare November 6, 2024 15:38
@SteveRodrigue
Copy link

The changes looks to me. I would argue the problem with the precedence isn't the documentation but the fact it is very complex and convoluted. But this is out of scope.

Maybe the "... adjacent to..." could be defined outside the list to keep the list clean and avoid repetition.

@amarao
Copy link
Author

amarao commented Nov 7, 2024

@SteveRodrigue, I may be slightly odd, but every time I read group_vars/all, I can't understand what it means, there is an ambiguity between file path and logical path in the inventory.

I understand that I bring a lot of verbosity, but I want to make it absolutely non-ambiguous, because of my personal bad experience when I tried to internalize those rules. You read the line, and it describes what it is beyond any doubts.

Copy link
Contributor

@oraNod oraNod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a few suggestions to wording and added backticks for formatting. I know some of the wording was in there before the suggested changes but might as well try and improve that while we're in here.

Thanks for the contribution @amarao

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
If an 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/``. As a result, the ``group_vars/`` from the inventory file get precedence over the playbook ``group_vars``.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, at no point does the inventory file get precedence over the playbook's group_vars.

What happens is that since the file is processed x2, it ends up with the highest precedence, that of the playbook relative file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thank you for suggestion I accept it, but correct for playbook been 'stronger'.

docs/docsite/rst/playbook_guide/playbooks_variables.rst Outdated Show resolved Hide resolved
#. host facts / cached set_facts [4]_
#. play vars
#. play vars (defined in the section vars for the play)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#. play vars (defined in the section vars for the play)
#. play vars (defined in the ``vars`` section for the play)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are also vars_prompt and vars_files

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was added by Don Naro

@oraNod oraNod requested a review from bcoca December 2, 2024 12:48
@oraNod oraNod added the techreview needs technical review label Dec 2, 2024
@bcoca
Copy link
Member

bcoca commented Dec 2, 2024

The text shows precedence by source, there is a separate text for 'group' precedence rules https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#how-variables-are-merged.

A change is needed, but mostly should be about vars plugins vs 'host_vars/group_vars' which are specific to the default vars plugin (host_group_vars) which is shipped with core.

@amarao
Copy link
Author

amarao commented Dec 17, 2024

I understand about rewriting, but within this change, should I change something? @bcoca?

@bcoca
Copy link
Member

bcoca commented Dec 17, 2024

I would remove mentions of 'all' and either change the group_vars/host_vars to instances of vars plugin per group/host or make note that this is the case but using host_group_vars as an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new_contributor This PR is the first contribution by a new community member. techreview needs technical review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants