2023-12-24 17:09:08 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
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)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
# 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
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
# 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'
|
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
|
|
|
api_url=dict(type='str', required=True),
|
2023-12-28 21:38:26 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
changed=False,
|
|
|
|
original_message='',
|
2023-12-28 21:38:26 +00:00
|
|
|
state=''
|
2023-12-24 17:09:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2023-12-27 19:30:39 +00:00
|
|
|
if not HAS_HVAC:
|
|
|
|
module.fail_json(
|
2023-12-28 21:38:26 +00:00
|
|
|
msg="missing_required_lib(hvac)",
|
2023-12-27 19:30:39 +00:00
|
|
|
exception=HVAC_IMPORT_ERROR
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
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(
|
|
|
|
module.params['key_shares'],
|
|
|
|
module.params['key_threshold']
|
|
|
|
)
|
|
|
|
result['state'] = vault_init_result
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=f"Vault initialization failed: {str(e)}")
|
|
|
|
|
|
|
|
if vault_init_result:
|
2023-12-24 17:09:08 +00:00
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|