diff --git a/changelogs/fragments/bitwarden-lookup-performance.yaml b/changelogs/fragments/bitwarden-lookup-performance.yaml new file mode 100644 index 0000000000..cb0405b1cb --- /dev/null +++ b/changelogs/fragments/bitwarden-lookup-performance.yaml @@ -0,0 +1,2 @@ +minor_changes: + - "bitwarden lookup plugin - when looking for items using an item ID, the item is now accessed directly with ``bw get item`` instead of searching through all items. This doubles the lookup speed (https://github.com/ansible-collections/community.general/pull/7468)." diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index d49274c5c9..3756c1fcfe 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -104,6 +104,8 @@ class Bitwarden(object): out, err = p.communicate(to_bytes(stdin)) rc = p.wait() if rc != expected_rc: + if len(args) > 2 and args[0] == 'get' and args[1] == 'item' and b'Not found.' in err: + return 'null', '' raise BitwardenException(err) return to_text(out, errors='surrogate_or_strict'), to_text(err, errors='surrogate_or_strict') @@ -112,7 +114,10 @@ class Bitwarden(object): """ # Prepare set of params for Bitwarden CLI - params = ['list', 'items', '--search', search_value] + if search_field == 'id': + params = ['get', 'item', search_value] + else: + params = ['list', 'items', '--search', search_value] if collection_id: params.extend(['--collectionid', collection_id]) @@ -121,7 +126,11 @@ class Bitwarden(object): # This includes things that matched in different fields. initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] - + if search_field == 'id': + if initial_matches is None: + initial_matches = [] + else: + initial_matches = [initial_matches] # Filter to only include results from the right field. return [item for item in initial_matches if item[search_field] == search_value]