Debian Codegraph @

lib/ansible/playbook/playbook_include.py

raw
1 # (c) 2012-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.module_utils.common.text.converters import to_bytes
23 from ansible.playbook.attribute import NonInheritableFieldAttribute
24 from ansible.playbook.base import Base
25 from ansible.playbook.conditional import Conditional
26 from ansible.playbook.taggable import Taggable
27 from ansible.utils.collection_loader import AnsibleCollectionConfig
28 from ansible.utils.collection_loader._collection_finder import _get_collection_name_from_path, _get_collection_playbook_path
29 from ansible._internal._templating._engine import TemplateEngine
30 from ansible.errors import AnsibleError
31 from ansible import constants as C
32
33
34 class PlaybookInclude(Base, Conditional, Taggable):
35
36 import_playbook = NonInheritableFieldAttribute(isa='string', required=True)
37
38 _post_validate_object = True # manually post_validate to get free arg validation/coercion
39
40 def preprocess_data(self, ds):
41 keys = {action for action in C._ACTION_IMPORT_PLAYBOOK if action in ds}
42
43 if len(keys) != 1:
44 raise AnsibleError(f'Found conflicting import_playbook actions: {", ".join(sorted(keys))}')
45
46 key = next(iter(keys))
47
48 ds['import_playbook'] = ds.pop(key)
49
50 return ds
51
52 @staticmethod
53 def load(data, basedir, variable_manager=None, loader=None):
54 return PlaybookInclude().load_data(ds=data, basedir=basedir, variable_manager=variable_manager, loader=loader)
55
56 def load_data(self, ds, variable_manager=None, loader=None, basedir=None):
57 """
58 Overrides the base load_data(), as we're actually going to return a new
59 Playbook() object rather than a PlaybookInclude object
60 """
61
62 # import here to avoid a dependency loop
63 from ansible.playbook import Playbook
64 from ansible.playbook.play import Play
65
66 # first, we use the original parent method to correctly load the object
67 # via the load_data/preprocess_data system we normally use for other
68 # playbook objects
69 new_obj = super(PlaybookInclude, self).load_data(ds, variable_manager, loader)
70
71 all_vars = self.vars.copy()
72
73 if variable_manager:
74 all_vars |= variable_manager.get_vars()
75
76 templar = TemplateEngine(loader=loader, variables=all_vars)
77
78 new_obj.post_validate(templar)
79
80 # then we use the object to load a Playbook
81 pb = Playbook(loader=loader)
82
83 file_name = new_obj.import_playbook
84
85 # check for FQCN
86 resource = _get_collection_playbook_path(file_name)
87
88 if resource is not None:
89 playbook = resource[1]
90 playbook_collection = resource[2]
91 else:
92 # not FQCN try path
93 playbook = file_name
94 if not os.path.isabs(playbook):
95 playbook = os.path.join(basedir, playbook)
96
97 # might still be collection playbook
98 playbook_collection = _get_collection_name_from_path(playbook)
99
100 if playbook_collection:
101 # it is a collection playbook, setup default collections
102 AnsibleCollectionConfig.default_collection = playbook_collection
103 else:
104 # it is NOT a collection playbook, setup adjacent paths
105 AnsibleCollectionConfig.playbook_paths.append(os.path.dirname(os.path.abspath(to_bytes(playbook, errors='surrogate_or_strict'))))
106 # broken, see: https://github.com/ansible/ansible/issues/85357
107
108 pb._load_playbook_data(file_name=playbook, variable_manager=variable_manager, vars=self.vars.copy())
109
110 # finally, update each loaded playbook entry with any variables specified
111 # on the included playbook and/or any tags which may have been set
112 for entry in pb._entries:
113
114 # conditional includes on a playbook need a marker to skip gathering
115 if new_obj.when and isinstance(entry, Play):
116 entry._included_conditional = new_obj.when[:]
117
118 temp_vars = entry.vars | new_obj.vars
119 param_tags = temp_vars.pop('tags', None)
120 if param_tags is not None:
121 entry.tags.extend(param_tags.split(','))
122 entry.vars = temp_vars
123 entry.tags = list(set(entry.tags).union(new_obj.tags))
124 if entry._included_path is None:
125 entry._included_path = os.path.dirname(playbook)
126
127 # Check to see if we need to forward the conditionals on to the included
128 # plays. If so, we can take a shortcut here and simply prepend them to
129 # those attached to each block (if any)
130 if new_obj.when:
131 for task_block in (entry.pre_tasks + entry.roles + entry.tasks + entry.post_tasks):
132 task_block._when = new_obj.when[:] + task_block.when[:]
133
134 return pb