logging_patch.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/python
  2. """ Ansible module to help with creating context patch file with whitelisting for logging """
  3. import difflib
  4. import re
  5. from ansible.module_utils.basic import AnsibleModule
  6. DOCUMENTATION = '''
  7. ---
  8. module: logging_patch
  9. short_description: This will create a context patch file while giving ability
  10. to whitelist some lines (excluding them from comparison)
  11. description:
  12. - "To create configmap patches for logging"
  13. author:
  14. - Eric Wolinetz ewolinet@redhat.com
  15. '''
  16. EXAMPLES = '''
  17. - logging_patch:
  18. original_file: "{{ tempdir }}/current.yml"
  19. new_file: "{{ configmap_new_file }}"
  20. whitelist: "{{ configmap_protected_lines | default([]) }}"
  21. '''
  22. def account_for_whitelist(file_contents, white_list=None):
  23. """ This method will remove lines that contain whitelist values from the content
  24. of the file so that we aren't build a patch based on that line
  25. Usage:
  26. for file_contents:
  27. index:
  28. number_of_shards: {{ es_number_of_shards | default ('1') }}
  29. number_of_replicas: {{ es_number_of_replicas | default ('0') }}
  30. unassigned.node_left.delayed_timeout: 2m
  31. translog:
  32. flush_threshold_size: 256mb
  33. flush_threshold_period: 5m
  34. and white_list:
  35. ['number_of_shards', 'number_of_replicas']
  36. We would end up with:
  37. index:
  38. unassigned.node_left.delayed_timeout: 2m
  39. translog:
  40. flush_threshold_size: 256mb
  41. flush_threshold_period: 5m
  42. """
  43. for line in white_list:
  44. file_contents = re.sub(r".*%s:.*\n" % line, "", file_contents)
  45. return file_contents
  46. def run_module():
  47. """ The body of the module, we check if the variable name specified as the value
  48. for the key is defined. If it is then we use that value as for the original key """
  49. module = AnsibleModule(
  50. argument_spec=dict(
  51. original_file=dict(type='str', required=True),
  52. new_file=dict(type='str', required=True),
  53. whitelist=dict(required=False, type='list', default=[])
  54. ),
  55. supports_check_mode=True
  56. )
  57. original_fh = open(module.params['original_file'], "r")
  58. original_contents = original_fh.read()
  59. original_fh.close()
  60. original_contents = account_for_whitelist(original_contents, module.params['whitelist'])
  61. new_fh = open(module.params['new_file'], "r")
  62. new_contents = new_fh.read()
  63. new_fh.close()
  64. new_contents = account_for_whitelist(new_contents, module.params['whitelist'])
  65. uni_diff = difflib.unified_diff(new_contents.splitlines(),
  66. original_contents.splitlines(),
  67. lineterm='')
  68. return module.exit_json(changed=False, # noqa: F405
  69. raw_patch="\n".join(uni_diff))
  70. def main():
  71. """ main """
  72. run_module()
  73. if __name__ == '__main__':
  74. main()