mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-15 17:40:50 -07:00
Allow loading dirs from role defaults/vars (#36357)
This commit moves code to look for vars files/dirs to a common place and uses it for loading role defaults/vars. This allows things such as 'defaults/main' or 'vars/main' being a directory in a role, allowing splitting of defaults/vars into multiple files. This commit also fixes the role loading unit tests for py3 when bytestrings are used for paths instead of utf8 strings. This fixes #14248 and #11639.
This commit is contained in:
parent
cc250156c4
commit
95ce00ff00
5 changed files with 151 additions and 84 deletions
|
@ -187,6 +187,68 @@ class TestRole(unittest.TestCase):
|
|||
self.assertEqual(r._default_vars, dict(foo='bar'))
|
||||
self.assertEqual(r._role_vars, dict(foo='bam'))
|
||||
|
||||
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
||||
def test_load_role_with_vars_dirs(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo_vars/defaults/main/foo.yml": """
|
||||
foo: bar
|
||||
""",
|
||||
"/etc/ansible/roles/foo_vars/vars/main/bar.yml": """
|
||||
foo: bam
|
||||
""",
|
||||
})
|
||||
|
||||
mock_play = MagicMock()
|
||||
mock_play.ROLE_CACHE = {}
|
||||
|
||||
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
||||
r = Role.load(i, play=mock_play)
|
||||
|
||||
self.assertEqual(r._default_vars, dict(foo='bar'))
|
||||
self.assertEqual(r._role_vars, dict(foo='bam'))
|
||||
|
||||
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
||||
def test_load_role_with_vars_nested_dirs(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo_vars/defaults/main/foo/bar.yml": """
|
||||
foo: bar
|
||||
""",
|
||||
"/etc/ansible/roles/foo_vars/vars/main/bar/foo.yml": """
|
||||
foo: bam
|
||||
""",
|
||||
})
|
||||
|
||||
mock_play = MagicMock()
|
||||
mock_play.ROLE_CACHE = {}
|
||||
|
||||
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
||||
r = Role.load(i, play=mock_play)
|
||||
|
||||
self.assertEqual(r._default_vars, dict(foo='bar'))
|
||||
self.assertEqual(r._role_vars, dict(foo='bam'))
|
||||
|
||||
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
||||
def test_load_role_with_vars_dir_vs_file(self):
|
||||
|
||||
fake_loader = DictDataLoader({
|
||||
"/etc/ansible/roles/foo_vars/vars/main/foo.yml": """
|
||||
foo: bar
|
||||
""",
|
||||
"/etc/ansible/roles/foo_vars/vars/main.yml": """
|
||||
foo: bam
|
||||
""",
|
||||
})
|
||||
|
||||
mock_play = MagicMock()
|
||||
mock_play.ROLE_CACHE = {}
|
||||
|
||||
i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader)
|
||||
r = Role.load(i, play=mock_play)
|
||||
|
||||
self.assertEqual(r._role_vars, dict(foo='bam'))
|
||||
|
||||
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
||||
def test_load_role_with_metadata(self):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue