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

Fix ansible.builtin.include_vars - depth #80995

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/80995-include-all-var-files.yml
Original file line number Diff line number Diff line change
@@ -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).
14 changes: 7 additions & 7 deletions lib/ansible/plugins/action/include_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from os import path, walk
import re
import pathlib

import ansible.constants as C
from ansible.errors import AnsibleError
Expand Down Expand Up @@ -182,16 +183,15 @@ def _traverse_dir_depth(self):
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub11: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config11: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config112: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub12: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub21: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config211: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config212: defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config3: defined
12 changes: 12 additions & 0 deletions test/integration/targets/include_vars/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"