yedit.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pylint: skip-file
  2. def main():
  3. '''
  4. ansible oc module for secrets
  5. '''
  6. module = AnsibleModule(
  7. argument_spec=dict(
  8. state=dict(default='present', type='str',
  9. choices=['present', 'absent', 'list']),
  10. debug=dict(default=False, type='bool'),
  11. src=dict(default=None, type='str'),
  12. content=dict(default=None, type='dict'),
  13. key=dict(default=None, type='str'),
  14. value=dict(default=None, type='str'),
  15. value_format=dict(default='yaml', choices=['yaml', 'json'], type='str'),
  16. ),
  17. #mutually_exclusive=[["src", "content"]],
  18. supports_check_mode=True,
  19. )
  20. state = module.params['state']
  21. yamlfile = Yedit(module.params['src'], module.params['content'])
  22. rval = yamlfile.get()
  23. if not rval and state != 'present':
  24. module.fail_json(msg='Error opening file [%s]. Verify that the' + \
  25. ' file exists, that it is has correct permissions, and is valid yaml.')
  26. if state == 'list':
  27. module.exit_json(changed=False, results=rval, state="list")
  28. if state == 'absent':
  29. rval = yamlfile.delete(module.params['key'])
  30. module.exit_json(changed=rval[0], results=rval[1], state="absent")
  31. if state == 'present':
  32. if module.params['value_format'] == 'yaml':
  33. value = yaml.load(module.params['value'])
  34. elif module.params['value_format'] == 'json':
  35. value = json.loads(module.params['value'])
  36. if rval:
  37. rval = yamlfile.put(module.params['key'], value)
  38. module.exit_json(changed=rval[0], results=rval[1], state="present")
  39. if not module.params['content']:
  40. rval = yamlfile.create(module.params['key'], value)
  41. else:
  42. yamlfile.write()
  43. rval = yamlfile.get()
  44. module.exit_json(changed=rval[0], results=rval[1], state="present")
  45. module.exit_json(failed=True,
  46. changed=False,
  47. results='Unknown state passed. %s' % state,
  48. state="unknown")
  49. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  50. # import module snippets. This are required
  51. from ansible.module_utils.basic import *
  52. main()