kubeclient_ca.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # vim: expandtab:tabstop=4:shiftwidth=4
  4. ''' kubeclient_ca ansible module '''
  5. import base64
  6. import yaml
  7. from ansible.module_utils.basic import AnsibleModule
  8. DOCUMENTATION = '''
  9. ---
  10. module: kubeclient_ca
  11. short_description: Modify kubeclient certificate-authority-data
  12. author: Andrew Butcher
  13. requirements: [ ]
  14. '''
  15. EXAMPLES = '''
  16. - kubeclient_ca:
  17. client_path: /etc/origin/master/admin.kubeconfig
  18. ca_path: /etc/origin/master/ca-bundle.crt
  19. - slurp:
  20. src: /etc/origin/master/ca-bundle.crt
  21. register: ca_data
  22. - kubeclient_ca:
  23. client_path: /etc/origin/master/admin.kubeconfig
  24. ca_data: "{{ ca_data.content }}"
  25. '''
  26. def main():
  27. ''' Modify kubeconfig located at `client_path`, setting the
  28. certificate authority data to specified `ca_data` or contents of
  29. `ca_path`.
  30. '''
  31. module = AnsibleModule( # noqa: F405
  32. argument_spec=dict(
  33. client_path=dict(required=True),
  34. ca_data=dict(required=False, default=None),
  35. ca_path=dict(required=False, default=None),
  36. backup=dict(required=False, default=True, type='bool'),
  37. ),
  38. supports_check_mode=True,
  39. mutually_exclusive=[['ca_data', 'ca_path']],
  40. required_one_of=[['ca_data', 'ca_path']]
  41. )
  42. client_path = module.params['client_path']
  43. ca_data = module.params['ca_data']
  44. ca_path = module.params['ca_path']
  45. backup = module.params['backup']
  46. try:
  47. with open(client_path) as client_config_file:
  48. client_config_data = yaml.safe_load(client_config_file.read())
  49. if ca_data is None:
  50. with open(ca_path) as ca_file:
  51. ca_data = base64.standard_b64encode(ca_file.read())
  52. changes = []
  53. # Naively update the CA information for each cluster in the
  54. # kubeconfig.
  55. for cluster in client_config_data['clusters']:
  56. if cluster['cluster']['certificate-authority-data'] != ca_data:
  57. cluster['cluster']['certificate-authority-data'] = ca_data
  58. changes.append(cluster['name'])
  59. if not module.check_mode:
  60. if len(changes) > 0 and backup:
  61. module.backup_local(client_path)
  62. with open(client_path, 'w') as client_config_file:
  63. client_config_string = yaml.dump(client_config_data, default_flow_style=False)
  64. client_config_string = client_config_string.replace('\'\'', '""')
  65. client_config_file.write(client_config_string)
  66. return module.exit_json(changed=(len(changes) > 0))
  67. # ignore broad-except error to avoid stack trace to ansible user
  68. # pylint: disable=broad-except
  69. except Exception as error:
  70. return module.fail_json(msg=str(error))
  71. if __name__ == '__main__':
  72. main()