Added support for custom field properties

This commit is contained in:
callum 2020-06-22 18:24:26 +01:00
parent 106c13ad0a
commit f6fc6437be
4 changed files with 24 additions and 6 deletions

View File

@ -77,10 +77,12 @@ Define variables you need e.g. in any file in group_vars
ansible_user : "{{ lookup('keepass', 'path/to/entry', 'username') }}"
ansible_become_pass: "{{ lookup('keepass', 'path/to/entry', 'password') }}"
ansible_become_pass: "{{ lookup('keepass', 'path/to/entry', 'custom_field_property', true) }}"
You can get another [properties of an KeePass entry](https://github.com/pschmitt/pykeepass/blob/master/pykeepass/entry.py)
(not only `username` or `password`)
Specify a boolean value of true to use custom field properties
`ansible-doc -t lookup keepass` - to get description of the plugin

View File

@ -7,10 +7,14 @@
spam_password: "{{ lookup('keepass', 'spam', 'password')}}"
ham_login: "{{ lookup('keepass', 'example/ham', 'username')}}"
ham_password: "{{ lookup('keepass', 'example/ham', 'password')}}"
pork_custom_property: "{{ lookup('keepass', 'example/pork', 'pork_custom_propertys', true)}}"
tasks:
- debug:
msg: "fetch group: / username: {{ spam_login }} password: {{ spam_password }}"
- debug:
msg: "fetch group: /example/ham username: {{ ham_login }} password: {{ ham_password }}"
msg: "fetch group: /example/ham username: {{ ham_login }} password: {{ ham_password }}"
- debug:
msg: "fetch group: /example/pork pork_custom: {{ pork_custom_property }}"

Binary file not shown.

View File

@ -45,11 +45,15 @@ class LookupModule(LookupBase):
keepass = None
def run(self, terms, variables=None, **kwargs):
if not terms or len(terms) != 2:
if not terms or len(terms) < 2 or len(terms) > 3:
raise AnsibleError('Wrong request format')
entry_path = terms[0].strip('/')
entry_attr = terms[1]
enable_custom_attr = False
if len(terms) == 3:
enable_custom_attr = terms[2]
kp_dbx = variables.get('keepass_dbx', '')
kp_dbx = os.path.realpath(os.path.expanduser(kp_dbx))
if os.path.isfile(kp_dbx):
@ -64,9 +68,9 @@ class LookupModule(LookupBase):
kp_key = variables.get('keepass_key')
display.v(u"Keepass: fetch from kdbx file")
return self._fetch_file(
kp_dbx, str(kp_psw), kp_key, entry_path, entry_attr)
kp_dbx, str(kp_psw), kp_key, entry_path, entry_attr, enable_custom_attr)
def _fetch_file(self, kp_dbx, kp_psw, kp_key, entry_path, entry_attr):
def _fetch_file(self, kp_dbx, kp_psw, kp_key, entry_path, entry_attr, enable_custom_attr):
if kp_key:
kp_key = os.path.realpath(os.path.expanduser(kp_key))
if os.path.isfile(kp_key):
@ -81,7 +85,15 @@ class LookupModule(LookupBase):
raise AnsibleError(u"Entry '%s' is not found" % entry_path)
display.vv(
u"KeePass: attr: %s in path: %s" % (entry_attr, entry_path))
return [getattr(entry, entry_attr)]
entry_val = None
if enable_custom_attr:
entry_val = entry.get_custom_property(entry_attr)
if entry_val is not None:
return [entry_val]
else:
raise AnsibleError(AttributeError(u"'No custom field property '%s'" % (entry_attr)))
else:
return [getattr(entry, entry_attr)]
except ChecksumError:
raise AnsibleError("Wrong password/keyfile {}".format(kp_dbx))
except (AttributeError, FileNotFoundError) as e: