Skip to content

Commit

Permalink
Avoid erroring with empty vars files
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Aug 20, 2022
1 parent 1cefe54 commit a0d1951
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def matchplay(self, file: Lintable, data: odict[str, Any]) -> List[MatchError]:
results: List[MatchError] = []
raw_results: List[MatchError] = []

if not data:
return results
# If the Play uses the 'vars' section to set variables
our_vars = data.get("vars", {})
for key in our_vars.keys():
Expand Down Expand Up @@ -161,7 +163,7 @@ def matchyaml(self, file: Lintable) -> List[MatchError]:
raw_results: List[MatchError] = []
meta_data: Dict[AnsibleUnicode, Any] = {}

if str(file.kind) == "vars":
if str(file.kind) == "vars" and file.data:
meta_data = parse_yaml_from_file(str(file.path))
for key in meta_data.keys():
if self.is_invalid_variable_name(key):
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def nested_items_path(
:returns: each iteration yields the key (of the parent dict) or the index (lists)
"""
# As typing and mypy cannot effectively ensure we are called only with
# valid data, we better ignore: NoneType, [], {}, str
if not data_collection or isinstance(data_collection, str):
return
yield from _nested_items_path(data_collection=data_collection, parent_path=[])


Expand Down

0 comments on commit a0d1951

Please sign in to comment.