diff --git a/changelogs/fragments/80995-include-all-var-files.yml b/changelogs/fragments/80995-include-all-var-files.yml new file mode 100644 index 00000000000..f1ac72b8970 --- /dev/null +++ b/changelogs/fragments/80995-include-all-var-files.yml @@ -0,0 +1,2 @@ +bugfixes: +- include_vars - fix calculating ``depth`` relative to the root and ensure all files are included (https://github.com/ansible/ansible/issues/80987). diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 14913b5e655..c32e6227dbf 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -5,6 +5,7 @@ from __future__ import annotations from os import path, walk import re +import pathlib import ansible.constants as C from ansible.errors import AnsibleError @@ -181,16 +182,15 @@ class ActionModule(ActionBase): alphabetical order. Do not iterate pass the set depth. The default depth is unlimited. """ - current_depth = 0 sorted_walk = list(walk(self.source_dir, onerror=self._log_walk, followlinks=True)) sorted_walk.sort(key=lambda x: x[0]) for current_root, current_dir, current_files in sorted_walk: - current_depth += 1 - if current_depth <= self.depth or self.depth == 0: - current_files.sort() - yield (current_root, current_files) - else: - break + # Depth 1 is the root, relative_to omits the root + current_depth = len(pathlib.Path(current_root).relative_to(self.source_dir).parts) + 1 + if self.depth != 0 and current_depth > self.depth: + continue + current_files.sort() + yield (current_root, current_files) def _ignore_file(self, filename): """ Return True if a file matches the list of ignore_files. diff --git a/test/integration/targets/include_vars/files/test_depth/sub1/sub11.yml b/test/integration/targets/include_vars/files/test_depth/sub1/sub11.yml new file mode 100644 index 00000000000..9a5ecb80624 --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub1/sub11.yml @@ -0,0 +1 @@ +sub11: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config11.yml b/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config11.yml new file mode 100644 index 00000000000..02c2897980e --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config11.yml @@ -0,0 +1 @@ +config11: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config112.yml b/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config112.yml new file mode 100644 index 00000000000..e8bc9d94184 --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub1/sub11/config112.yml @@ -0,0 +1 @@ +config112: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub1/sub12.yml b/test/integration/targets/include_vars/files/test_depth/sub1/sub12.yml new file mode 100644 index 00000000000..9aff2876a29 --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub1/sub12.yml @@ -0,0 +1 @@ +sub12: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub2/sub21.yml b/test/integration/targets/include_vars/files/test_depth/sub2/sub21.yml new file mode 100644 index 00000000000..1f7c455e9ec --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub2/sub21.yml @@ -0,0 +1 @@ +sub21: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config211.yml b/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config211.yml new file mode 100644 index 00000000000..a5126a7b498 --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config211.yml @@ -0,0 +1 @@ +config211: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config212.yml b/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config212.yml new file mode 100644 index 00000000000..633841df82d --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub2/sub21/config212.yml @@ -0,0 +1 @@ +config212: defined diff --git a/test/integration/targets/include_vars/files/test_depth/sub3/config3.yml b/test/integration/targets/include_vars/files/test_depth/sub3/config3.yml new file mode 100644 index 00000000000..d6a8192d8fb --- /dev/null +++ b/test/integration/targets/include_vars/files/test_depth/sub3/config3.yml @@ -0,0 +1 @@ +config3: defined diff --git a/test/integration/targets/include_vars/tasks/main.yml b/test/integration/targets/include_vars/tasks/main.yml index 62c7118348d..245916fa8b8 100644 --- a/test/integration/targets/include_vars/tasks/main.yml +++ b/test/integration/targets/include_vars/tasks/main.yml @@ -255,3 +255,15 @@ vars: hash1: "{{ role_path }}/test_symlink/symlink/hashes/hash1.yml" hash2: "{{ role_path }}/test_symlink/symlink/hashes/hash2.yml" + +- name: Test include_vars includes everything to the correct depth + ansible.builtin.include_vars: + dir: "{{ role_path }}/files/test_depth" + depth: 3 + name: test_depth_var + register: test_depth + +- assert: + that: + - "test_depth.ansible_included_var_files|length == 8" + - "test_depth_var.keys()|length == 8"