Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions docs/docsite/rst/playbook_guide/playbooks_reuse_roles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Copy link
Contributor

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 means tasks/handlers/defaults/vars.

To include files from outside of tasks/defaults/vars directories, include_tasks/import_tasks/include_vars must be used, not include_role/import_role. Handlers just can't be loaded from other directories since there's no mechanism (though an include_tasks handler in a standard location can run tasks from an arbitrary directory).

Suggested change
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``.
You can add YAML files named something other than ``main.yml`` in ``tasks/``, ``handlers/``, ``defaults/``, and ``vars/``, but they won't be used by default. They can be specified when using ``include_role``/``import_role``. Note that ``include_role`` and ``import_role`` do not support loading from other directories. You can include tasks/variables from arbitrary locations using ``include_tasks``/``import_tasks``/``include_vars``.

Copy link
Author

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 🙂

For example, you can place platform-specific tasks in separate files and refer to them in the ``tasks/main.yml`` file:

Expand Down Expand Up @@ -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
Expand All @@ -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).
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
In this example, no variables are included automatically. Instead, they should be included explicitly (e.g. using the ``vars_from`` in ``import_role`` statements).
In this example, no variables are included automatically. Instead, they should be included explicitly (for example, using the ``vars_from`` argument in an ``import_role`` task).

Copy link
Contributor

Choose a reason for hiding this comment

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

The reporter is using the roles keyword, so I think we should add a note/warning that roles and dependencies cannot configure other entrypoints. (Also related: ansible/ansible#85876)

There's a note in the roles section (below, which links to this section) that says:

vars and defaults can also match to a directory of the same name and Ansible will process all the files contained in that directory. See Role directory structure for more details.

I think it should say "a directory named 'main'", instead.

Copy link
Author

Choose a reason for hiding this comment

The 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 vars_from in roles won't work? Basically also fixing ansible/ansible#85876 in this PR?


.. _role_search_path:

Storing and finding roles
Expand Down