From b6ea8de39999ccf67c0afcbeceb27345ab1cbb54 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Sat, 23 May 2015 20:30:08 +0200 Subject: [PATCH 1/2] limit extensions for files in group/host_vars dir inventory vars: make loading from a directory obey the same rules as when checking the base paths, looking at the file name extensions as defined in CONSTANTS.YAML_FILENAME_EXTENSIONS Fixes Github issue #11017 --- lib/ansible/utils/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 476a1e28e81..eb6fa2a712b 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -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] for path in paths: _found, results = _load_vars_from_path(path, results, vault_password=vault_password) return results From b92d70c5b66aa741d35e9f6a294d27f43367205e Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Sat, 23 May 2015 21:37:12 +0200 Subject: [PATCH 2/2] tests files extensions in group/host_vars dir only files with extensions as per C.YAML_FILENAME_EXTENSIONS should be parsed --- test/units/TestInventory.py | 7 ++++++- test/units/inventory_test_data/group_vars/noparse/all.yml~ | 2 ++ test/units/inventory_test_data/group_vars/noparse/file.txt | 2 ++ test/units/inventory_test_data/group_vars/parse/all.yml | 2 ++ test/units/inventory_test_data/simple_hosts | 6 ++++++ 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/units/inventory_test_data/group_vars/noparse/all.yml~ create mode 100644 test/units/inventory_test_data/group_vars/noparse/file.txt create mode 100644 test/units/inventory_test_data/group_vars/parse/all.yml diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index dc3a0ce6d6e..b4bee4300ef 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -56,7 +56,7 @@ class TestInventory(unittest.TestCase): 'thrudgelmir0', 'thrudgelmir1', 'thrudgelmir2', 'thrudgelmir3', 'thrudgelmir4', 'thrudgelmir5', 'Hotep-a', 'Hotep-b', 'Hotep-c', - 'BastC', 'BastD', 'neptun', ] + 'BastC', 'BastD', 'neptun', 'goldorak', ] ##################################### ### Empty inventory format tests @@ -222,6 +222,11 @@ class TestInventory(unittest.TestCase): 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') diff --git a/test/units/inventory_test_data/group_vars/noparse/all.yml~ b/test/units/inventory_test_data/group_vars/noparse/all.yml~ new file mode 100644 index 00000000000..6f52f114b13 --- /dev/null +++ b/test/units/inventory_test_data/group_vars/noparse/all.yml~ @@ -0,0 +1,2 @@ +--- +YAML_FILENAME_EXTENSIONS_TEST: False diff --git a/test/units/inventory_test_data/group_vars/noparse/file.txt b/test/units/inventory_test_data/group_vars/noparse/file.txt new file mode 100644 index 00000000000..6f52f114b13 --- /dev/null +++ b/test/units/inventory_test_data/group_vars/noparse/file.txt @@ -0,0 +1,2 @@ +--- +YAML_FILENAME_EXTENSIONS_TEST: False diff --git a/test/units/inventory_test_data/group_vars/parse/all.yml b/test/units/inventory_test_data/group_vars/parse/all.yml new file mode 100644 index 00000000000..8687c86c7c3 --- /dev/null +++ b/test/units/inventory_test_data/group_vars/parse/all.yml @@ -0,0 +1,2 @@ +--- +YAML_FILENAME_EXTENSIONS_TEST: True diff --git a/test/units/inventory_test_data/simple_hosts b/test/units/inventory_test_data/simple_hosts index 4625b3dbabe..08c62b45376 100644 --- a/test/units/inventory_test_data/simple_hosts +++ b/test/units/inventory_test_data/simple_hosts @@ -20,3 +20,9 @@ Bast[C:D] [auth] neptun auth="YWRtaW46YWRtaW4=" + +[parse:children] +noparse + +[noparse] +goldorak