Debian Codegraph @

lib/ansible/playbook/role/metadata.py

raw
1 # (c) 2014 Michael DeHaan, <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import os
21
22 from ansible.errors import AnsibleParserError, AnsibleError
23 from ansible.playbook.attribute import NonInheritableFieldAttribute
24 from ansible.playbook.base import Base
25 from ansible.playbook.collectionsearch import CollectionSearch
26 from ansible.playbook.helpers import load_list_of_roles
27 from ansible.playbook.role.requirement import RoleRequirement
28
29 __all__ = ['RoleMetadata']
30
31
32 class RoleMetadata(Base, CollectionSearch):
33 """
34 This class wraps the parsing and validation of the optional metadata
35 within each Role (meta/main.yml).
36 """
37
38 allow_duplicates = NonInheritableFieldAttribute(isa='bool', default=False)
39 dependencies = NonInheritableFieldAttribute(isa='list', default=list)
40 galaxy_info = NonInheritableFieldAttribute(isa='dict')
41 argument_specs = NonInheritableFieldAttribute(isa='dict', default=dict)
42
43 def __init__(self, owner=None):
44 self._owner = owner
45 super(RoleMetadata, self).__init__()
46
47 @staticmethod
48 def load(data, owner, variable_manager=None, loader=None):
49 """
50 Returns a new RoleMetadata object based on the datastructure passed in.
51 """
52
53 if not isinstance(data, dict):
54 raise AnsibleParserError("the 'meta/main.yml' for role %s is not a dictionary" % owner.get_name())
55
56 m = RoleMetadata(owner=owner).load_data(data, variable_manager=variable_manager, loader=loader)
57 return m
58
59 def _load_dependencies(self, attr, ds):
60 """
61 This is a helper loading function for the dependencies list,
62 which returns a list of RoleInclude objects
63 """
64
65 roles = []
66 if ds:
67 if not isinstance(ds, list):
68 raise AnsibleParserError("Expected role dependencies to be a list.", obj=self._ds)
69
70 for role_def in ds:
71 # FIXME: consolidate with ansible-galaxy to keep this in sync
72 if isinstance(role_def, str) or 'role' in role_def or 'name' in role_def:
73 roles.append(role_def)
74 continue
75 try:
76 # role_def is new style: { src: 'galaxy.role,version,name', other_vars: "here" }
77 def_parsed = RoleRequirement.role_yaml_parse(role_def)
78 if def_parsed.get('name'):
79 role_def['name'] = def_parsed['name']
80 roles.append(role_def)
81 except AnsibleError as ex:
82 raise AnsibleParserError("Error parsing role dependencies.", obj=role_def) from ex
83
84 current_role_path = None
85 collection_search_list = None
86
87 if self._owner:
88 current_role_path = os.path.dirname(self._owner._role_path)
89
90 # if the calling role has a collections search path defined, consult it
91 collection_search_list = self._owner.collections[:] or []
92
93 # if the calling role is a collection role, ensure that its containing collection is searched first
94 owner_collection = self._owner._role_collection
95 if owner_collection:
96 collection_search_list = [c for c in collection_search_list if c != owner_collection]
97 collection_search_list.insert(0, owner_collection)
98 # ensure fallback role search works
99 if 'ansible.legacy' not in collection_search_list:
100 collection_search_list.append('ansible.legacy')
101
102 try:
103 return load_list_of_roles(roles, play=self._owner._play, current_role_path=current_role_path,
104 variable_manager=self._variable_manager, loader=self._loader,
105 collection_search_list=collection_search_list)
106 except AssertionError as ex:
107 raise AnsibleParserError("A malformed list of role dependencies was encountered.", obj=self._ds) from ex