diff --git a/plugins/lookup/passwordstore.py b/plugins/lookup/passwordstore.py index a221e496256..bee30e3b04b 100644 --- a/plugins/lookup/passwordstore.py +++ b/plugins/lookup/passwordstore.py @@ -274,7 +274,8 @@ def parse_params(self, term): if os.path.isdir(self.paramvals['directory']): self.env['PASSWORD_STORE_DIR'] = self.paramvals['directory'] else: - raise AnsibleError('Passwordstore directory \'{0}\' does not exist'.format(self.paramvals['directory'])) + if self.backend == 'pass': + raise AnsibleError('Passwordstore directory \'{0}\' does not exist'.format(self.paramvals['directory'])) # Set PASSWORD_STORE_UMASK if umask is set if 'umask' in self.paramvals: @@ -287,8 +288,11 @@ def parse_params(self, term): def check_pass(self): try: + pass_show = ["pass", "show", self.passname] + if self.backend == 'gopass': + pass_show = ["pass", "show", "--password", self.passname] self.passoutput = to_text( - check_output2(["pass", "show", self.passname], env=self.env), + check_output2(pass_show, env=self.env), errors='surrogate_or_strict' ).splitlines() self.password = self.passoutput[0] @@ -302,9 +306,13 @@ def check_pass(self): if ':' in line: name, value = line.split(':', 1) self.passdict[name.strip()] = value.strip() - if os.path.isfile(os.path.join(self.paramvals['directory'], self.passname + ".gpg")): - # Only accept password as found, if there a .gpg file for it (might be a tree node otherwise) + if self.backend == 'pass': + if os.path.isfile(os.path.join(self.paramvals['directory'], self.passname + ".gpg")): + # Only accept password as found, if there a .gpg file for it (might be a tree node otherwise) + return True + else: return True + except (subprocess.CalledProcessError) as e: # 'not in password store' is the expected error if a password wasn't found if 'not in the password store' not in e.output: @@ -379,6 +387,16 @@ def opt_lock(self, type): else: yield + def setup_backend(self): + try: + pass_version = to_text( + check_output2(['pass', '--version'])) + except (subprocess.CalledProcessError) as e: + raise AnsibleError(e) + self.backend = 'pass' + if 'gopass' in pass_version: + self.backend = 'gopass' + def setup(self, variables): self.locked = None timeout = self.get_option('locktimeout') @@ -403,6 +421,7 @@ def setup(self, variables): def run(self, terms, variables, **kwargs): self.setup(variables) + self.setup_backend() result = [] for term in terms: