Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ The UNIX socket file is stored in a temporary folder according to OS.

Requirements: `python 3`, `pykeepass==4.0.3`

pip install 'pykeepass==4.0.3' --user
ansible-galaxy collection install viczem.keepass

```sh
pip install 'pykeepass==4.0.3' --user
ansible-galaxy collection install viczem.keepass
```

## Variables

Expand All @@ -37,14 +38,14 @@ If you want to use ansible-keepass with continuous integration, it could be help
The environment variables will only be used, if no ansible variable is set.

You can than start the socket in another background process like this

```sh
export ANSIBLE_KEEPASS_PSW=mySecret
export ANSIBLE_KEEPASS_SOCKET=/home/build/.my-ansible-sock.${CI_JOB_ID}
export ANSIBLE_TTL=600 # 10 Minutes
/home/build/ansible-pyenv/bin/python3 /home/build/.ansible/roles/ansible_collections/viczem/keepass/plugins/lookup/keepass.py /path-to/my-keepass.kdbx &
ansible-playbook -v playbook1.yml
ansible-playbook -v playbook2.yml

```

## Usage
Expand All @@ -54,33 +55,40 @@ ansible-playbook -v playbook2.yml
> **WARNING**: For security reasons, do not store KeePass passwords in plain text.
Use `ansible-vault encrypt_string` to encrypt it and use it like below

# file: group_vars/all
```yaml
# file: group_vars/all

keepass_dbx: "~/.keepass/database.kdbx"
keepass_psw: !vault |
$ANSIBLE_VAULT;1.1;AES256
...encrypted password...
keepass_dbx: "~/.keepass/database.kdbx"
keepass_psw: !vault |
$ANSIBLE_VAULT;1.1;AES256
...encrypted password...
```

### Examples

More examples see in [/docs/examples](/docs/examples).

#### Lookup

ansible_user : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'username') }}"
ansible_become_pass : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'password') }}"
custom_field : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'custom_properties', 'a_custom_property_name') }}"
attachment : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'attachments', 'a_file_name') }}"
```yaml
ansible_user : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'username') }}"
ansible_become_pass : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'password') }}"
custom_field : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'custom_properties', 'a_custom_property_name') }}"
attachment : "{{ lookup('viczem.keepass.keepass', 'path/to/entry', 'attachments', 'a_file_name') }}"
```

#### Module
- name: "Export file: attachment.txt"
viczem.keepass.attachment:
database: "{{ keepass_dbx }}"
password: "{{ keepass_psw }}"
entrypath: example/attachments
attachment: "attachment.txt"
dest: "{{ keepass_attachment_1_name }}"

```yaml
- name: "Export file: attachment.txt"
viczem.keepass.attachment:
database: "{{ keepass_dbx }}"
password: "{{ keepass_psw }}"
entrypath: example/attachments
attachment: "attachment.txt"
dest: "{{ keepass_attachment_1_name }}"
```

## Contributing

See [/docs/contributing](docs/contributing).
See [/docs/contributing](docs/contributing).
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: viczem
name: keepass

# The version of the collection. Must be compatible with semantic versioning
version: 0.7.5
version: 0.7.6

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
16 changes: 16 additions & 0 deletions plugins/lookup/keepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,25 @@ def run(self, terms, variables=None, **kwargs):
socket_path = _keepass_socket_path(var_dbx)
lock_file_ = socket_path + ".lock"

# Create socket if needed
create_new_socket = False
try:
os.open(lock_file_, os.O_RDWR)
except FileNotFoundError:
display.vvvv("Socket lock file doesn't exist, will create socket")
create_new_socket = True

if not create_new_socket:
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(socket_path)
sock.close()
except ConnectionRefusedError:
display.vvvv("Socket connection refused, recreating")
create_new_socket = True
os.remove(socket_path)

if create_new_socket:
cmd = [
sys.executable,
os.path.abspath(__file__),
Expand Down