1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Added ability to limit role dependencies to just one inclusion

This commit is contained in:
James Cammarata 2013-08-30 11:35:35 -05:00
parent 6f6efa268b
commit 736c8b19d3
2 changed files with 25 additions and 6 deletions

View file

@ -568,10 +568,10 @@ Role dependencies can also be specified as a full path, just like top level role
dependencies: dependencies:
- { role: '/path/to/common/roles/foo', x: 1 } - { role: '/path/to/common/roles/foo', x: 1 }
Roles dependencies are always executed before the role that includes them, and are recursive. Roles dependencies are always executed before the role that includes them, and are recursive. By default,
roles can also only be added as a dependency once - if another role also lists it as a dependency it will
Role dependencies may be included more than once. Continuing the above example, the 'car' role could not be run again. This behavior can be overridden by adding `allow_duplicates: yes` to the `meta/main.yml` file.
add 'wheel' dependencies as follows:: For example, a role named 'car' could add a role named 'wheel' to its dependencies as follows::
--- ---
dependencies: dependencies:
@ -580,7 +580,15 @@ add 'wheel' dependencies as follows::
- { role: wheel, n: 3 } - { role: wheel, n: 3 }
- { role: wheel, n: 4 } - { role: wheel, n: 4 }
If the wheel role required tire and brake in turn, this would result in the following execution order:: And the `meta/main.yml` for wheel contained the following::
---
allow_duplicates: yes
dependencies:
- { role: tire }
- { role: brake }
The resulting order of execution would be as follows::
tire(n=1) tire(n=1)
brake(n=1) brake(n=1)

View file

@ -29,7 +29,7 @@ class Play(object):
__slots__ = [ __slots__ = [
'hosts', 'name', 'vars', 'default_vars', 'vars_prompt', 'vars_files', 'hosts', 'name', 'vars', 'default_vars', 'vars_prompt', 'vars_files',
'handlers', 'remote_user', 'remote_port', 'handlers', 'remote_user', 'remote_port', 'included_roles',
'sudo', 'sudo_user', 'transport', 'playbook', 'sudo', 'sudo_user', 'transport', 'playbook',
'tags', 'gather_facts', 'serial', '_ds', '_handlers', '_tasks', 'tags', 'gather_facts', 'serial', '_ds', '_handlers', '_tasks',
'basedir', 'any_errors_fatal', 'roles', 'max_fail_pct' 'basedir', 'any_errors_fatal', 'roles', 'max_fail_pct'
@ -75,6 +75,7 @@ class Play(object):
self._update_vars_files_for_host(None) self._update_vars_files_for_host(None)
# now we load the roles into the datastructure # now we load the roles into the datastructure
self.included_roles = []
ds = self._load_roles(self.roles, ds) ds = self._load_roles(self.roles, ds)
# and finally re-process the vars files as they may have # and finally re-process the vars files as they may have
@ -177,6 +178,16 @@ class Play(object):
dependencies = data.get('dependencies',[]) dependencies = data.get('dependencies',[])
for dep in dependencies: for dep in dependencies:
(dep_path,dep_vars) = self._get_role_path(dep) (dep_path,dep_vars) = self._get_role_path(dep)
meta = self._resolve_main(utils.path_dwim(self.basedir, os.path.join(dep_path, 'meta')))
if os.path.isfile(meta):
meta_data = utils.parse_yaml_from_file(meta)
if meta_data:
allow_dupes = utils.boolean(meta_data.get('allow_duplicates',''))
if not allow_dupes:
if dep.get('role') in self.included_roles:
continue
else:
self.included_roles.append(dep.get('role'))
dep_vars = utils.combine_vars(passed_vars, dep_vars) dep_vars = utils.combine_vars(passed_vars, dep_vars)
dep_vars = utils.combine_vars(role_vars, dep_vars) dep_vars = utils.combine_vars(role_vars, dep_vars)
vars = self._resolve_main(utils.path_dwim(self.basedir, os.path.join(dep_path, 'vars'))) vars = self._resolve_main(utils.path_dwim(self.basedir, os.path.join(dep_path, 'vars')))