| 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 import constants as C |
| 23 |
from ansible.errors import AnsibleParserError |
| 24 |
from ansible.module_utils.common.text.converters import to_text, to_native |
| 25 |
from ansible.playbook.play import Play |
| 26 |
from ansible.playbook.playbook_include import PlaybookInclude |
| 27 |
from ansible.plugins.loader import add_all_plugin_dirs |
| 28 |
from ansible.utils.display import Display |
| 29 |
from ansible.utils.path import unfrackpath |
| 30 |
|
| 31 |
display = Display() |
| 32 |
|
| 33 |
|
| 34 |
__all__ = ['Playbook'] |
| 35 |
|
| 36 |
|
| 37 |
class Playbook: |
| 38 |
|
| 39 |
def __init__(self, loader): |
| 40 |
# Entries in the datastructure of a playbook may |
| 41 |
# be either a play or an include statement |
| 42 |
self._entries = [] |
| 43 |
self._basedir = to_text(os.getcwd(), errors='surrogate_or_strict') |
| 44 |
self._loader = loader |
| 45 |
self._file_name = None |
| 46 |
|
| 47 |
@staticmethod |
| 48 |
def load(file_name, variable_manager=None, loader=None): |
| 49 |
pb = Playbook(loader=loader) |
| 50 |
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager) |
| 51 |
return pb |
| 52 |
|
| 53 |
def _load_playbook_data(self, file_name, variable_manager, vars=None): |
| 54 |
|
| 55 |
if os.path.isabs(file_name): |
| 56 |
self._basedir = os.path.dirname(file_name) |
| 57 |
else: |
| 58 |
self._basedir = os.path.normpath(os.path.join(self._basedir, os.path.dirname(file_name))) |
| 59 |
|
| 60 |
# set the loaders basedir |
| 61 |
cur_basedir = self._loader.get_basedir() |
| 62 |
self._loader.set_basedir(self._basedir) |
| 63 |
|
| 64 |
add_all_plugin_dirs(self._basedir) |
| 65 |
|
| 66 |
self._file_name = file_name |
| 67 |
|
| 68 |
try: |
| 69 |
ds = self._loader.load_from_file(os.path.basename(file_name), trusted_as_template=True) |
| 70 |
except UnicodeDecodeError as e: |
| 71 |
raise AnsibleParserError("Could not read playbook (%s) due to encoding issues: %s" % (file_name, to_native(e))) |
| 72 |
|
| 73 |
# check for errors and restore the basedir in case this error is caught and handled |
| 74 |
if ds is None: |
| 75 |
self._loader.set_basedir(cur_basedir) |
| 76 |
raise AnsibleParserError("Empty playbook, nothing to do: %s" % unfrackpath(file_name), obj=ds) |
| 77 |
elif not isinstance(ds, list): |
| 78 |
self._loader.set_basedir(cur_basedir) |
| 79 |
raise AnsibleParserError("A playbook must be a list of plays, got a %s instead: %s" % (type(ds), unfrackpath(file_name)), obj=ds) |
| 80 |
elif not ds: |
| 81 |
self._loader.set_basedir(cur_basedir) |
| 82 |
raise AnsibleParserError("A playbook must contain at least one play: %s" % unfrackpath(file_name)) |
| 83 |
|
| 84 |
# Parse the playbook entries. For plays, we simply parse them |
| 85 |
# using the Play() object, and includes are parsed using the |
| 86 |
# PlaybookInclude() object |
| 87 |
for entry in ds: |
| 88 |
if not isinstance(entry, dict): |
| 89 |
# restore the basedir in case this error is caught and handled |
| 90 |
self._loader.set_basedir(cur_basedir) |
| 91 |
raise AnsibleParserError("playbook entries must be either valid plays or 'import_playbook' statements", obj=entry) |
| 92 |
|
| 93 |
if any(action in entry for action in C._ACTION_IMPORT_PLAYBOOK): |
| 94 |
pb = PlaybookInclude.load(entry, basedir=self._basedir, variable_manager=variable_manager, loader=self._loader) |
| 95 |
if pb is not None: |
| 96 |
self._entries.extend(pb._entries) |
| 97 |
else: |
| 98 |
which = entry |
| 99 |
for k in C._ACTION_IMPORT_PLAYBOOK: |
| 100 |
if k in entry: |
| 101 |
which = entry[k] |
| 102 |
break |
| 103 |
display.display("skipping playbook '%s' due to conditional test failure" % which, color=C.COLOR_SKIP) |
| 104 |
else: |
| 105 |
entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader, vars=vars) |
| 106 |
self._entries.append(entry_obj) |
| 107 |
|
| 108 |
# we're done, so restore the old basedir in the loader |
| 109 |
self._loader.set_basedir(cur_basedir) |
| 110 |
|
| 111 |
def get_loader(self): |
| 112 |
return self._loader |
| 113 |
|
| 114 |
def get_plays(self): |
| 115 |
return self._entries[:] |