Ansible-KeePass aktualisiert
This commit is contained in:
parent
5705532e7a
commit
e36890f85c
2 changed files with 26 additions and 11 deletions
|
@ -1,5 +1,11 @@
|
||||||
# ansible_heimserver
|
# 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
|
## collections als Dependency
|
||||||
- in meta
|
- in meta
|
||||||
```
|
```
|
||||||
|
|
|
@ -32,6 +32,7 @@ DOCUMENTATION = """
|
||||||
description:
|
description:
|
||||||
- first is a path to KeePass entry
|
- first is a path to KeePass entry
|
||||||
- second is a property name of the entry, e.g. username or password
|
- 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
|
required: True
|
||||||
notes:
|
notes:
|
||||||
- https://github.com/viczem/ansible-keepass
|
- https://github.com/viczem/ansible-keepass
|
||||||
|
@ -47,6 +48,11 @@ class LookupModule(LookupBase):
|
||||||
def run(self, terms, variables=None, **kwargs):
|
def run(self, terms, variables=None, **kwargs):
|
||||||
if not terms or len(terms) < 2 or len(terms) > 3:
|
if not terms or len(terms) < 2 or len(terms) > 3:
|
||||||
raise AnsibleError('Wrong request format')
|
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_path = terms[0].strip('/')
|
||||||
entry_attr = terms[1]
|
entry_attr = terms[1]
|
||||||
enable_custom_attr = False
|
enable_custom_attr = False
|
||||||
|
@ -54,7 +60,7 @@ class LookupModule(LookupBase):
|
||||||
if len(terms) == 3:
|
if len(terms) == 3:
|
||||||
enable_custom_attr = terms[2]
|
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))
|
kp_dbx = os.path.realpath(os.path.expanduser(kp_dbx))
|
||||||
if os.path.isfile(kp_dbx):
|
if os.path.isfile(kp_dbx):
|
||||||
display.v(u"Keepass: database file %s" % 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()
|
kp_soc = "%s/ansible-keepass.sock" % tempfile.gettempdir()
|
||||||
if os.path.exists(kp_soc):
|
if os.path.exists(kp_soc):
|
||||||
display.v(u"Keepass: fetch from socket")
|
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_psw = self._templar.template(variables_for_templating.get('keepass_psw', ''), fail_on_undefined=True)
|
||||||
kp_key = variables.get('keepass_key')
|
kp_key = self._templar.template(variables_for_templating.get('keepass_key', ''), fail_on_undefined=True)
|
||||||
display.v(u"Keepass: fetch from kdbx file")
|
display.v(u"Keepass: fetch from kdbx file")
|
||||||
return self._fetch_file(
|
return self._fetch_file(
|
||||||
kp_dbx, str(kp_psw), kp_key, entry_path, entry_attr, enable_custom_attr)
|
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:
|
except (AttributeError, FileNotFoundError) as e:
|
||||||
raise AnsibleError(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")
|
display.vvvv(u"KeePass: try to socket connect")
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
sock.connect(kp_soc)
|
sock.connect(kp_soc)
|
||||||
display.vvvv(u"KeePass: connected")
|
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))
|
display.vv(u"KeePass: attr: %s in path: %s" % (entry_attr, entry_path))
|
||||||
try:
|
try:
|
||||||
msg = json.loads(sock.recv(1024).decode())
|
msg = json.loads(sock.recv(1024).decode())
|
||||||
|
|
Loading…
Add table
Reference in a new issue