PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/ansible/runner/lookup_plugins/pipe.py

https://github.com/ajanthanm/ansible
Python | 52 lines | 19 code | 8 blank | 25 comment | 4 complexity | 172db9225db4afcc2fa0495814a44a83 MD5 | raw file
 1# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.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
18import subprocess
19from ansible import utils, errors
20
21class LookupModule(object):
22
23    def __init__(self, basedir=None, **kwargs):
24        self.basedir = basedir
25
26    def run(self, terms, inject=None, **kwargs):
27
28        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 
29
30        if isinstance(terms, basestring):
31            terms = [ terms ] 
32
33        ret = []
34        for term in terms:
35            '''
36            http://docs.python.org/2/library/subprocess.html#popen-constructor
37
38            The shell argument (which defaults to False) specifies whether to use the 
39            shell as the program to execute. If shell is True, it is recommended to pass 
40            args as a string rather than as a sequence
41
42            https://github.com/ansible/ansible/issues/6550
43            '''
44            term = str(term)
45
46            p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
47            (stdout, stderr) = p.communicate()
48            if p.returncode == 0:
49                ret.append(stdout.decode("utf-8").rstrip())
50            else:
51                raise errors.AnsibleError("lookup_plugin.pipe(%s) returned %d" % (term, p.returncode))
52        return ret