2014-01-04 19:31:44 +01:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 05:26:16 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# This file is part of Ansible
|
2012-02-24 05:26:16 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2012-02-24 05:26:16 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
import os
|
2015-02-13 02:58:30 +01:00
|
|
|
|
2016-11-21 16:02:03 +01:00
|
|
|
from ansible import constants as C
|
2015-10-26 22:23:09 +01:00
|
|
|
from ansible.errors import AnsibleParserError
|
2016-11-21 16:02:03 +01:00
|
|
|
from ansible.module_utils._text import to_text
|
2015-05-04 04:47:26 +02:00
|
|
|
from ansible.playbook.play import Play
|
|
|
|
from ansible.playbook.playbook_include import PlaybookInclude
|
2015-08-04 18:10:23 +02:00
|
|
|
from ansible.plugins import get_all_plugin_loaders
|
2015-02-13 02:58:30 +01:00
|
|
|
|
2015-08-04 21:24:28 +02:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2014-07-03 03:02:28 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
__all__ = ['Playbook']
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
class Playbook:
|
2012-08-18 15:52:13 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def __init__(self, loader):
|
|
|
|
# Entries in the datastructure of a playbook may
|
|
|
|
# be either a play or an include statement
|
|
|
|
self._entries = []
|
2016-11-21 16:02:03 +01:00
|
|
|
self._basedir = to_text(os.getcwd(), errors='surrogate_or_strict')
|
2015-05-04 04:47:26 +02:00
|
|
|
self._loader = loader
|
2015-12-02 18:29:51 +01:00
|
|
|
self._file_name = None
|
2013-11-27 11:59:12 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
@staticmethod
|
|
|
|
def load(file_name, variable_manager=None, loader=None):
|
|
|
|
pb = Playbook(loader=loader)
|
|
|
|
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
|
|
|
|
return pb
|
2013-11-27 11:59:12 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def _load_playbook_data(self, file_name, variable_manager):
|
2013-11-27 11:59:12 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
if os.path.isabs(file_name):
|
|
|
|
self._basedir = os.path.dirname(file_name)
|
2012-08-18 15:52:13 +02:00
|
|
|
else:
|
2015-05-04 04:47:26 +02:00
|
|
|
self._basedir = os.path.normpath(os.path.join(self._basedir, os.path.dirname(file_name)))
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# set the loaders basedir
|
2016-05-23 20:53:18 +02:00
|
|
|
cur_basedir = self._loader.get_basedir()
|
2015-05-04 04:47:26 +02:00
|
|
|
self._loader.set_basedir(self._basedir)
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2015-12-02 18:29:51 +01:00
|
|
|
self._file_name = file_name
|
|
|
|
|
2015-08-05 11:29:46 +02:00
|
|
|
# dynamically load any plugins from the playbook directory
|
2015-08-04 18:10:23 +02:00
|
|
|
for name, obj in get_all_plugin_loaders():
|
|
|
|
if obj.subdir:
|
|
|
|
plugin_path = os.path.join(self._basedir, obj.subdir)
|
|
|
|
if os.path.isdir(plugin_path):
|
|
|
|
obj.add_directory(plugin_path)
|
2013-09-07 07:37:17 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
ds = self._loader.load_from_file(os.path.basename(file_name))
|
|
|
|
if not isinstance(ds, list):
|
2016-05-23 20:53:18 +02:00
|
|
|
# restore the basedir in case this error is caught and handled
|
|
|
|
self._loader.set_basedir(cur_basedir)
|
2015-05-04 04:47:26 +02:00
|
|
|
raise AnsibleParserError("playbooks must be a list of plays", obj=ds)
|
2013-11-07 19:54:06 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
# Parse the playbook entries. For plays, we simply parse them
|
|
|
|
# using the Play() object, and includes are parsed using the
|
|
|
|
# PlaybookInclude() object
|
|
|
|
for entry in ds:
|
|
|
|
if not isinstance(entry, dict):
|
2016-05-23 20:53:18 +02:00
|
|
|
# restore the basedir in case this error is caught and handled
|
|
|
|
self._loader.set_basedir(cur_basedir)
|
2015-05-04 04:47:26 +02:00
|
|
|
raise AnsibleParserError("playbook entries must be either a valid play or an include statement", obj=entry)
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
if 'include' in entry:
|
|
|
|
pb = PlaybookInclude.load(entry, basedir=self._basedir, variable_manager=variable_manager, loader=self._loader)
|
2015-08-04 21:24:28 +02:00
|
|
|
if pb is not None:
|
|
|
|
self._entries.extend(pb._entries)
|
|
|
|
else:
|
2015-12-29 21:41:00 +01:00
|
|
|
display.display("skipping playbook include '%s' due to conditional test failure" % entry.get('include', entry), color=C.COLOR_SKIP)
|
2014-03-25 18:32:11 +01:00
|
|
|
else:
|
2015-05-04 04:47:26 +02:00
|
|
|
entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
|
|
|
|
self._entries.append(entry_obj)
|
2014-03-25 18:32:11 +01:00
|
|
|
|
2016-05-23 20:53:18 +02:00
|
|
|
# we're done, so restore the old basedir in the loader
|
|
|
|
self._loader.set_basedir(cur_basedir)
|
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def get_loader(self):
|
|
|
|
return self._loader
|
2014-03-24 15:28:48 +01:00
|
|
|
|
2015-05-04 04:47:26 +02:00
|
|
|
def get_plays(self):
|
|
|
|
return self._entries[:]
|