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