Skip to content

Commit

Permalink
passwordstore: Add missing_subkey parameter
Browse files Browse the repository at this point in the history
Add ability to trigger error or warning when a subkey is missing in pass file.
By default the behavior is unchanged (if subkey is missing, None is returned).
This option can also be sit in ansible.cfg
  • Loading branch information
mluzarreta committed Mar 29, 2024
1 parent b2b8fc3 commit 51b2240
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- passwordstore lookup - add `missing_subkey` parameter defining the behavior of the lookup when a passwordstore subkey is missing
31 changes: 31 additions & 0 deletions plugins/lookup/passwordstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@
type: bool
default: true
version_added: 8.1.0
missing_subkey:
description:
- List of preference about what to do if the password subkey is missing
- If set to V(error), the lookup will error out if the subkey does not exist.
- If set to V(empty) or V(warn), will return a V(none) in case the subkey does not exist.
version_added: 8.5.1
type: str
default: empty
choices:
- error
- warn
- empty
ini:
- section: passwordstore_lookup
key: missing_subkey
notes:
- The lookup supports passing all options as lookup parameters since community.general 6.0.0.
'''
Expand All @@ -147,6 +162,7 @@
[passwordstore_lookup]
lock=readwrite
locktimeout=45s
missing_subkey=warn
tasks.yml: |
---
Expand Down Expand Up @@ -432,6 +448,20 @@ def get_passresult(self):
if self.paramvals['subkey'] in self.passdict:
return self.passdict[self.paramvals['subkey']]
else:
if self.paramvals["missing_subkey"] == "error":
raise AnsibleError(
"passwordstore: subkey {0} for passname {1} not found and missing=error is set".format(
self.paramvals["subkey"], self.passname
)
)

if self.paramvals["missing_subkey"] == "warn":
display.warning(
"passwordstore: subkey {0} for passname {1} not found".format(
self.paramvals["subkey"], self.passname
)
)

return None

@contextmanager
Expand Down Expand Up @@ -481,6 +511,7 @@ def setup(self, variables):
'umask': self.get_option('umask'),
'timestamp': self.get_option('timestamp'),
'preserve': self.get_option('preserve'),
"missing_subkey": self.get_option("missing_subkey"),
}

def run(self, terms, variables, **kwargs):
Expand Down

0 comments on commit 51b2240

Please sign in to comment.