Ansible-KeePass aktualisiert

This commit is contained in:
Michael Grote 2021-01-27 08:41:58 +01:00
parent 5705532e7a
commit e36890f85c
2 changed files with 26 additions and 11 deletions

View File

@ -1,5 +1,11 @@
# ansible_heimserver
## Ansible KeePass Lookup Plugin aktualisieren
```bash
pip install 'pykeepass>3.2.0' --user
mkdir -p ~/.ansible/plugins/lookup && cd "$_"
curl https://raw.githubusercontent.com/viczem/ansible-keepass/master/keepass.py -o ./keepass.py
```
## collections als Dependency
- in meta
```

View File

@ -25,17 +25,18 @@ DOCUMENTATION = """
version_added: '0.2'
short_description: fetch data from KeePass file
description:
- This lookup returns a value of a property of a KeePass entry
- This lookup returns a value of a property of a KeePass entry
- which fetched by given path
options:
_terms:
description:
description:
- first is a path to KeePass entry
- second is a property name of the entry, e.g. username or password
- third (optional property) if true custem_field_property is return
required: True
notes:
- https://github.com/viczem/ansible-keepass
example:
- "{{ lookup('keepass', 'path/to/entry', 'password') }}"
"""
@ -47,14 +48,19 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
if not terms or len(terms) < 2 or len(terms) > 3:
raise AnsibleError('Wrong request format')
if variables is not None:
self._templar.available_variables = variables
variables_for_templating = getattr(self._templar, '_available_variables', {})
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 = self._templar.template(variables_for_templating.get('keepass_dbx', ''), fail_on_undefined=True)
kp_dbx = os.path.realpath(os.path.expanduser(kp_dbx))
if os.path.isfile(kp_dbx):
display.v(u"Keepass: database file %s" % kp_dbx)
@ -62,10 +68,10 @@ class LookupModule(LookupBase):
kp_soc = "%s/ansible-keepass.sock" % tempfile.gettempdir()
if os.path.exists(kp_soc):
display.v(u"Keepass: fetch from socket")
return self._fetch_socket(kp_soc, entry_path, entry_attr)
return self._fetch_socket(kp_soc, entry_path, entry_attr, enable_custom_attr)
kp_psw = variables.get('keepass_psw', '')
kp_key = variables.get('keepass_key')
kp_psw = self._templar.template(variables_for_templating.get('keepass_psw', ''), fail_on_undefined=True)
kp_key = self._templar.template(variables_for_templating.get('keepass_key', ''), fail_on_undefined=True)
display.v(u"Keepass: fetch from kdbx file")
return self._fetch_file(
kp_dbx, str(kp_psw), kp_key, entry_path, entry_attr, enable_custom_attr)
@ -99,12 +105,15 @@ class LookupModule(LookupBase):
except (AttributeError, FileNotFoundError) as e:
raise AnsibleError(e)
def _fetch_socket(self, kp_soc, entry_path, entry_attr):
def _fetch_socket(self, kp_soc, entry_path, entry_attr, enable_custom_attr):
display.vvvv(u"KeePass: try to socket connect")
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(kp_soc)
display.vvvv(u"KeePass: connected")
sock.send(json.dumps({'attr': entry_attr, 'path': entry_path}).encode())
data = {'attr': entry_attr, 'path': entry_path}
if enable_custom_attr:
data['enable_custom_attr'] = True
sock.send(json.dumps(data).encode())
display.vv(u"KeePass: attr: %s in path: %s" % (entry_attr, entry_path))
try:
msg = json.loads(sock.recv(1024).decode())