Skip to content

Instantly share code, notes, and snippets.

@markuman
Created March 26, 2019 20:18
Show Gist options
  • Save markuman/f958b62863ec997db4181ac9da33ee40 to your computer and use it in GitHub Desktop.
Save markuman/f958b62863ec997db4181ac9da33ee40 to your computer and use it in GitHub Desktop.
import requests
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.lookup import LookupBase
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
# get options
self.set_options(direct=kwargs)
# setup connection
host = self.get_option('host')
user = self.get_option('user')
token = self.get_option('token')
if None in [host, user, token]:
raise AnsibleLookupError('Unable to perform nextcloud passwords lookup. '
'host, user, token and label are required.')
display.debug('nextcloud password lookup term: %s' % term)
display.vvvv(u"nextcloud host %s " % host)
display.vvvv(u"nextcloud user %s " % user)
display.vvvv(u"nextcloud token %s " % token)
r = requests.get(
'https://{HOST}/index.php/apps/passwords/api/1.0/password/find'.format(HOST = host),
auth=(user, token)
)
display.vvvv(u"nextcloud response code is %d " % r.status_code)
ret = []
for term in terms:
try:
if r.status_code == 200:
for item in r.json():
if item['label'] == term:
ret.append(item['password'])
else:
raise AnsibleParserError()
except AnsibleParserError:
raise AnsibleError("nextcloud response with: %d" % r.status_code)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment