summaryrefslogtreecommitdiffstats
path: root/lib/action_plugins/fetch_cmd.py
blob: 57d7220322a816e0c14a43c4bc0f551f2d04d1ff (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
# Fetch the output of a remote command
# Copyright (c) 2016 Guilhem Moulin <guilhem@fripost.org>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

import subprocess, os
from ansible.plugins.action import ActionBase
from ansible.utils.path import makedirs_safe
from ansible.utils.hashing import checksum

class ActionModule(ActionBase):
    TRANSFERS_FILES = True

    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        if self._play_context.check_mode:
            return dict(skipped=True, msg='check mode not supported for this module')

        result = super(ActionModule, self).run(tmp, task_vars)

        cmd = self._task.args.get('cmd', None)
        stdin = self._task.args.get('stdin', None)
        dest = self._task.args.get('dest', None)

        if cmd is None or dest is None:
            return dict(failed=True, msg="cmd and dest are required")

        if stdin is not None:
            stdin = self._connection._shell.join_path(stdin)
            stdin = self._remote_expand_user(stdin)

        remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
        stdout = self._connection._shell.join_path(self._make_tmp_path(remote_user), 'stdout')
        result.update(self._execute_module(module_args=dict(cmd=cmd, stdin=stdin, dest=stdout), task_vars=task_vars))

        # calculate checksum for the local file
        local_checksum = checksum(dest)

        # calculate checksum for the remote file, don't bother if using become as slurp will be used
        remote_checksum = self._remote_checksum(stdout, all_vars=task_vars)

        if remote_checksum != local_checksum:
            makedirs_safe(os.path.dirname(dest))
            self._connection.fetch_file(stdout, dest)
            if checksum(dest) == remote_checksum:
                result.update(dict(changed=True))
            else:
                result.update(dict(failed=True))
        return result