From 48a308a87cf42cd1a88a808e67772460dcb22ac6 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Thu, 9 Oct 2014 02:47:41 -0400 Subject: [PATCH] Allow both old-style and new-style role dependencies to be valid. Fixes #9173 --- lib/ansible/utils/__init__.py | 38 ++++++++++++++++----- test/units/TestUtils.py | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 8 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 15f387b55ae..952e8537d0b 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -416,14 +416,36 @@ def role_spec_parse(role_spec): def role_yaml_parse(role): - if 'github.com' in role["src"] and 'http' in role["src"] and '+' not in role["src"] and not role["src"].endswith('.tar.gz'): - role["src"] = "git+" + role["src"] - if '+' in role["src"]: - (scm, src) = role["src"].split('+') - role["scm"] = scm - role["src"] = src - if 'name' not in role: - role["name"] = repo_url_to_role_name(role["src"]) + if 'role' in role: + # Old style: {role: "galaxy.role,version,name", other_vars: "here" } + role_info = role_spec_parse(role['role']) + if isinstance(role_info, dict): + # Warning: Slight change in behaviour here. name may be being + # overloaded. Previously, name was only a parameter to the role. + # Now it is both a parameter to the role and the name that + # ansible-galaxy will install under on the local system. + if 'name' in role and 'name' in role_info: + del role_info['name'] + role.update(role_info) + else: + # New style: { src: 'galaxy.role,version,name', other_vars: "here" } + if 'github.com' in role["src"] and 'http' in role["src"] and '+' not in role["src"] and not role["src"].endswith('.tar.gz'): + role["src"] = "git+" + role["src"] + + if '+' in role["src"]: + (scm, src) = role["src"].split('+') + role["scm"] = scm + role["src"] = src + + if 'name' not in role: + role["name"] = repo_url_to_role_name(role["src"]) + + if 'version' not in role: + role['version'] = '' + + if 'scm' not in role: + role['scm'] = None + return role diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index 07b52a9d380..af10a1e0553 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -841,3 +841,67 @@ class TestUtils(unittest.TestCase): for (spec, result) in tests: self.assertEqual(ansible.utils.role_spec_parse(spec), result) + def test_role_yaml_parse(self): + tests = ( + ( + # Old style + { + 'role': 'debops.elasticsearch', + 'name': 'elks' + }, + { + 'role': 'debops.elasticsearch', + 'name': 'elks', + 'scm': None, + 'src': 'debops.elasticsearch', + 'version': '', + } + ), + ( + { + 'role': 'debops.elasticsearch,1.0,elks', + 'my_param': 'foo' + }, + { + 'role': 'debops.elasticsearch,1.0,elks', + 'name': 'elks', + 'scm': None, + 'src': 'debops.elasticsearch', + 'version': '1.0', + 'my_param': 'foo', + } + ), + ( + { + 'role': 'debops.elasticsearch,1.0', + 'my_param': 'foo' + }, + { + 'role': 'debops.elasticsearch,1.0', + 'name': 'debops.elasticsearch', + 'scm': None, + 'src': 'debops.elasticsearch', + 'version': '1.0', + 'my_param': 'foo', + } + ), + # New style + ( + { + 'src': 'debops.elasticsearch', + 'name': 'elks', + 'my_param': 'foo' + }, + { + 'name': 'elks', + 'scm': None, + 'src': 'debops.elasticsearch', + 'version': '', + 'my_param': 'foo' + } + ), + ) + + for (role, result) in tests: + self.assertEqual(ansible.utils.role_yaml_parse(role), result) +