diff --git a/changelogs/fragments/4955-fix-path-detection-for-gopass.yaml b/changelogs/fragments/4955-fix-path-detection-for-gopass.yaml new file mode 100644 index 0000000000..0ea6106664 --- /dev/null +++ b/changelogs/fragments/4955-fix-path-detection-for-gopass.yaml @@ -0,0 +1,2 @@ +bugfixes: + - passwordstore - fix password store path detection for gopass (https://github.com/ansible-collections/community.general/pull/4955). diff --git a/plugins/lookup/passwordstore.py b/plugins/lookup/passwordstore.py index 5823756e35..2f904abdb2 100644 --- a/plugins/lookup/passwordstore.py +++ b/plugins/lookup/passwordstore.py @@ -21,8 +21,14 @@ DOCUMENTATION = ''' description: query key. required: True passwordstore: - description: location of the password store. - default: '~/.password-store' + description: + - Location of the password store. + - 'The value is decided by checking the following in order:' + - If set, this value is used. + - If C(directory) is set, that value will be used. + - If I(backend=pass), then C(~/.password-store) is used. + - If I(backend=gopass), then the C(path) field in C(~/.config/gopass/config.yml) is used, + falling back to C(~/.local/share/gopass/stores/root) if not defined. directory: description: The directory of the password store. env: @@ -428,11 +434,22 @@ class LookupModule(LookupBase): raise AnsibleError("{0} is not a correct value for locktimeout".format(timeout)) unit_to_seconds = {"s": 1, "m": 60, "h": 3600} self.lock_timeout = int(timeout[:-1]) * unit_to_seconds[timeout[-1]] + + directory = variables.get('passwordstore', os.environ.get('PASSWORD_STORE_DIR', None)) + + if directory is None: + if self.backend == 'gopass': + try: + with open(os.path.expanduser('~/.config/gopass/config.yml')) as f: + directory = yaml.safe_load(f)['path'] + except (FileNotFoundError, KeyError, yaml.YAMLError): + directory = os.path.expanduser('~/.local/share/gopass/stores/root') + else: + directory = os.path.expanduser('~/.password-store') + self.paramvals = { 'subkey': 'password', - 'directory': variables.get('passwordstore', os.environ.get( - 'PASSWORD_STORE_DIR', - os.path.expanduser('~/.password-store'))), + 'directory': directory, 'create': False, 'returnall': False, 'overwrite': False,