2023-12-26 18:14:30 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2024-01-28 15:21:38 +00:00
|
|
|
from typing import Tuple
|
2023-12-29 22:40:34 +00:00
|
|
|
|
2023-12-26 18:14:30 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
DOCUMENTATION = r"""
|
2023-12-26 18:14:30 +00:00
|
|
|
---
|
|
|
|
module: my_test
|
|
|
|
|
|
|
|
short_description: This is my test module
|
|
|
|
|
|
|
|
# If this is part of a collection, you need to use semantic versioning,
|
|
|
|
# i.e. the version is of the form "2.5.0" and not "2.4".
|
|
|
|
version_added: "1.0.0"
|
|
|
|
|
|
|
|
description: This is my longer description explaining my test module.
|
|
|
|
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description: This is the message to send to the test module.
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
new:
|
|
|
|
description:
|
|
|
|
- Control to demo if the result of this module is changed or not.
|
|
|
|
- Parameter description can be a list as well.
|
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
# Specify this value according to your collection
|
|
|
|
# in format of namespace.collection.doc_fragment_name
|
|
|
|
# extends_documentation_fragment:
|
|
|
|
# - my_namespace.my_collection.my_doc_fragment_name
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Your Name (@yourGitHubHandle)
|
2023-12-29 22:40:34 +00:00
|
|
|
"""
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
EXAMPLES = r"""
|
2023-12-26 18:14:30 +00:00
|
|
|
# Pass in a message
|
|
|
|
- name: Test with a message
|
|
|
|
my_namespace.my_collection.my_test:
|
|
|
|
name: hello world
|
|
|
|
|
|
|
|
# pass in a message and have changed true
|
|
|
|
- name: Test with a message and changed output
|
|
|
|
my_namespace.my_collection.my_test:
|
|
|
|
name: hello world
|
|
|
|
new: true
|
|
|
|
|
|
|
|
# fail the module
|
|
|
|
- name: Test failure of the module
|
|
|
|
my_namespace.my_collection.my_test:
|
|
|
|
name: fail me
|
2023-12-29 22:40:34 +00:00
|
|
|
"""
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
RETURN = r"""
|
2023-12-26 18:14:30 +00:00
|
|
|
# These are examples of possible return values, and in general should use other names for return values.
|
|
|
|
original_message:
|
|
|
|
description: The original name param that was passed in.
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: 'hello world'
|
|
|
|
message:
|
|
|
|
description: The output message that the test module generates.
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: 'goodbye'
|
2023-12-29 22:40:34 +00:00
|
|
|
"""
|
2023-12-26 18:14:30 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-12-29 11:51:00 +00:00
|
|
|
import traceback
|
2023-12-26 18:14:30 +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-29 22:40:34 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
def unseal_vault(api_url: str, key_shares: list) -> Tuple[bool, dict]:
|
|
|
|
client = hvac.Client(url=api_url)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if client.sys.is_sealed():
|
|
|
|
return True, client.sys.submit_unseal_keys(key_shares)
|
|
|
|
else:
|
|
|
|
return False, {"message": "Vault is already unsealed"}
|
|
|
|
except hvac.exceptions.VaultError as e:
|
|
|
|
raise hvac.exceptions.VaultError(f"Vault unsealing failed: {str(e)}")
|
|
|
|
|
|
|
|
|
2023-12-26 18:14:30 +00:00
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
2023-12-29 22:40:34 +00:00
|
|
|
api_url=dict(type="str", required=True),
|
|
|
|
key_shares=dict(type="list", required=False, default=[]),
|
2023-12-26 18:14:30 +00:00
|
|
|
)
|
2024-01-24 22:54:58 +00:00
|
|
|
result = dict(changed=False, state="")
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
module = AnsibleModule(argument_spec=module_args, supports_check_mode=False)
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
client = hvac.Client(url=module.params["api_url"])
|
|
|
|
|
|
|
|
if not client.sys.is_sealed():
|
|
|
|
module.exit_json(**result)
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 11:51:00 +00:00
|
|
|
try:
|
2024-01-28 15:21:38 +00:00
|
|
|
if not HAS_HVAC:
|
|
|
|
module.fail_json(
|
|
|
|
msg="Missing required library: hvac", exception=HVAC_IMPORT_ERROR
|
|
|
|
)
|
|
|
|
vault_unseal_result, response_data = unseal_vault(
|
|
|
|
api_url=module.params["api_url"], key_shares=module.params["key_shares"]
|
|
|
|
)
|
2024-01-25 21:40:44 +00:00
|
|
|
|
2024-04-01 21:59:48 +00:00
|
|
|
if hvac.Client(url=module.params["api_url"]).sys.is_sealed():
|
2024-01-28 15:21:38 +00:00
|
|
|
module.fail_json(
|
|
|
|
msg="Vault unsealing failed. The unseal operation worked, but the vault is still sealed, maybe you didn't pass enough keys ?"
|
|
|
|
)
|
2024-01-25 21:40:44 +00:00
|
|
|
|
2024-01-28 15:21:38 +00:00
|
|
|
result["changed"] = vault_unseal_result
|
|
|
|
result["state"] = response_data
|
2023-12-29 11:51:00 +00:00
|
|
|
except hvac.exceptions.VaultError as ve:
|
|
|
|
module.fail_json(msg=f"Vault unsealing failed: {ve}")
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 11:51:00 +00:00
|
|
|
module.exit_json(**result)
|
2023-12-26 18:14:30 +00:00
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
|
2023-12-26 18:14:30 +00:00
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
2023-12-29 22:40:34 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-12-26 18:14:30 +00:00
|
|
|
main()
|