You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

84 lines
2.5 KiB

  1. from __future__ import (absolute_import, division, print_function)
  2. __metaclass__ = type
  3. from ansible.errors import AnsibleError, AnsibleParserError
  4. from ansible.plugins.lookup import LookupBase
  5. try:
  6. import requests
  7. except ImportError:
  8. raise AnsibleError("Please install requests library.")
  9. ANSIBLE_METADATA = {
  10. 'metadata_version': '1.1',
  11. 'status': ['preview'],
  12. 'supported_by': 'community'
  13. }
  14. DOCUMENTATION = """
  15. lookup: nextcloud_passwords
  16. author:
  17. - Markus Bergholz <markuman@gmail.com>
  18. version_added: 2.10
  19. short_description: read passwords from nextcloud "passwords" app
  20. description:
  21. - This lookup returns the password stored in nextcloud passwords app based by label
  22. - See U(https://apps.nextcloud.com/apps/passwords)
  23. options:
  24. _terms:
  25. description: Label of the requested password.
  26. required: True
  27. host:
  28. description: host of the requested nextcloud instance (required https)
  29. required: True
  30. user:
  31. description: username of the requested nextcloud passwords user
  32. required: True
  33. token:
  34. description: generated token of the user on the requested nextcloud passwords user
  35. required: True
  36. notes:
  37. - This lookup plugin requires a https connection to the requested nextcloud instance.
  38. """
  39. EXAMPLES = """
  40. - name: Retrieve Password with label "Stackoverflow"
  41. debug:
  42. var: lookup('nextcloud_passwords', 'Stackoverflow' , host='nextcloud.tld', user='ansible', token='some-token')
  43. """
  44. class LookupModule(LookupBase):
  45. def run(self, terms, variables, **kwargs):
  46. # get options
  47. host = kwargs.get('host')
  48. user = kwargs.get('user')
  49. token = kwargs.get('token')
  50. if None in [host, user, token]:
  51. raise AnsibleLookupError('Unable to perform nextcloud passwords lookup. '
  52. 'host, user, token and label are required.')
  53. r = requests.get(
  54. 'https://{HOST}/index.php/apps/passwords/api/1.0/password/list'.format(HOST=host),
  55. auth=(user, token)
  56. )
  57. ret = []
  58. for term in terms:
  59. try:
  60. if r.status_code == 200:
  61. for item in r.json():
  62. if item['label'] == term:
  63. ret.append(item['password'])
  64. else:
  65. raise AnsibleParserError()
  66. except AnsibleParserError:
  67. raise AnsibleError("nextcloud response with status code: %d" % r.status_code)
  68. return ret