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.

123 lines
5.0 KiB

  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_ca_filename: Filename for copied issuing CA cert (default ca.pem)
  8. # issue_cert_dir_mode: Mode of the placed cert directory
  9. # issue_cert_file_group: Group of the placed cert file and directory
  10. # issue_cert_file_mode: Mode of the placed cert file
  11. # issue_cert_file_owner: Owner of the placed cert file and directory
  12. # issue_cert_format: Format for returned data. Can be pem, der, or pem_bundle
  13. # issue_cert_hosts: List of hosts to distribute the cert to
  14. # issue_cert_ip_sans: Requested IP Subject Alternative Names, in a list
  15. # issue_cert_mount_path: Mount point in Vault to make the request to
  16. # issue_cert_path: Full path to the cert, include its name
  17. # issue_cert_role: The Vault role to issue the cert with
  18. # issue_cert_url: Url to reach Vault, including protocol and port
  19. - name: issue_cert | Ensure target directory exists
  20. file:
  21. path: "{{ issue_cert_path | dirname }}"
  22. state: directory
  23. group: "{{ issue_cert_file_group | d('root' )}}"
  24. mode: "{{ issue_cert_dir_mode | d('0755') }}"
  25. owner: "{{ issue_cert_file_owner | d('root') }}"
  26. - name: "issue_cert | Read in the local credentials"
  27. command: cat {{ vault_roles_dir }}/{{ issue_cert_role }}/userpass
  28. register: vault_creds_cat
  29. delegate_to: "{{ groups.vault|first }}"
  30. run_once: true
  31. - name: gen_certs_vault | Set facts for read Vault Creds
  32. set_fact:
  33. user_vault_creds: "{{ vault_creds_cat.stdout|from_json }}"
  34. delegate_to: "{{ groups.vault|first }}"
  35. run_once: true
  36. - name: gen_certs_vault | Ensure vault cert dir exists
  37. file:
  38. path: "{{ vault_cert_dir }}"
  39. state: directory
  40. recurse: yes
  41. owner: "vault"
  42. group: "root"
  43. mode: 0755
  44. - name: gen_certs_vault | install hvac
  45. pip:
  46. name: "hvac"
  47. state: "present"
  48. - name: gen_certs_vault | Pull vault CA
  49. get_url:
  50. url: "{{ issue_cert_url }}/v1/vault/ca/pem"
  51. dest: "{{ vault_cert_dir }}/ca.pem"
  52. validate_certs: no
  53. when: '"https" in issue_cert_url'
  54. - name: gen_certs_vault | Log into Vault and obtain a scoped token
  55. hashivault_token_create:
  56. url: "{{ issue_cert_url }}"
  57. token: "{{ vault_root_token | default(hostvars[groups.vault|first]['vault_root_token']) }}"
  58. ca_cert: "{{ vault_cert_dir }}/ca.pem"
  59. policies: "{{ user_vault_creds.username }}"
  60. display_name: "{{ user_vault_creds.username }}"
  61. register: vault_client_token_request
  62. run_once: true
  63. - name: gen_certs_vault | Pull token from request
  64. set_fact:
  65. vault_client_token: "{{ vault_client_token_request['token']['auth']['client_token'] }}"
  66. run_once: true
  67. - name: "issue_cert | Generate {{ issue_cert_path }} for {{ issue_cert_role }} role"
  68. hashivault_write:
  69. url: "{{ issue_cert_url }}"
  70. token: "{{ vault_client_token }}"
  71. ca_cert: "{% if 'https' in issue_cert_url %}{{ vault_cert_dir }}/ca.pem{% endif %}"
  72. secret: "{{ issue_cert_mount_path|d('/pki') }}/issue/{{ issue_cert_role }}"
  73. data:
  74. alt_names: "{{ issue_cert_alt_names | d([]) | join(',') }}"
  75. common_name: "{{ issue_cert_common_name | d(issue_cert_path.rsplit('/', 1)[1].rsplit('.', 1)[0]) }}"
  76. format: "{{ issue_cert_format | d('pem') }}"
  77. ip_sans: "{{ issue_cert_ip_sans | default([]) | join(',') }}"
  78. register: issue_cert_result
  79. run_once: "{{ issue_cert_run_once | d(false) }}"
  80. - name: "issue_cert | Copy {{ issue_cert_path }} cert to all hosts"
  81. copy:
  82. content: "{{ issue_cert_result['data']['data']['certificate'] }}\n"
  83. dest: "{{ issue_cert_path }}"
  84. group: "{{ issue_cert_file_group | d('root' )}}"
  85. mode: "{{ issue_cert_file_mode | d('0644') }}"
  86. owner: "{{ issue_cert_file_owner | d('root') }}"
  87. - name: "issue_cert | Copy key for {{ issue_cert_path }} to all hosts"
  88. copy:
  89. content: "{{ issue_cert_result['data']['data']['private_key'] }}"
  90. dest: "{{ issue_cert_path.rsplit('.', 1)|first }}-key.{{ issue_cert_path.rsplit('.', 1)|last }}"
  91. group: "{{ issue_cert_file_group | d('root' )}}"
  92. mode: "{{ issue_cert_file_mode | d('0640') }}"
  93. owner: "{{ issue_cert_file_owner | d('root') }}"
  94. - name: issue_cert | Copy issuing CA cert
  95. copy:
  96. content: "{{ issue_cert_result['data']['data']['issuing_ca'] }}\n"
  97. dest: "{{ issue_cert_path | dirname }}/{{ issue_cert_ca_filename | default('ca.pem') }}"
  98. group: "{{ issue_cert_file_group | d('root' )}}"
  99. mode: "{{ issue_cert_file_mode | d('0644') }}"
  100. owner: "{{ issue_cert_file_owner | d('root') }}"
  101. when: issue_cert_copy_ca|default(false)
  102. - name: issue_cert | Copy certificate serial to all hosts
  103. copy:
  104. content: "{{ issue_cert_result['data']['data']['serial_number'] }}"
  105. dest: "{{ issue_cert_path.rsplit('.', 1)|first }}.serial"
  106. group: "{{ issue_cert_file_group | d('root' )}}"
  107. mode: "{{ issue_cert_file_mode | d('0640') }}"
  108. owner: "{{ issue_cert_file_owner | d('root') }}"