Debian Codegraph @

lib/ansible/playbook/collectionsearch.py

raw
1 # Copyright: (c) 2019, Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 from ansible.playbook.attribute import FieldAttribute
7 from ansible.utils.collection_loader import AnsibleCollectionConfig
8 from ansible.utils.display import Display
9
10 display = Display()
11
12
13 def _ensure_default_collection(collection_list=None):
14 default_collection = AnsibleCollectionConfig.default_collection
15
16 # Will be None when used as the default
17 if collection_list is None:
18 collection_list = []
19
20 # FIXME: exclude role tasks?
21 if default_collection and default_collection not in collection_list:
22 collection_list.insert(0, default_collection)
23
24 # if there's something in the list, ensure that builtin or legacy is always there too
25 if collection_list and 'ansible.builtin' not in collection_list and 'ansible.legacy' not in collection_list:
26 collection_list.append('ansible.legacy')
27
28 return collection_list
29
30
31 class CollectionSearch:
32
33 # this needs to be populated before we can resolve tasks/roles/etc
34 collections = FieldAttribute(isa='list', listof=(str,), priority=100, default=_ensure_default_collection, always_post_validate=True, static=True)
35
36 def _load_collections(self, attr, ds):
37 # We are always a mixin with Base, so we can validate this untemplated
38 # field early on to guarantee we are dealing with a list.
39 ds = self.get_validated_value('collections', self.fattributes.get('collections'), ds, None)
40
41 # this will only be called if someone specified a value; call the shared value
42 _ensure_default_collection(collection_list=ds)
43
44 if not ds: # don't return an empty collection list, just return None
45 return None
46
47 return ds