2023-12-24 17:09:08 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
DOCUMENTATION = r"""
|
2023-12-24 17:09:08 +00:00
|
|
|
---
|
2024-01-24 22:54:58 +00:00
|
|
|
module: ednxzu.hashistack.vault_init
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
short_description: Manages the initialization of HashiCorp Vault.
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
version_added: "1.0.0"
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
description:
|
|
|
|
- This module initializes HashiCorp Vault, ensuring that it is securely set up for use.
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
options:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url:
|
|
|
|
description: The URL of the HashiCorp Vault API.
|
2023-12-24 17:09:08 +00:00
|
|
|
required: true
|
|
|
|
type: str
|
2024-01-24 21:33:08 +00:00
|
|
|
key_shares:
|
|
|
|
description:
|
|
|
|
- The number of key shares to split the master key into.
|
|
|
|
- Default is 5.
|
|
|
|
required: false
|
|
|
|
type: int
|
|
|
|
default: 5
|
|
|
|
key_threshold:
|
2023-12-24 17:09:08 +00:00
|
|
|
description:
|
2024-01-24 21:33:08 +00:00
|
|
|
- The number of key shares required to reconstruct the master key.
|
|
|
|
- Default is 3.
|
2023-12-24 17:09:08 +00:00
|
|
|
required: false
|
2024-01-24 21:33:08 +00:00
|
|
|
type: int
|
|
|
|
default: 3
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
author:
|
2024-01-24 21:33:08 +00:00
|
|
|
- Bertrand Lanson (@ednxzu)
|
|
|
|
"""
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
EXAMPLES = r"""
|
|
|
|
# Example: Initialize HashiCorp Vault with default settings
|
|
|
|
- name: Initialize HashiCorp Vault
|
2023-12-24 17:09:08 +00:00
|
|
|
my_namespace.my_collection.my_test:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url: https://vault.example.com
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
# Example: Initialize HashiCorp Vault with custom key shares and threshold
|
|
|
|
- name: Initialize HashiCorp Vault with custom settings
|
2023-12-24 17:09:08 +00:00
|
|
|
my_namespace.my_collection.my_test:
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url: https://vault.example.com
|
|
|
|
key_shares: 7
|
|
|
|
key_threshold: 4
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = r"""
|
|
|
|
state:
|
|
|
|
description: Information about the state of HashiCorp Vault after initialization.
|
|
|
|
type: complex
|
2023-12-24 17:09:08 +00:00
|
|
|
returned: always
|
2024-01-24 21:33:08 +00:00
|
|
|
sample: {
|
|
|
|
"keys": [
|
|
|
|
"70e15679de84ac951633b5a79a3b8b45fcc719c6c219d785230a230674cbdff063",
|
|
|
|
"1a5badb309c9bf8ce384b13db28195f56c3adea70d29b58ad59ad8d573450632e2",
|
|
|
|
"2aa8ee4bdb87b70582e712a180720d877106b67838fcd8c606879ba462c0f6972b"
|
|
|
|
],
|
|
|
|
"keys_base64": [
|
|
|
|
"cOFWed6ErJUWM7WnmjuLRfzHGcbCGdeFIwojBnTL3/Bj",
|
|
|
|
"GlutswnJv4zjhLE9soGV9Ww63qcNKbWK1ZrY1XNFBjLi",
|
|
|
|
"KqjuS9uHtwWC5xKhgHINh3EGtng4/NjGBoebpGLA9pcr"
|
|
|
|
],
|
|
|
|
"root_token": "hvs.WasuYYUlbc1xsF2TIpbyNnWi"
|
|
|
|
}
|
|
|
|
"""
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-12-28 21:38:26 +00:00
|
|
|
import traceback
|
2023-12-27 12:40:25 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import hvac
|
|
|
|
except ImportError:
|
|
|
|
HAS_HVAC = False
|
|
|
|
HVAC_IMPORT_ERROR = traceback.format_exc()
|
|
|
|
else:
|
|
|
|
HVAC_IMPORT_ERROR = None
|
|
|
|
HAS_HVAC = True
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
2024-01-24 21:33:08 +00:00
|
|
|
api_url=dict(type="str", required=True),
|
|
|
|
key_shares=dict(type="int", required=False, default=5),
|
|
|
|
key_threshold=dict(type="int", required=False, default=3),
|
2023-12-24 17:09:08 +00:00
|
|
|
)
|
|
|
|
|
2024-01-24 22:54:58 +00:00
|
|
|
result = dict(changed=False, state="")
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2023-12-27 19:30:39 +00:00
|
|
|
if not HAS_HVAC:
|
|
|
|
module.fail_json(
|
2024-01-24 21:33:08 +00:00
|
|
|
msg="Missing required library: hvac", exception=HVAC_IMPORT_ERROR
|
2023-12-27 19:30:39 +00:00
|
|
|
)
|
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2023-12-28 21:38:26 +00:00
|
|
|
vault_init_result = None
|
2024-01-24 21:33:08 +00:00
|
|
|
client = hvac.Client(url=module.params["api_url"])
|
2023-12-24 17:09:08 +00:00
|
|
|
|
2023-12-28 21:38:26 +00:00
|
|
|
try:
|
|
|
|
if not client.sys.is_initialized():
|
|
|
|
vault_init_result = client.sys.initialize(
|
2024-01-24 21:33:08 +00:00
|
|
|
module.params["key_shares"], module.params["key_threshold"]
|
2023-12-28 21:38:26 +00:00
|
|
|
)
|
2024-01-24 21:33:08 +00:00
|
|
|
result["state"] = vault_init_result
|
2023-12-28 21:38:26 +00:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=f"Vault initialization failed: {str(e)}")
|
|
|
|
|
|
|
|
if vault_init_result:
|
2024-01-24 21:33:08 +00:00
|
|
|
result["changed"] = True
|
2023-12-24 17:09:08 +00:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
2023-12-24 17:09:08 +00:00
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
2024-01-24 21:33:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-12-24 17:09:08 +00:00
|
|
|
main()
|