Playbooks to a new Lilik
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.

110 lines
2.5 KiB

  1. #! /usr/bin/env python
  2. from ansible.module_utils.basic import AnsibleModule
  3. DOCUMENTATION = '''
  4. ---
  5. module: cert_request
  6. author: Edoardo Putti
  7. short_description: generate a host certificate request
  8. options:
  9. host:
  10. required: true
  11. description: FQDN for the host
  12. path:
  13. required: true
  14. description: path to key to be signed
  15. proto:
  16. required: true
  17. description:
  18. choices:
  19. - ssh
  20. - ssl
  21. '''
  22. EXAMPLES = '''
  23. - name: Generate ssl host request
  24. cert_request:
  25. host: "gandalf.lilik.it"
  26. path: "/etc/openvpn/openvpn.csr"
  27. proto: "ssl"
  28. - name: Generate ssh host request
  29. cert_request:
  30. host: ""
  31. path: "/etc/ssh/ssh_host_ed25519_key.pub"
  32. proto: "ssh"
  33. '''
  34. RETURN = '''
  35. type
  36. description: protocol used for the key
  37. returned: always
  38. sample: sign_request
  39. type: string
  40. keyType
  41. description: which type of key we are requesting
  42. returned: always
  43. sample:
  44. ssh_host
  45. ssl_host
  46. hostName
  47. description: FQDN of the host requesting a cert
  48. returned: always
  49. sample:
  50. example.lilik.it
  51. keyData:
  52. description: string representation of the key
  53. returned: always
  54. '''
  55. def main():
  56. module = AnsibleModule(
  57. argument_spec=dict(
  58. host=dict(
  59. required=True,
  60. type='str',
  61. ),
  62. path=dict(
  63. required=True,
  64. type='str',
  65. ),
  66. proto=dict(
  67. required=True,
  68. choices=['ssh', 'ssl'],
  69. ),
  70. client=dict(
  71. required=False,
  72. default=False,
  73. choices=[True, False],
  74. ),
  75. ),
  76. supports_check_mode=False,
  77. )
  78. host = module.params.get('host')
  79. path = module.params.get('path')
  80. proto = module.params.get('proto')
  81. client = module.params.get('client')
  82. with open(path, 'r') as src:
  83. result = {
  84. 'type': 'sign_request',
  85. 'request': {
  86. 'keyData': src.read(),
  87. },
  88. }
  89. if client:
  90. result['request']['keyType'] = '{}_user'.format(proto)
  91. result['request']['userName'] = host
  92. else:
  93. result['request']['keyType'] = '{}_host'.format(proto)
  94. result['request']['hostName'] = host
  95. module.exit_json(**result)
  96. if __name__ == '__main__':
  97. main()