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

limit extensions for files in group/host_vars dir #11058

Merged
merged 2 commits into from
May 26, 2015
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
4 changes: 3 additions & 1 deletion lib/ansible/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,9 @@ def _load_vars_from_folder(folder_path, results, vault_password=None):
names.sort()

# do not parse hidden files or dirs, e.g. .svn/
paths = [os.path.join(folder_path, name) for name in names if not name.startswith('.')]
paths = [os.path.join(folder_path, name) for name in names
if not name.startswith('.')
and os.path.splitext(name)[1] in C.YAML_FILENAME_EXTENSIONS]
Copy link
Member

Choose a reason for hiding this comment

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

would -1 not be better as the array index, JIC someone names.their.files.yml this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is os.path.splitext which always returns 2 items. So [1] and [-1] would always be the same.

https://docs.python.org/2/library/os.path.html#os.path.splitext
Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').

In [1]: import os

In [2]: os.path.splitext('names.their.files.yml')
Out[2]: ('names.their.files', '.yml')

for path in paths:
_found, results = _load_vars_from_path(path, results, vault_password=vault_password)
return results
Expand Down
7 changes: 6 additions & 1 deletion test/units/TestInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def dir_inventory(self):
'thrudgelmir0', 'thrudgelmir1', 'thrudgelmir2',
'thrudgelmir3', 'thrudgelmir4', 'thrudgelmir5',
'Hotep-a', 'Hotep-b', 'Hotep-c',
'BastC', 'BastD', 'neptun', ]
'BastC', 'BastD', 'neptun', 'goldorak', ]

#####################################
### Empty inventory format tests
Expand Down Expand Up @@ -222,6 +222,11 @@ def test_subset_filename(self):
inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern'))
self.assertEqual(sorted(inventory.list_hosts()), sorted(['thor','odin']))

def test_vars_yaml_extension(self):
inventory = self.simple_inventory()
vars = inventory.get_variables('goldorak')
assert vars['YAML_FILENAME_EXTENSIONS_TEST']

@raises(errors.AnsibleError)
def testinvalid_entry(self):
Inventory('1234')
Expand Down
2 changes: 2 additions & 0 deletions test/units/inventory_test_data/group_vars/noparse/all.yml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
YAML_FILENAME_EXTENSIONS_TEST: False
2 changes: 2 additions & 0 deletions test/units/inventory_test_data/group_vars/noparse/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
YAML_FILENAME_EXTENSIONS_TEST: False
2 changes: 2 additions & 0 deletions test/units/inventory_test_data/group_vars/parse/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
YAML_FILENAME_EXTENSIONS_TEST: True
6 changes: 6 additions & 0 deletions test/units/inventory_test_data/simple_hosts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ Bast[C:D]

[auth]
neptun auth="YWRtaW46YWRtaW4="

[parse:children]
noparse

[noparse]
goldorak