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.

115 lines
4.6 KiB

7 years ago
  1. ---
  2. # This could be a role or custom module
  3. # Vars:
  4. # issue_cert_alt_name: Requested Subject Alternative Names, in a list.
  5. # issue_cert_common_name: Common Name included in the cert
  6. # issue_cert_copy_ca: Copy issuing CA cert needed
  7. # issue_cert_dir_mode: Mode of the placed cert directory
  8. # issue_cert_file_group: Group of the placed cert file and directory
  9. # issue_cert_file_mode: Mode of the placed cert file
  10. # issue_cert_file_owner: Owner of the placed cert file and directory
  11. # issue_cert_format: Format for returned data. Can be pem, der, or pem_bundle
  12. # issue_cert_hosts: List of hosts to distribute the cert to
  13. # issue_cert_ip_sans: Requested IP Subject Alternative Names, in a list
  14. # issue_cert_mount_path: Mount point in Vault to make the request to
  15. # issue_cert_path: Full path to the cert, include its name
  16. # issue_cert_role: The Vault role to issue the cert with
  17. # issue_cert_url: Url to reach Vault, including protocol and port
  18. - name: issue_cert | Ensure target directory exists
  19. file:
  20. path: "{{ issue_cert_path | dirname }}"
  21. state: directory
  22. group: "{{ issue_cert_file_group | d('root' )}}"
  23. mode: "{{ issue_cert_dir_mode | d('0755') }}"
  24. owner: "{{ issue_cert_file_owner | d('root') }}"
  25. - name: "issue_cert | Read in the local credentials"
  26. command: cat {{ vault_roles_dir }}/{{ issue_cert_role }}/userpass
  27. register: vault_creds_cat
  28. delegate_to: "{{ groups.vault|first }}"
  29. run_once: true
  30. - name: gen_certs_vault | Set facts for read Vault Creds
  31. set_fact:
  32. user_vault_creds: "{{ vault_creds_cat.stdout|from_json }}"
  33. delegate_to: "{{ groups.vault|first }}"
  34. run_once: true
  35. - name: gen_certs_vault | Log into Vault and obtain an token
  36. uri:
  37. url: "{{ hostvars[groups.vault|first]['vault_leader_url'] }}/v1/auth/userpass/login/{{ user_vault_creds.username }}"
  38. headers:
  39. Accept: application/json
  40. Content-Type: application/json
  41. method: POST
  42. body_format: json
  43. body:
  44. password: "{{ user_vault_creds.password }}"
  45. register: vault_login_result
  46. delegate_to: "{{ groups.vault|first }}"
  47. run_once: true
  48. - name: gen_certs_vault | Set fact for vault_client_token
  49. set_fact:
  50. vault_client_token: "{{ vault_login_result.get('json', {}).get('auth', {}).get('client_token') }}"
  51. run_once: true
  52. - name: gen_certs_vault | Set fact for Vault API token
  53. set_fact:
  54. issue_cert_headers:
  55. Accept: application/json
  56. Content-Type: application/json
  57. X-Vault-Token: "{{ vault_client_token }}"
  58. run_once: true
  59. when: vault_client_token != ""
  60. - name: "issue_cert | Generate {{ issue_cert_path }} for {{ issue_cert_role }} role"
  61. uri:
  62. url: "{{ issue_cert_url }}/v1/{{ issue_cert_mount_path|d('pki') }}/issue/{{ issue_cert_role }}"
  63. headers: "{{ issue_cert_headers }}"
  64. method: POST
  65. body_format: json
  66. body:
  67. alt_names: "{{ issue_cert_alt_names | d([]) | join(',') }}"
  68. common_name: "{{ issue_cert_common_name | d(issue_cert_path.rsplit('/', 1)[1].rsplit('.', 1)[0]) }}"
  69. format: "{{ issue_cert_format | d('pem') }}"
  70. ip_sans: "{{ issue_cert_ip_sans | default([]) | join(',') }}"
  71. register: issue_cert_result
  72. delegate_to: "{{ issue_cert_hosts|first }}"
  73. run_once: true
  74. - name: "issue_cert | Copy {{ issue_cert_path }} cert to all hosts"
  75. copy:
  76. content: "{{ issue_cert_result['json']['data']['certificate'] }}\n"
  77. dest: "{{ issue_cert_path }}"
  78. group: "{{ issue_cert_file_group | d('root' )}}"
  79. mode: "{{ issue_cert_file_mode | d('0644') }}"
  80. owner: "{{ issue_cert_file_owner | d('root') }}"
  81. - name: "issue_cert | Copy key for {{ issue_cert_path }} to all hosts"
  82. copy:
  83. content: "{{ issue_cert_result['json']['data']['private_key'] }}"
  84. dest: "{{ issue_cert_path.rsplit('.', 1)|first }}-key.{{ issue_cert_path.rsplit('.', 1)|last }}"
  85. group: "{{ issue_cert_file_group | d('root' )}}"
  86. mode: "{{ issue_cert_file_mode | d('0640') }}"
  87. owner: "{{ issue_cert_file_owner | d('root') }}"
  88. - name: issue_cert | Copy issuing CA cert
  89. copy:
  90. content: "{{ issue_cert_result['json']['data']['issuing_ca'] }}\n"
  91. dest: "{{ issue_cert_path | dirname }}/ca.pem"
  92. group: "{{ issue_cert_file_group | d('root' )}}"
  93. mode: "{{ issue_cert_file_mode | d('0644') }}"
  94. owner: "{{ issue_cert_file_owner | d('root') }}"
  95. when: issue_cert_copy_ca|default(false)
  96. - name: issue_cert | Copy certificate serial to all hosts
  97. copy:
  98. content: "{{ issue_cert_result['json']['data']['serial_number'] }}"
  99. dest: "{{ issue_cert_path.rsplit('.', 1)|first }}.serial"
  100. group: "{{ issue_cert_file_group | d('root' )}}"
  101. mode: "{{ issue_cert_file_mode | d('0640') }}"
  102. owner: "{{ issue_cert_file_owner | d('root') }}"