1# (c) 2014 Michael DeHaan, <michael@ansible.com>
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# Make coding more python3-ish
19from __future__ import (absolute_import, division, print_function)
20__metaclass__ = type
21
22import os
23
24from ansible.errors import AnsibleError, AnsibleParserError
25from ansible.module_utils.six import iteritems, string_types
26from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
27from ansible.playbook.attribute import Attribute, FieldAttribute
28from ansible.playbook.role.definition import RoleDefinition
29from ansible.playbook.role.requirement import RoleRequirement
30from ansible.module_utils._text import to_native
31
32
33__all__ = ['RoleInclude']
34
35
36class RoleInclude(RoleDefinition):
37
38    """
39    A derivative of RoleDefinition, used by playbook code when a role
40    is included for execution in a play.
41    """
42
43    _delegate_to = FieldAttribute(isa='string')
44    _delegate_facts = FieldAttribute(isa='bool', default=False)
45
46    def __init__(self, play=None, role_basedir=None, variable_manager=None, loader=None, collection_list=None):
47        super(RoleInclude, self).__init__(play=play, role_basedir=role_basedir, variable_manager=variable_manager,
48                                          loader=loader, collection_list=collection_list)
49
50    @staticmethod
51    def load(data, play, current_role_path=None, parent_role=None, variable_manager=None, loader=None, collection_list=None):
52
53        if not (isinstance(data, string_types) or isinstance(data, dict) or isinstance(data, AnsibleBaseYAMLObject)):
54            raise AnsibleParserError("Invalid role definition: %s" % to_native(data))
55
56        if isinstance(data, string_types) and ',' in data:
57            raise AnsibleError("Invalid old style role requirement: %s" % data)
58
59        ri = RoleInclude(play=play, role_basedir=current_role_path, variable_manager=variable_manager, loader=loader, collection_list=collection_list)
60        return ri.load_data(data, variable_manager=variable_manager, loader=loader)
61