summaryrefslogtreecommitdiffstats
path: root/lib/modules/fetch_cmd.py
blob: ac8757fa9a90d3521cb457f6c8933057aebfc95b (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
#!/usr/bin/python

# 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 module snippets
from ansible.module_utils.basic import *

def main():
    module = AnsibleModule(
        argument_spec   = dict(
            cmd   = dict( default=None ),
            stdin = dict( default=None ),
            dest  = dict( default=None ),
        ),
        supports_check_mode=False
    )

    params = module.params
    cmd    = params['cmd']
    stdin  = params['stdin']
    dest   = params['dest']

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

    changed = False
    try:
        if stdin is not None:
            stdin = open(stdin, 'r')

        with open(dest, 'w') as stdout:
            subprocess.check_call(cmd.split(), stdin=stdin, stdout=stdout)
            if stdin is not None:
                stdin.close()

    except KeyError, e:
        module.fail_json(msg=str(e))

    module.exit_json(changed=changed)

main()