summaryrefslogtreecommitdiff
path: root/v2/ansible/playbook/become.py
blob: daa8c80ba943acd3314b1b98c4eb637351a5ce6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# 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/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.playbook.attribute import Attribute, FieldAttribute
#from ansible.utils.display import deprecated

class Become:

    # Privlege escalation
    _become              = FieldAttribute(isa='bool', default=False)
    _become_method       = FieldAttribute(isa='string')
    _become_user         = FieldAttribute(isa='string')
    _become_pass         = FieldAttribute(isa='string')

    def __init__(self):
        return super(Become, self).__init__()

    def _detect_privilege_escalation_conflict(self, ds):

        # Fail out if user specifies conflicting privilege escalations
        has_become = 'become' in ds or 'become_user'in ds
        has_sudo   = 'sudo' in ds or 'sudo_user' in ds
        has_su     = 'su' in ds or 'su_user' in ds

        if has_become:
            msg = 'The become params ("become", "become_user") and'
            if has_sudo:
                raise AnsibleParserError('%s sudo params ("sudo", "sudo_user") cannot be used together' % msg)
            elif has_su:
                raise AnsibleParserError('%s su params ("su", "su_user") cannot be used together' % msg)
        elif has_sudo and has_su:
            raise AnsibleParserError('sudo params ("sudo", "sudo_user") and su params ("su", "su_user") cannot be used together')

    def _preprocess_data_become(self, ds):
        """Preprocess the playbook data for become attributes

        This is called from the Base object's preprocess_data() method which
        in turn is called pretty much anytime any sort of playbook object
        (plays, tasks, blocks, etc) are created.
        """

        self._detect_privilege_escalation_conflict(ds)

        # Setting user implies setting become/sudo/su to true
        if 'become_user' in ds and not ds.get('become', False):
            ds['become'] = True

        # Privilege escalation, backwards compatibility for sudo/su
        if 'sudo' in ds or 'sudo_user' in ds:
            ds['become_method'] = 'sudo'
            if 'sudo' in ds:
                ds['become'] = ds['sudo']
                del ds['sudo']
            else:
                ds['become'] = True
            if 'sudo_user' in ds:
                ds['become_user'] = ds['sudo_user']
                del ds['sudo_user']

            #deprecated("Instead of sudo/sudo_user, use become/become_user and set become_method to 'sudo' (default)")

        elif 'su' in ds or 'su_user' in ds:
            ds['become_method'] = 'su'
            if 'su' in ds:
                ds['become'] = ds['su']
                del ds['su']
            else:
                ds['become'] = True
            if 'su_user' in ds:
                ds['become_user'] = ds['su_user']
                del ds['su_user']

            #deprecated("Instead of su/su_user, use become/become_user and set become_method to 'su' (default is sudo)")

        # if we are becoming someone else, but some fields are unset,
        # make sure they're initialized to the default config values
        if ds.get('become', False):
            if ds.get('become_method', None) is None:
                ds['become_method'] = C.DEFAULT_BECOME_METHOD
            if ds.get('become_user', None) is None:
                ds['become_user'] = C.DEFAULT_BECOME_USER

        return ds

    def _get_attr_become(self):
        '''
        Override for the 'become' getattr fetcher, used from Base.
        '''
        if hasattr(self, '_get_parent_attribute'):
            return self._get_parent_attribute('become')
        else:
            return self._attributes['become']

    def _get_attr_become_method(self):
        '''
        Override for the 'become_method' getattr fetcher, used from Base.
        '''
        if hasattr(self, '_get_parent_attribute'):
            return self._get_parent_attribute('become_method')
        else:
            return self._attributes['become_method']

    def _get_attr_become_user(self):
        '''
        Override for the 'become_user' getattr fetcher, used from Base.
        '''
        if hasattr(self, '_get_parent_attribute'):
            return self._get_parent_attribute('become_user')
        else:
            return self._attributes['become_user']

    def _get_attr_become_password(self):
        '''
        Override for the 'become_password' getattr fetcher, used from Base.
        '''
        if hasattr(self, '_get_parent_attribute'):
            return self._get_parent_attribute('become_password')
        else:
            return self._attributes['become_password']