Skip to content

Instantly share code, notes, and snippets.

@dochang
Last active March 31, 2016 07:13
Show Gist options
  • Save dochang/0f67e3a97aaef40fcd3781142af2e4eb to your computer and use it in GitHub Desktop.
Save dochang/0f67e3a97aaef40fcd3781142af2e4eb to your computer and use it in GitHub Desktop.
# (c) 2016, ZHANG Weiyi <dochang@gmail.com>
#
# This file is NOT part of Ansible
from __future__ import (absolute_import, division, print_function)
__metaclass_ = type
import subprocess
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
ret = []
for term in terms:
'''
http://docs.python.org/2/library/subprocess.html#popen-constructor
The shell argument (which defaults to False) specifies whether to use the
shell as the program to execute. If shell is True, it is recommended to pass
args as a string rather than as a sequence
https://github.com/ansible/ansible/issues/6550
'''
term = str(term)
p = subprocess.Popen("pass show " + term, cwd=self._loader.get_basedir(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
if p.returncode == 0:
ret.append(stdout.decode("utf-8").rstrip())
else:
raise AnsibleError("lookup_plugin.pass(%s) returned %d: %s" % (term, p.returncode, stderr.decode("utf-8")))
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment