Commit 295d8695 authored by Navid Sassan's avatar Navid Sassan
Browse files

adjusted according to pylint

parent 7ff7e0b6
......@@ -7,9 +7,24 @@
# License: The Unlicense, see LICENSE file.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
PYKEEPASS_IMP_ERR = None
try:
from pykeepass import PyKeePass
import pykeepass.exceptions
except ImportError:
PYKEEPASS_IMP_ERR = traceback.format_exc()
pykeepass_found = False
else:
pykeepass_found = True
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
......@@ -121,22 +136,6 @@ password:
description: The generated or retrieved password
type: str
'''
import traceback
from ansible.module_utils.basic import AnsibleModule,missing_required_lib
PYKEEPASS_IMP_ERR = None
try:
from pykeepass import PyKeePass
import pykeepass.exceptions
except ImportError:
PYKEEPASS_IMP_ERR = traceback.format_exc()
pykeepass_found = False
else:
pykeepass_found = True
import subprocess
import argparse
def main():
# define available arguments/parameters a user can pass to the module
......@@ -201,7 +200,7 @@ def main():
# try to get the entry from the database
entry = get_password(module, kp, hostname, username, purpose)
entry = get_password(kp, hostname, purpose)
if entry:
entry_username, entry_password = entry
if entry_username == username:
......@@ -210,10 +209,10 @@ def main():
module.exit_json(**result)
# if there is no matching entry, create a new one
password = generate_password(module, password_length)
password = generate_password(password_length)
if not module.check_mode:
try:
set_password(module, kp, hostname, username, purpose, password)
set_password(kp, hostname, username, purpose, password)
except:
KEEPASS_SAVE_ERR = traceback.format_exc()
module.fail_json(msg='Could not add the entry or save the database.', exception=KEEPASS_SAVE_ERR)
......@@ -227,7 +226,7 @@ def main():
module.exit_json(**result)
def generate_password(module, length):
def generate_password(length):
import string
alphabet = string.ascii_letters + string.digits
try:
......@@ -235,11 +234,11 @@ def generate_password(module, length):
except ImportError:
import random
password = ''.join(random.choice(alphabet) for i in range(length))
password = ''.join(random.choice(alphabet) for _ in range(length))
return password
def set_password(module, kp, hostname, username, purpose, password):
def set_password(kp, hostname, username, purpose, password):
if hostname:
kp.add_entry(kp.root_group, hostname + ' - ' + purpose, username, password, icon='47', notes='Generated by ansible.')
else:
......@@ -248,16 +247,15 @@ def set_password(module, kp, hostname, username, purpose, password):
kp.save()
def get_password(module, kp, hostname, username, purpose):
def get_password(kp, hostname, purpose):
if hostname:
entry = kp.find_entries(title=hostname + ' - ' + purpose, first=True)
else:
entry = kp.find_entries(title=purpose, first=True)
if (entry):
if entry:
return (entry.username, entry.password)
else:
return None
return None
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment