-
Notifications
You must be signed in to change notification settings - Fork 732
Clarify when variables and defaults are automatically included in nested directories #3090
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
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -40,7 +40,7 @@ By default, Ansible will look in most role directories for a ``main.yml`` file f | |||||
- On stand alone roles you can also include custom modules and/or plugins, for example ``library/my_module.py``, which may be used within this role (see :ref:`embedding_modules_and_plugins_in_roles` for more information). | ||||||
- A 'stand alone' role refers to role that is not part of a collection but as individually installable content. | ||||||
- Variables from ``vars/`` and ``defaults/`` are imported into play scope unless you disable it via the ``public`` option in ``import_role``/``include_role``. | ||||||
|
||||||
You can add other YAML files in some directories, but they won't be used by default. They can be included/imported directly or specified when using ``include_role/import_role``. | ||||||
For example, you can place platform-specific tasks in separate files and refer to them in the ``tasks/main.yml`` file: | ||||||
|
||||||
|
@@ -79,7 +79,7 @@ Or call those tasks directly when loading the role, which bypasses the ``main.ym | |||||
when: ansible_facts['os_family'] == 'Debian' | ||||||
Directories ``defaults`` and ``vars`` may also include *nested directories*. If your variables file is a directory, Ansible reads all variables files and directories inside in alphabetical order. If a nested directory contains variables files as well as directories, Ansible reads the directories first. Below is an example of a ``vars/main`` directory: | ||||||
Directories ``defaults`` and ``vars`` may also include *nested directories*. If your variables file is a directory, Ansible reads all variables files and directories inside in alphabetical order. If a nested directory contains variables files as well as directories, Ansible reads the directories first. Do note however that directories that are not nested under ``main`` will not be automatically included. Below is an example of a ``vars/main`` directory: | ||||||
|
||||||
.. code-block:: text | ||||||
|
@@ -93,6 +93,24 @@ Directories ``defaults`` and ``vars`` may also include *nested directories*. If | |||||
second_variables_file.yml | ||||||
third_variables_file.yml | ||||||
In this example, variables from all nested directories will automatically be included by Ansible. Below is an example of nested directories where that won't be the case: | ||||||
|
||||||
.. code-block:: text | ||||||
roles/ | ||||||
common/ # this hierarchy represents a "role" | ||||||
vars/ | ||||||
linux/ # <-- variables in a non-main directory -> not automatically read | ||||||
debian/ | ||||||
debian.yml | ||||||
rocky/ | ||||||
rocky.yml | ||||||
windows/ | ||||||
windows.yml | ||||||
In this example, no variables are included automatically. Instead, they should be included explicitly (e.g. using the ``vars_from`` in ``import_role`` statements). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reporter is using the There's a note in the
I think it should say "a directory named 'main'", instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you clarify that first part? Are you suggesting to add a note that explicitly calls out that for instance |
||||||
|
||||||
.. _role_search_path: | ||||||
|
||||||
Storing and finding roles | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can ignore or rephrase this suggestion, it's only tangentially related, but I had to read this line multiple times to understand it.
By 'other YAML files' I think it means non-
main.yml
/main.yaml
/main
files, and by 'some directories' I think it meanstasks
/handlers
/defaults
/vars
.To include files from outside of
tasks
/defaults
/vars
directories,include_tasks
/import_tasks
/include_vars
must be used, notinclude_role
/import_role
. Handlers just can't be loaded from other directories since there's no mechanism (though aninclude_tasks
handler in a standard location can run tasks from an arbitrary directory).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah sorry I didn't necessarily want to refactor this, there were just a bunch of trailing spaces and I figured I might as well remove them. I can definitely rephrase this though if this is something you think would improve this page 🙂